0% found this document useful (0 votes)
2 views3 pages

Java 2

Uploaded by

rupam.slg.in
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Java 2

Uploaded by

rupam.slg.in
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

34. What is the difference between final, finally, and finalize()?

Answer:
- final is a keyword used to denote that a variable’s value cannot be modified, a
method cannot be overridden, or a class cannot be subclassed.
- finally is a block used in exception handling that executes regardless of whether
an exception is thrown or caught, typically used for cleanup operations.
- finalize() is a method defined in the Object class that is called by the garbage
collector before an object is reclaimed. It is intended for cleanup operations, but
its use is discouraged due to unpredictability and performance concerns.

35. Can a final variable be modified?


Answer:
No, a final variable cannot be modified once it has been assigned. For a primitive
data type, its value remains constant, and for an object reference, the reference
cannot be changed to point to a different object. However, the internal state of an
object referenced by a final variable can be modified if the object's class allows
it.

36. What happens if we declare a method as final?


Answer:
Declaring a method as final prevents any subclass from overriding that method. This
is useful when you want to ensure that the behavior of the method remains
consistent across the class hierarchy and is not modified by subclasses.

37. Can we override a final method? Why or why not?


Answer:
No, you cannot override a final method because the final keyword explicitly
indicates that the method is not allowed to be overridden. Attempting to do so will
result in a compile-time error.

38. What is the use of the finally block in exception handling?


Answer:
The finally block is used to execute important cleanup code such as releasing
resources (like file handles or database connections) regardless of whether an
exception was thrown or caught. It ensures that the cleanup code always runs, even
if an exception occurs or is handled.

39. Will the finally block always execute?


Answer:
In most cases, the finally block will always execute after the try and catch
blocks, regardless of whether an exception was thrown. However, if the JVM exits
(for example, by calling [Link]()) or if the thread is killed, the finally
block may not execute.

40. Can we have a finally block without a try block?


Answer:
No, a finally block cannot exist independently. It must be associated with a try
block (with or without catch blocks). The syntax of Java requires that the finally
block directly follows a try block.

41. Can a finally block contain a return statement? What happens in that case?
Answer:
Yes, a finally block can contain a return statement. However, if a return statement
is executed in the finally block, it will override any return statements in the try
or catch blocks. This may lead to unexpected behavior, so it is generally
recommended to avoid using return statements in the finally block.

42. What is the purpose of the finalize() method in Java?


Answer:
The finalize() method is intended to be called by the garbage collector before an
object is removed from memory. Its purpose is to allow the object to clean up
resources or perform other finalization tasks. However, its execution is
unpredictable, and its use is discouraged in favor of explicit resource management
(such as using try-with-resources).

43. How is finalize() different from garbage collection?


Answer:
Garbage collection is the process by which the JVM automatically reclaims memory by
destroying objects that are no longer reachable. finalize() is a method that may be
called on an object by the garbage collector before the object’s memory is
reclaimed. Garbage collection is automatic and controlled by the JVM, whereas
finalize() is a callback that provides a chance to release resources, although its
invocation is neither guaranteed nor timely.

44. Can we explicitly call finalize() in Java? What happens when we do?
Answer:
Yes, you can explicitly call finalize() like any other method. However, doing so
does not trigger garbage collection, and it does not remove the object from memory.
It simply executes the method’s code, and it is not recommended since finalize() is
intended to be called by the garbage collector.

45. Is it guaranteed that finalize() will be called before an object is garbage


collected?
Answer:
No, it is not guaranteed that finalize() will be called before an object is garbage
collected. The JVM makes no assurances about the timing or even the eventual
execution of finalize(). Due to its unpredictability, reliance on finalize() for
releasing critical resources is discouraged.

46. What types of methods can an interface have?


Answer:
An interface can have:
- Abstract methods (implicitly public and abstract),
- Default methods (with a default implementation, introduced in Java 8),
- Static methods (which belong to the interface itself),
- And from Java 9 onward, private methods (to encapsulate common code within the
interface).
Note that interface methods are not allowed to be final.

47. Can an interface method be final or private? Why or why not?


Answer:
No, an interface method cannot be final because final methods cannot be overridden,
and the purpose of an interface is to allow multiple implementations. Traditional
interface methods were public and abstract. However, from Java 9, interfaces can
have private methods to share code between default methods, but these private
methods are not part of the interface's public API.

48. Can an interface have a constructor? Why or why not?


Answer:
No, an interface cannot have a constructor because it is not a concrete class and
cannot be instantiated. An interface is meant to define a contract of methods that
implementing classes must provide, so there is no need for a constructor.

49. What happens when a class implements multiple interfaces that have the same
default method?
Answer:
If a class implements multiple interfaces that provide a default method with the
same signature, the compiler will report an ambiguity error. The class must
override the conflicting default method and provide its own implementation to
resolve the conflict.

50. How does Java handle ambiguity when multiple interfaces define the same method?
Answer:
When multiple interfaces define the same default method, Java requires the
implementing class to override that method to resolve the ambiguity. Within that
overriding method, the class can choose to call one of the interface’s default
implementations using the syntax [Link]() if needed.

51. Can an abstract class have a constructor? If so, what is its purpose? Can an
abstract class have static methods? Why or why not? Can an abstract class have
private methods?
Answer:
- Yes, an abstract class can have a constructor. Although abstract classes cannot
be instantiated directly, the constructor is used to initialize fields and perform
setup for subclasses when they are created.
- An abstract class can have static methods because static methods belong to the
class itself rather than to any instance. These static methods can provide utility
functions or common behavior independent of the abstract nature of the class.
- An abstract class can also have private methods. Private methods are used to
encapsulate internal logic and can be called by other methods within the class.
They are not part of the abstract contract for subclasses.

52. Can an abstract class implement an interface? How?


Answer:
Yes, an abstract class can implement an interface. When an abstract class
implements an interface, it can choose to provide implementations for some or all
of the interface’s methods. If it does not implement all methods, it remains
abstract, and the responsibility to implement the remaining methods is passed to
its concrete subclasses.

53. What happens if we do not provide an implementation for an abstract method in a


subclass?
Answer:
If a subclass does not provide an implementation for an abstract method inherited
from its abstract parent class, then that subclass must also be declared as
abstract. Otherwise, the compiler will report an error because concrete classes are
required to implement all abstract methods from their abstract superclasses or
interfaces.

54. What is the difference between an abstract method and a concrete method in
Java?
Answer:
An abstract method is declared without an implementation (i.e., it has no body) and
must be implemented by a subclass. It serves as a placeholder, forcing subclasses
to provide the specific behavior. A concrete method, on the other hand, has a
complete implementation, including a method body, and can be executed as-is.
Abstract methods define a contract, while concrete methods provide actual behavior.

You might also like