Chapter 7: Beyond Classes Flashcards

1
Q

Anonymous classes can be declared static?

A

False

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

Anonymous classes always have an implicit reference to their enclosing class?

A

True

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

Anonymous classes are a type of nested class that can be static?

A

False

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

Since anonymous classes cannot be static, they must be associated with an instance of the enclosing class?

A

True

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

The keyword “static” can be used when defining anonymous classes?

A

False

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

Interface variables in Java are public, static, and final by default.

A

True

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

Interfaces in Java are required to define at least one method.

A

False: Interfaces can be empty, and the compiler automatically treats them as abstract even without method definitions.

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

An interface can be instantiated directly

A

False: Interfaces cannot be instantiated because they only provide method signatures without implementations.

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

Interfaces are implicitly considered abstract, even without the abstract keyword.

A

True

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

Interfaces can be marked as final in Java.

A

False: Interfaces cannot be final, as final would prevent implementation, defeating their purpose.

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

In Java 17, if a class inherits two interfaces with the same default method signature, it must override the method. (True/False) If false, why?

A

True

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

A class can inherit conflicting default methods from multiple interfaces without requiring an override. (True/False) If false, why?

A

False – The compiler enforces overriding to resolve ambiguity in method implementation.

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

The syntax to access an inherited default method is <interface>.super.<method>. (True/False) If false, why?</method></interface>

A

True

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

In Java 17, a class can directly call a default method from an interface without specifying the interface name. (True/False) If false, why?

A

False – It must use <interface>.super.<method> to specify the source interface.</method></interface>

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

Default methods in interfaces cannot be overridden by implementing classes. (True/False) If false, why?

A

False – Implementing classes can override default methods to provide custom behavior.

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

An interface can extend multiple interfaces using the extends keyword.

A

True

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

A class can extend multiple interfaces.

A

False – A class implements interfaces, but it can extend only one class.

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

Interfaces are initialized as part of a class hierarchy.

A

False – Interfaces do not have constructors and are not part of instance initialization.

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

A class can extend an interface.

A

False – A class can only implement an interface, not extend it.

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

An interface can implement another interface.

A

False – An interface can extend another interface, but it cannot implement one.

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

A method that properly overrides inherited methods can use covariant return types.

A

True

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

Interfaces are implicitly abstract.

A

True

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

Interface variables are implicitly public, static, and final.

A

True

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

If a method or variable is marked with a conflicting modifier, the compiler applies the public modifier without conflict.

A

The compiler detects a conflict and generates an error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Both abstract classes and interfaces use implicit modifiers.
False – Only interfaces use implicit modifiers; abstract classes do not. If false, why?
26
A constant variable in an interface is implicitly public static final. If false, why?
True
27
An abstract method in an interface is implicitly private abstract. If false, why?
False – It is implicitly public abstract.
28
A default method in an interface must be marked with the default keyword and have a method body. If false, why?
True
29
A static method in an interface is implicitly private static. If false, why?
False – It is implicitly public static.
30
A private method in an interface can have an abstract modifier. If false, why?
False – Private methods must have a method body, so they cannot be abstract.
31
A private static method in an interface does not require a method body. If false, why?
False – All private methods must have a method body.
32
A method marked protected in an interface is allowed. If false, why?
False – Interfaces do not support protected methods because they cannot be extended like classes.
33
A method in an interface without an access modifier is implicitly package-private. If false, why?
False – It is implicitly public, to maintain backward compatibility.
34
A default method in an interface can be marked final. If false, why?
False – Default methods cannot be final because they are meant to be overridden.
35
A class must override a default method if it inherits two default methods with the same signature. If false, why?
True
36
A class implementing an interface must override all its default methods. If false, why?
False – Default methods are optional to override; they have a provided implementation.
37
An interface can contain both static and private methods. If false, why?
True
38
A class implementing multiple interfaces with the same default method must override the method to resolve ambiguity. If false, why?
True
39
A default method in an interface behaves only like an instance method, not like a static method. If false, why?
False – It exhibits properties of both static and instance methods.
40
The super keyword is used with interface names to indicate following instance inheritance. If false, why?
True
41
Interfaces can declare static methods without using the static keyword. If false, why?
False – A static method must be explicitly marked with static.
42
Static methods in interfaces are always private by default. If false, why?
False – They are implicitly public if no access modifier is provided.
43
Static methods in interfaces cannot be inherited by implementing classes. If false, why?
True
44
An interface can define a static method without a method body. If false, why?
False – A static method must always have a method body.
45
Static methods in interfaces can be marked as final. If false, why?
False – Static methods in interfaces cannot be final.
46
Private static interface methods can be called by any method within the interface. If false, why?
True
47
A private non-static interface method can be called by both static and non-static methods within the interface. If false, why?
False – It can only be called by non-static methods.
48
Private interface methods were introduced primarily to enhance encapsulation and reduce code duplication. If false, why?
True
49
A private interface method can be called directly from a class implementing the interface. If false, why?
False – Private methods in interfaces cannot be accessed by implementing classes.
50
A class can override a private method of an interface. If false, why?
False – Private methods in interfaces are not inherited, so they cannot be overridden.
51
An interface can have both default and static methods. If false, why?
True
52
A private interface method can be marked as abstract. If false, why?
False – Private methods must have a method body, so they cannot be abstract.
53
A private static interface method can be accessed by another private static method within the same interface. If false, why?
True
54
Default and private non-static methods in an interface can access abstract methods. If false, why?
True.
55
Abstract methods in an interface require an instance of the interface for access. If false, why?
True.
56
Constant variables in an interface can be accessed without an instance. If false, why?
True.
57
Private static methods in an interface are accessible from other static methods within the same interface. If false, why?
True
58
Default methods in an interface are accessible by classes implementing the interface. If false, why?
True.
59
Static methods in an interface require the interface name for access. If false, why?
True
60
Abstract methods in an interface are accessible within the interface itself. If false, why?
Abstract methods cannot have a body and are not accessible within the interface. (False)
61
Private methods in an interface are accessible from implementing classes. If false, why?
Private methods are only accessible within the interface itself. (False)
62
You can extend an enum to add more values. If false, why?
Enums are final by design and cannot be extended. (False)
63
Enum values can be dynamically modified at runtime. If false, why?
Enum values are constants and immutable. (False)
64
The semicolon at the end of a simple enum’s value list is required. If false, why? )
It is optional unless additional code follows. (False
65
Enums do not offer type safety compared to using constants. If false, why?
Enums are type-safe, unlike numeric or string constants. (False)
66
An enum provides a values() method to get an array of all of the values. You can use this like any normal array, including in a for-each loop.
True
67
Each enum value has a corresponding int value, and the values are listed in the order in which they are declared.
True
68
You can’t compare an int and an enum value directly since an enum is a type, like a Java class, and not a primitive int.
True
69
The valueOf() method allows retrieving an enum value from a String, and the String must match the enum value exactly.
True
70
Enums can be used in both switch statements and switch expressions.
True
71
All enum constructors are implicitly private, and using public or protected in the constructor will result in a compilation error
True
72
Enum values can be modified after they are created.
Enum values should be immutable since they are shared in the JVM, and modifying them is a poor practice.
73
Enum constructors can be explicitly declared as public or protected.
Enum constructors are always private (implicitly or explicitly); public or protected modifiers cause a compilation error.
74
Enums must contain only a list of values and cannot have instance variables or methods.
Enums can have instance variables and methods, making them more complex if needed.
75
The order of enum values can be changed at runtime.
Enum values are assigned at compile-time and cannot be reordered at runtime.
76
You can extend an enum in Java.
Enums are implicitly final, meaning they cannot be subclassed.
77
In an enum, instance variables are shared across instances of the enum.
Each enum value is a singleton, so instance variables are specific to each enum constant.
78
A sealed class is a class that restricts which other classes may directly extend it.
True
79
A sealed class declares a list of classes that can extend it, while the subclasses declare that they extend the sealed class.
True
80
Sealed classes are commonly declared with the abstract modifier, although this is certainly not required.
True
81
Every class that directly extends a sealed class must specify exactly one of the following three modifiers: final, sealed, or non-sealed.
True
82
Besides classes, interfaces can also be sealed, and many of the same rules apply.
True
83
The permits keyword is mandatory when declaring a sealed class.
It is optional if all subclasses are in the same file or nested.
84
A non-sealed class cannot be extended by other classes.
(A non-sealed class removes restrictions and allows unrestricted extension.)
85
A sealed class can be extended by any class in the same package.
(Only explicitly permitted subclasses can extend a sealed class.)
86
Sealed interfaces must have their permitted classes/interfaces listed using permits.
(The permits clause is optional in certain conditions.)
87
The permits clause must always explicitly list all allowed subclasses.
(It can be omitted if subclasses are declared in the same file or are nested.)
88
A final interface can extend a sealed interface.
(Interfaces cannot be marked final; they can only be sealed or non-sealed.)
89
Sealed interfaces and their implementations must be in different packages.
(They must be in the same package or module.)
90
A sealed class and its permitted subclasses must be in different files.
(They can be in the same file or even be nested.)
91
The permits clause must always be used for nested subclasses.
(It can be omitted if all subclasses are declared within the same sealed class.)
92
Sealed classes are declared with the sealed and permits modifiers.
True.
93
Sealed classes must always be declared in a named module along with their direct subclasse
They must be in the same package or module, but not necessarily a named module
94
Direct subclasses of a sealed class can be marked as abstract.
(False – They must be marked final, sealed, or non-sealed.)
95
The permits clause is optional if the sealed class and its direct subclasses are in the same file.
True
96
A sealed interface can only limit the classes that implement it, not the interfaces that extend it.
(False – It can limit both implementing classes and extending interfaces.)
97
A POJO requires a no-arg constructor like a JavaBean.
(False – A POJO does not require a no-arg constructor.)
98
Encapsulation prevents modifying methods in a class.
(False – Encapsulation restricts direct access to instance variables but allows method modifications.)
99
Encapsulation ensures that method behavior cannot change in the future.
(False – Methods can be modified while maintaining the same signatures.)
100
Records allow defining explicit setter methods.
(False – Record fields are implicitly final, so setters are not allowed.)
101
The constructor of a record allows parameters in any order.
(False – It follows the same order as the record declaration.)
102
The equals() method of a record allows customization without overriding it.
(False – Customization requires explicitly overriding equals().)
103
Records automatically generate a toString() method that includes all fields.
True
104
The final modifier prevents a class from being extended.
True
105
Java allows a class to have multiple direct parent classes.
(False – Java supports single inheritance, but a class can implement multiple interfaces.)
106
A class in Java can extend multiple classes at the same time.
(False – Java does not allow multiple inheritance for classes.)
107
Abstract classes are an exception to Java’s single inheritance rule.
(False – Java’s exception to single inheritance is interfaces, not abstract classes.)
108
A subclass in Java can inherit directly from two different superclasses.
(False – A subclass can have only one direct parent class.)
109
If a class has a parent, it cannot have multiple child classes.
(False – Single inheritance does not prevent a parent class from having multiple children.)
110
All Java classes implicitly inherit from java.lang.Object.
True
111
Primitive types like int and boolean inherit from Object.
(False – Primitive types are not classes and do not inherit from Object.)
112
If a class extends another, Java implicitly adds extends Object.
(False – Java does not implicitly extend Object if a class already extends another.)
113
If a class has no explicit superclass, Java automatically extends java.lang.Object.
(True)
114
The equals() method in Java is always useful for comparing objects.
(False – The equals() method must be overridden for meaningful comparisons.)
115
By default, toString() in Java provides a human-readable description of an object.
(False – The default toString() returns a memory reference unless overridden.)
116
Java allows a class to extend multiple classes using the extends keyword.
(False – A class can extend only one class but implement multiple interfaces.)
117
The inheritance structure of every Java class ends with null.
(False – The inheritance structure always ends with Object, not null.)
118
Java automatically calls super.toString() when printing an object.
(False – super.toString() is not automatically called unless explicitly implemented.)
119
Object is the only class in Java that does not have a parent.
True
120
All permitted subtypes of a sealed type must belong to the same package.
False – They can belong to the same package or the same named module.
121
A sealed class in a named module can have permitted subtypes from different modules.
False – All permitted subtypes must belong to the same module as the sealed class.
122
If a sealed class is in an unnamed module, all its permitted subtypes must be in the same package.
✅ True – This is required to avoid a compile-time error.
123
A sealed class in a named module can permit subtypes from different packages as long as they are in the same module.
✅ True – The package restriction applies only to unnamed modules.
124
A permitted subclass of a sealed class must always be in the same package.
❌ False – This is only true for unnamed modules. For named modules, the permitted subclass can be in a different package as long as it is in the same module.
125
The permits clause of a sealed class is optional if all permitted subclasses are declared in the same file.
✅ True – If all subclasses are declared in the same compilation unit, Java can infer the permitted subclasses.
126
If a sealed class is declared in an unnamed module, it can permit subclasses from different modules.
❌ False – Unnamed modules don’t allow cross-module subclassing.
127
If a sealed class does not specify permits, it becomes non-sealed automatically.
❌ False – It only means Java will infer the permitted subclasses from the same compilation unit.
128
A sealed class in a named module can only have subclasses that are also in named modules.
✅ True – The subclasses must be in the same named module.
129
If a sealed class and its permitted subclasses are in different named modules, Java will generate a compile-time error.
✅ True – A named module cannot have permitted subclasses in another module.
130
Records don’t have setters because every field is inherently final and cannot be modified after construction.
(✅ True)
131
Records are implicitly final, meaning they cannot be extended.
(✅ True)
132
A record can implement a regular or sealed interface if it implements all the abstract methods.
(✅ True)
133
Records promote immutability, making them inherently thread-safe and suitable for concurrent frameworks.
(✅ True)
134
If a record has a constructor with the same parameter list as the automatically generated one, the compiler does not insert its own constructor.
(✅ True)
135
Records allow fields to be modified after object creation.
(❌ False – Fields in records are final.)
136
Records must explicitly declare the final keyword to prevent extension.
(❌ False – They are implicitly final.)
137
Records can extend other records or classes like normal Java classes.
(❌ False – They cannot extend any class.)
138
Records cannot have constructors with validation logic.
(❌ False – They can include validation logic, as shown in the example.)
139
Records eliminate all boilerplate code, even when declaring custom constructors.
(❌ False – Declaring a constructor with many fields still introduces boilerplate code.)
140
True/False: A Java record automatically provides a canonical constructor if no constructor is explicitly declared.
True – If no constructor is defined, Java generates a default canonical constructor.
141
A compact constructor in a record must explicitly declare parameter names.
Compact constructors omit explicit parameter declarations; the parameters are inferred from the record components.
142
A record in Java can have multiple compact constructors.
False – A record can have only one compact constructor
143
A compact constructor in a record allows performing validation or transformation of input parameters before assigning values to fields.
True – Compact constructors enable adding logic like validation before record fields are assigned.
144
If a record has a compact constructor, the compiler still generates a default canonical constructor separately.
The compact constructor replaces the canonical constructor; the compiler does not generate an additional one.
145
The compact constructor in a record does not require explicit assignment of values to record fields.
True – Java automatically assigns values to record components in a compact constructor.
146
A record in Java cannot have additional instance fields beyond the components defined in its declaration.
True – Records only store the fields defined in the record header; no extra instance fields are allowed.
147
The canonical constructor in a record must always call super() explicitly.
False – The compiler automatically inserts super() where necessary, so it is not required to be explicitly called.
148
A record constructor can define extra fields not listed in the record header.
False – Records cannot define additional instance fields beyond those in the record header.
149
The primary purpose of a compact constructor in a record is to simplify code by allowing logic such as validation without explicitly listing parameters.
True – Compact constructors help keep code clean by allowing logic like validation without requiring explicit parameter declarations.
150
A record in Java can have overloaded constructors that take a completely different list of parameters.
151
A static nested class is a static type defined at the member level of a class.
152
Compact constructors in a record are declared without curly braces {} to distinguish them from normal constructors.
❌ (Compact constructors require curly braces like normal constructors.)
153
A compact constructor in a record can modify both constructor parameters and instance fields.
❌ (Compact constructors can modify parameters but not instance fields.)
154
Instance initializers are supported in records to allow additional initialization logic outside constructors.
❌ (Records do not support instance initializers; all initialization must be done in a constructor.)
155
A record can define additional instance fields outside the record declaration as long as they are private.
❌ (Records do not allow extra instance fields outside the declared components.)
156
Anonymous classes are a special case of nested classes that are always static.
❌ (Anonymous classes are always inner classes and cannot be static.)
157
Overloaded constructors in a record must always call the primary constructor using this().
❌ (Overloaded constructors do not have to call the primary constructor.)
158
A local class is defined at the member level of a class, similar to inner and static nested classes.
❌ (Local classes are defined within a method body, not at the member level.)
159
A nested class in Java can only be a static nested class or an inner class, with no other variations.
❌ (There are four types: inner class, static nested class, local class, and anonymous class.)
160
The Java compiler will execute the full constructor of a record after executing the compact constructor.
❌ (The compact constructor is the full constructor; there is no separate execution.)
161
What does the sealed keyword do in a Java class declaration?
A sealed class restricts which classes can directly extend or implement it. These permitted subclasses must be explicitly listed using the permits keyword, and each must be declared as final, sealed, or non-sealed
162
What does the non-sealed modifier mean when applied to a subclass of a sealed class?
A non-sealed subclass of a sealed class allows further extension by unspecified classes. This essentially reopens the inheritance hierarchy beyond the explicitly permitted list in the sealed class​
163
What must be true for all direct subclasses of a sealed class?
All direct subclasses of a sealed class must be in the same package or named module and must be explicitly marked as final, sealed, or non-sealed. If they are in the same file or are nested, the permits clause can be omitted
164
What is the main syntactic difference between a canonical and a compact constructor in Java records?
The canonical constructor explicitly declares parameters and assigns fields, while the compact constructor omits the parameter list and assigns fields implicitly.
165
When should you use a compact constructor in a Java record?
Use a compact constructor when the constructor parameters match the record components and you only need to add simple validation or transformation logic.
166
Which type of constructor allows you to rename parameters or use completely custom logic?
The canonical constructor — it provides full control over parameters, naming, assignments, and logic.
167
Can an abstract class be sealed in Java?
Yes. An abstract class can be declared sealed to restrict which classes are allowed to extend it.
168
What must all subclasses of a sealed abstract class declare?
They must be explicitly marked as either final, sealed, or non-sealed.
169
Why use a sealed abstract class instead of a regular one?
To control the inheritance hierarchy while still requiring subclasses to implement behavior, often enabling exhaustiveness in pattern matching and safer design.
170
When a class implements an interface with a default method, is it required to override it?
No, overriding is optional unless there’s a conflict. You can override to provide a custom implementation.
171
What happens if a class implements two interfaces with conflicting default methods (same signature)?
The class must override the method to resolve the conflict, using either one implementation or combining both.
172
If one interface extends another and overrides its default method, does the implementing class need to override it?
No, Java automatically picks the most specific implementation (the one from the child interface).