Java Interview questions on Final, Finally and Finalize keywords.

Published On: 3/4/2023
Author: Admin
Share Now

What is Final Variable in Java?

When a variable is declared as final in Java, the value can’t be modified once it has been assigned. Value can be assigned only by the constructor of the class is no value is assigned to the final variable.

Explain final method in Java!

In a class if there is a method which should not be inherited or overwritten by its children classes then that is declared as Final Method.

A method declared as final cannot be overwritten by child classes.

When Java throws compilation error – “modifier final not allowed here”?

A constructor cannot be marked as final because whenever a class is inherited, the constructors are not inherited and it throws compilation error.

What is final Class?

Final Keyword is used for class when you want to restrict Inheritance of class. No class can be inherited from class declared as Final Class.

When Final Keyword is used in Java?

The final keyword is used as defining something as constant i.e. final and represents the non-access modifier. Inheritance of a final class and overriding of a final method is restricted by the use of the final keyword. After incorporating the final keyword the variable value becomes fixed.

When variable will throw error?

Variable declared as Final throws error when we try to modify assigned value.

final int iNum=20;
iNum = 0; // error
Second statement will throw error.

What is Finally block?

It is the block in a program where all the codes written inside it get executed irrespective of handling of exceptions. i.e. all code written in Finally block will be executed.

try {
int variable = 10;
catch (Exception exception) {
System.out.println("Exception occurred");
finally {
System.out.println("In finally block");

In above code “In finally block” will be printed, irrespective of other code execution.

What Is Finalize method?

Finalize method is called prior to the Garbage Collection. Finalize method called to implement clean up activity.

When Finally block will not be executed?

If we use System.exit()

prior to Finally Block execution and if there is fatal error while code execution then Finally block will not be executed.

Above JAVA questions are useful for preparation of Java Interview. These questions are for fresher level.

Practice Tests Available

Currently no practice papers available for above exam. We are working on it and tests will be available soon.


Subjects (Free Online Tests)

Your Comment:
Name :
Comment :
(0) Comments:

 Full Length Mock Tests
 Answers with Explanation**
 Timer Based Exams
 Instant Result and assesment
 Detailed analasys of Result