Java Interview Questions 8 of 9 - Final Keyword Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is the use of the final keyword in Java?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Can a class be marked final in Java?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can we create a final method in Java?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can we prohibit inheritance in Java?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can we make local variable final in Java?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why Integer class in final in Java?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a blank final variable in Java?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When to make a class final in Java?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can we initialize a blank final variable?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is constant in Java?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Is it allowed to declare main method as final?

A

Yes, we can mark the main method as final.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly