Final Flashcards

(119 cards)

1
Q

Can you change the value of a final variable in Java?

A

No. Once a final variable is assigned a value, it cannot be reassigned.

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

What does the final keyword mean for variables?

A

It means the variable can be assigned only once.

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

When can a final variable be assigned?

A

Either at declaration or once inside a constructor or initializer block.

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

Can a final variable remain uninitialized?

A

Yes, but it must be assigned exactly once before use.

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

What happens if you try to reassign a final variable?

A

The compiler throws an error.

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

Does final make objects immutable?

A

No. final prevents reference reassignment, not internal state changes.

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

Example of final reference behavior?

A

final List list = new ArrayList(); list.add(“A”); is allowed.

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

Can primitive final variables change value?

A

No. Primitive values cannot change once assigned.

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

Can instance final variables be assigned in constructor?

A

Yes. They can be initialized in constructors.

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

Can static final variables be assigned later?

A

They must be assigned at declaration or in a static block.

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

Why use final variables?

A

To create constants and prevent accidental modification.

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

Can final variables be overridden?

A

No. Variables cannot be overridden.

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

Does final improve performance?

A

Sometimes, because constants may be optimized by compiler.

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

What is a blank final variable?

A

A final variable declared without initialization but assigned later exactly once.

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

Key rule to remember?

A

final variable = assign once only.

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

Can a class be declared final in Java?

A

Yes. A class can be marked final to prevent it from being extended.

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

What does the final keyword mean for a class?

A

It means no other class can inherit from it.

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

Why would you make a class final?

A

To prevent modification through inheritance and ensure security or design integrity.

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

Example of a final class in Java API?

A

java.lang.String is final.

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

Can a final class have methods?

A

Yes. It can have any methods, but none can be overridden through inheritance.

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

Can a final class be abstract?

A

No. abstract requires inheritance, while final forbids it.

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

Can a final class be instantiated?

A

Yes. Final only prevents inheritance, not object creation.

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

What happens if you try to extend a final class?

A

The compiler throws an error.

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

Can a final class contain final methods?

A

Yes. Methods inside a final class can also be marked final.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Does final class improve security?
Yes. It prevents malicious subclasses from altering behavior.
26
Can interfaces be final?
No. Interfaces are meant to be implemented.
27
Can a class be both final and private?
Yes, but only for nested classes.
28
Does final affect object immutability?
Not directly. A final class can still have mutable fields.
29
Why are immutable classes often declared final?
To prevent subclasses from modifying behavior and breaking immutability guarantees.
30
Key rule to remember?
final class = cannot be inherited.
31
How do you create a final method in Java?
Add the final modifier to the method declaration.
32
What does a final method mean in Java?
It means the method cannot be overridden by subclasses.
33
Example of a final method declaration?
public final void display() {}
34
Why would you declare a method final?
To prevent subclasses from modifying its behavior.
35
Can a final method be inherited?
Yes. It can be inherited but not overridden.
36
Can a final method be overloaded?
Yes. Overloading is allowed because it does not override the method.
37
Can a final method be abstract?
No. abstract requires overriding, while final forbids it.
38
Can a final method be static?
Yes, but static methods already cannot be overridden.
39
What happens if a subclass tries to override a final method?
The compiler throws an error.
40
Can constructors be final?
No. Constructors cannot be overridden.
41
Does marking a method final improve performance?
Sometimes. It can allow JVM optimizations like inlining.
42
Can private methods be final?
Yes, but it is redundant because private methods cannot be overridden anyway.
43
Why do security-sensitive classes use final methods?
To prevent subclasses from altering critical logic.
44
Can an entire class be protected from overriding methods?
Yes. Mark the class final.
45
Key rule to remember?
final method = cannot be overridden.
46
How can you prohibit inheritance in Java?
By declaring a class as final.
47
What does marking a class final do?
It prevents any other class from extending it.
48
Example of prohibiting inheritance?
final class A {}
49
What happens if you try to extend a final class?
The compiler throws an error.
50
Why would you prohibit inheritance?
To protect class behavior, enforce design constraints, or ensure security.
51
Can abstract classes be final?
No. abstract requires inheritance, while final forbids it.
52
Can methods be used to restrict inheritance behavior?
Yes. Marking methods final prevents overriding but does not stop class inheritance.
53
Can constructors restrict inheritance?
Yes. Private constructors prevent subclass creation outside the class.
54
What is another way to prevent inheritance besides final class?
Using a private constructor and making class effectively non-subclassable.
55
Example of private constructor inheritance prevention?
class A { private A() {} }
56
Does making a class final prevent object creation?
No. It only prevents subclassing.
57
Are Java standard library classes final?
Some are, like String, to prevent behavior modification.
58
Why is String class final?
To ensure immutability and security.
59
Can interfaces be final?
No. Interfaces are meant to be implemented.
60
Key rule to remember?
final class = no inheritance allowed.
61
Why is the Integer class final in Java?
Integer is final to prevent subclassing that could alter its behavior, ensuring immutability, security, and consistent numeric operations.
62
What would happen if Integer were not final?
Subclasses could override methods and break correctness or security of integer operations.
63
Is Integer immutable?
Yes. Integer objects are immutable once created.
64
Why is immutability important for wrapper classes?
It ensures thread safety, predictability, and safe reuse of cached values.
65
How does final support immutability?
It prevents subclasses from adding mutable behavior.
66
Are all wrapper classes final?
Yes. Wrapper classes like Integer, Double, Boolean, and Character are final.
67
Why must wrapper classes be reliable?
Because they are widely used in collections, caching, autoboxing, and core APIs.
68
What optimization relies on Integer immutability?
Integer caching for values between -128 and 127.
69
Could subclassing break caching optimizations?
Yes. Modified behavior could invalidate assumptions about value equality.
70
Does final improve performance?
It can help JVM optimize calls because methods cannot be overridden.
71
Why is security a reason for making Integer final?
It prevents malicious subclasses from altering numeric logic.
72
Can you extend Integer class?
No. The compiler prevents extending final classes.
73
Are immutable classes usually final?
Yes. Making them final protects their state guarantees.
74
What is the design principle behind final wrapper classes?
Reliability and integrity of core language types.
75
Key rule to remember?
Integer is final to preserve correctness, immutability, and security.
76
What is a blank final variable in Java?
A blank final variable is a final variable declared without an initial value and assigned exactly once later.
77
When must a blank final variable be initialized?
It must be assigned before it is used.
78
Where can a blank final instance variable be initialized?
In a constructor or instance initializer block.
79
Where can a blank final static variable be initialized?
In a static initializer block.
80
Can a blank final variable be reassigned?
No. It can be assigned only once.
81
Why are blank final variables useful?
They allow flexible initialization while still enforcing immutability.
82
Example of blank final variable?
final int x; x = 10;
83
What happens if a blank final variable is not initialized?
The compiler throws an error.
84
Can a blank final variable be initialized conditionally?
Yes, as long as all execution paths assign it exactly once.
85
Are blank final variables allowed for local variables?
Yes. Local variables can also be blank final.
86
Does blank final mean null initially?
No. It has no value until explicitly assigned.
87
Can constructors assign blank final variables multiple times?
No. Each constructor must assign it once and only once.
88
Why does Java enforce single assignment for final variables?
To ensure immutability and predictable program behavior.
89
Difference between final and blank final?
final is assigned immediately; blank final is assigned later exactly once.
90
Key rule to remember?
Blank final = declare now, assign once later.
91
How can you initialize a blank final variable in Java?
A blank final variable can be initialized once in a constructor, initializer block, or at declaration depending on its type.
92
How is a blank final instance variable initialized?
It must be assigned inside a constructor or instance initializer block.
93
How is a blank final static variable initialized?
It must be assigned in a static initializer block or at declaration.
94
Why must blank final variables be initialized exactly once?
Because final variables can only be assigned one time.
95
What happens if a constructor does not initialize a blank final field?
The compiler throws an error because all execution paths must assign it.
96
Can a blank final variable be assigned in a method?
No. Instance blank finals must be assigned during object construction.
97
Can static blank final variables be initialized in constructors?
No. Static variables belong to the class, not instances.
98
Example of initializing blank final instance variable?
class A { final int x; A(){ x = 10; } }
99
Example of initializing blank final static variable?
class A { static final int x; static { x = 5; } }
100
Can multiple constructors assign a blank final variable?
Yes, but each constructor must assign it exactly once.
101
Can you assign blank final variable conditionally?
Yes, as long as all code paths assign it.
102
What error occurs if blank final is assigned twice?
Compile-time error.
103
What is the advantage of blank final variables?
They allow delayed initialization while preserving immutability.
104
Difference between final and blank final initialization timing?
final → assigned immediately; blank final → assigned later once.
105
Key rule to remember?
Blank final must be assigned once before use.
106
Can the main method be declared final in Java?
Yes. The main method can be declared final.
107
Why is it allowed to mark main as final?
Because JVM only requires the signature public static void main(String[] args); additional modifiers like final are permitted.
108
Does making main final affect program execution?
No. JVM can still invoke it normally.
109
What does final mean for a method?
It prevents subclasses from overriding it.
110
Does marking main final prevent overloading?
No. final prevents overriding, not overloading.
111
Is main usually declared final in practice?
No. It is rarely marked final because there is usually no need.
112
Can static methods like main be overridden anyway?
No. Static methods cannot be overridden, only hidden.
113
Is marking main final redundant?
Yes. Since static methods cannot be overridden, final adds no practical effect.
114
Can main be declared final static public?
Yes. Modifier order does not matter as long as syntax is correct.
115
Can main be abstract or synchronized?
It cannot be abstract, but it can be synchronized.
116
Does JVM require main to be non-final?
No. JVM does not care whether main is final.
117
Which modifiers are mandatory for main?
public, static, void.
118
Can final main exist in inherited classes?
Yes, but subclasses cannot override it.
119
Key rule to remember?
main can be final, but it is unnecessary.