Type Here to Get Search Results !

Top 1000 Java Interview Questions & Answers - Section VIII - Final Keyword ( Java )

0

 


We are sharing the top 1000 Java interview questions , these questions are frequently asked by the recruiters. Java interview questions can be asked from any core java topic . So here we tried our best to provide you the java interview questions and answers for experienced which should be in your to do list before facing java questions in  technical interview. 

In this Section we have the top selected interview questions on Final Keyword ( Java ).In the Java programming language, the final keyword is used in several contexts to define an entity that can only be assigned once.Once a final variable has been assigned, it always contains the same value. If a final variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object. This applies also to arrays, because arrays are objects; if a final variable holds a reference to an array, then the components of the array may be changed by operations on the array, but the variable will always refer to the same array.

Each question here about final keyword in java is accompanied with an answer so that you can prepare for job interview in short time.We have compiled this list after attending dozens of technical interviews in top-notch companies like- Facebook, Amazon,Oracle, Netflix, Microsoft etc.

Once you go through them in the first pass, mark the questions that you could not answer by yourself. Then, in second pass go through only the difficult questions.After going through this book 2-3 times, you will be well prepared to face a technical interview for a Java Developer position from Software Engineer level to Principal Engineer level.

All the best!


Section VIII - Final Keyword ( Java )


77. What is the use of the final keyword in Java?

The final keyword can be used with a class, method, and variables. If it is used with class then it prevents inheritance by not allowing you to create sub classes. If it is used with methods then it prevents overriding, you cannot override a final method in Java. If it is used with variables then they are treated as constant because you cannot change their value once assigned.


78. How can you change the value of a final variable in Java?

Java does not allow changing the value of a final variable. Once the value is set, it cannot be changed.


79.  Can we make a variable final in Java? What is different between a normal variable and final variable?

Yes, you can make a variable final in Java. The difference between normal variable and final variable comes from multiple assignments. you can re-assign value to a normal variable but you cannot change the value of a final variable once assigned.


 

80. Can a class be marked final in Java?

Yes a class can be marked final in Java. Once a class is marked final, it cannot be extended.

 

81. Can we make a method final in Java? explain the difference between normal method and final method

Yes, you can make a method final in Java. The difference is in the fact that you can override a non-final method, provided it'st not private and static but you cannot override a final method in Java.


82. How can we create a final method in Java?

To mark a method, add modifier final to that method. A final method can not be overridden by a child class.

 


83. How can we prohibit inheritance in Java?

If you mark a class final, it cannot be extended. This will prohibit the inheritance of that class in Java.


84. Can we make local variable final in Java?

Yes, you can make local variable final in Java. In fact, this was mandatory, if you want to access the local variable inside an Anonymous class until Java 8. From Java 8 onward, you don't need to make it final but make sure you don't change the value once assigned. This is also known as an effectively final variable in Java.

 

85. Why Integer class in final in Java?

Integer class is a wrapper for int. If it is not marked final, then any other class can extend it and modify the behavior of Integer operations. To avoid this Integer wrapper class is marked as final.

 


86. What is a blank final variable in Java?

When we declare a final variable without giving any initial value, then it is called blank final variable.

 

87. When to make a class final in Java?

You make a class final when you don't want anyone should extend it. This is mainly done due to security reason because it also hampers extensibility of your program. A couple of examples of final classes in JDK is String, Integer, and other wrapper class.


88. How can we initialize a blank final variable?

A blank final instance variable can be initialized in a constructor

A blank final static variable can be initialized in the static block of class.



89.  What is constant in Java?

A static final variable is known as constant in Java. They are also known as a compile time constant because of their value at the time of compilation. They are also inlined at the client end, means if you are using a static final variable then its value will be copied to your class at compile time. Which also means that you need to recompile all the classes which use the static final variable, whenever you change the value of a static final field. This has the potential to create subtle bugs.


90. Is it allowed to declare main method as final?

Yes, we can mark the main method as final.


91. Can a class be abstract and final at the same time?

No, it's not possible because the only way to use abstract class is by extending it and creating a concrete subclass, while it's not possible to extend a final class in Java.




Recommended Posts

Top 1000 Java Interview Questions & Answers - Section VII - Abstraction

Top 1000 Java Interview Questions & Answers - Section VI - Polymorphism

Top 1000 Java Interview Questions & Answers - Section V - Method Overloading and Overriding

Top 1000 Java Interview Questions & Answers - Section I - Java Basics

Top 1000 Java Interview Questions & Answers - Section II - OOPs ( Object Oriented Programming )

Top 1000 Java Interview Questions & Answers - Section III - Inheritance

Top 1000 Java Interview Questions & Answers - Section IV - Static

Core Java Important Questions Part 1

Core Java Important Questions Part 2

Core Java Important Questions Part 3

Core Java Important Questions Part 4

Core Java Important Questions Part 5

Core Java Important Questions Part 6

Core Java Important Questions Part 7

Core Java Important Questions Part 8

Core Java Important Questions Part 9

Java Collections Interview Questions Part 1

Java Collections Interview Questions Part 2

Java Collections Interview Questions Part 3

Java MCQs with Answers Set I

Java MCQs with Answers Set II

Java MCQs with Answers Set III

If you have any doubts join the discussion below ,our Moderator will reply to your Queries

Post a Comment

0 Comments

Top Post Ad

Below Post Ad