If a superclass method declares `throws IOException`, which of the following is true for an overriding method in a subclass?
✅ Correct Answer: C) The overriding method can declare `throws FileNotFoundException` (a subclass of `IOException`).
An overriding method can declare fewer exceptions, the same exceptions, or subclasses of the exceptions declared by the superclass method, but not broader or new checked exceptions. `FileNotFoundException` is a subclass of `IOException`.
Q2062hard
A `Serializable` class has a field marked `transient`. If this class also implements `private void writeObject(ObjectOutputStream oos)` and `private void readObject(ObjectInputStream ois)`, how will the `transient` field be handled?
✅ Correct Answer: B) The `transient` keyword's effect is overridden; the field *must* be explicitly written in `writeObject()` and read in `readObject()` to be preserved.
When custom `writeObject` and `readObject` methods are provided, they take full control. The `transient` keyword only affects the default serialization mechanism; custom methods can explicitly write and read `transient` fields.
Q2063mediumcode error
What will be printed to the console when the following Java code is executed?
java
import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;
public class TestClass {
public static void main(String[] args) {
String data = "Hello World";
BufferedReader br = new BufferedReader(new StringReader(data));
try {
br.readLine();
br.close();
int charCode = br.read(); // Attempt to read after close
System.out.println((char)charCode);
} catch (IOException e) {
System.err.println("Runtime Error: " + e.getMessage());
}
}
}
✅ Correct Answer: A) Runtime Error: Stream closed
After `br.close()` is called, the `BufferedReader` is no longer active. Any subsequent attempts to perform read operations (like `br.read()` or `br.readLine()`) on the closed stream will throw an `IOException` with the message 'Stream closed'.
Q2064medium
When using `LinkedList` as a `Queue` in Java, which method would you typically use to add an element to the tail of the queue according to the `Queue` interface contract?
✅ Correct Answer: C) offer()
While `LinkedList` implements `Deque` and has `addLast()`, the standard `Queue` interface method for adding an element to the tail is `offer()` (or `add()`). `push()` and `addFirst()` add to the head, often used for stack-like behavior.
Q2065hard
A `TreeSet` of integers, `originalSet`, contains elements {10, 20, 30, 40, 50}. A `NavigableSet` view `subSet` is created using `originalSet.subSet(20, true, 40, false)`. What happens if an attempt is made to add the element `45` to `subSet`?
✅ Correct Answer: B) An `IllegalArgumentException` is thrown because `45` is outside the range defined by `subSet`.
`subSet`, `headSet`, and `tailSet` views are 'backed' by the original set. Attempts to add elements to these views that fall outside their specified range will result in an `IllegalArgumentException`.
Q2066easycode output
What is the output of this Java code?
java
import java.util.function.Function;
public class Main {
public static void main(String[] args) {
Function<Integer, Integer> square = x -> x * x;
System.out.println(square.apply(4));
}
}
✅ Correct Answer: A) 16
The Function `square` takes an Integer and returns its square. Applying it to 4 results in 4 * 4 = 16.
Q2067medium
When implementing its `get(int index)` method, how does `LinkedList` optimize traversal?
✅ Correct Answer: D) It checks if the index is closer to the head or the tail and iterates from the closer end.
To optimize the O(n) traversal for `get(int index)`, LinkedList determines if the target index is closer to the beginning or the end of the list. It then starts iterating from the closer end, which can halve the number of traversals on average.
Q2068medium
If a lambda expression's body contains multiple statements, how must it be enclosed?
✅ Correct Answer: C) It must be enclosed in curly braces `{}`.
For multi-statement lambda bodies, curly braces `{}` are required, similar to a regular method body. Single-statement bodies can omit them.
Q2069hard
Given the declaration:
java
int[][] jagged = { {1, 2}, {3}, {4, 5, 6} };
What is the value of `jagged[1].length`?
✅ Correct Answer: B) `1`
`jagged[1]` refers to the second inner array, which is `{3}`. The `.length` property of this specific inner array is `1`, as it contains one element.
Q2070easycode output
What does this code print?
java
class NoDataException extends Exception {
public NoDataException() {
super("No data found for the request.");
}
}
public class Main {
public static void processData() throws NoDataException {
boolean dataExists = false;
if (!dataExists) {
throw new NoDataException();
}
}
public static void main(String[] args) {
try {
processData();
} catch (NoDataException e) {
System.out.println(e.getMessage());
} finally {
System.out.println("Processing attempt complete.");
}
}
}
✅ Correct Answer: A) No data found for the request.
Processing attempt complete.
The `NoDataException` is thrown, caught, and its message is printed. The `finally` block always executes after the `try-catch` block, regardless of whether an exception occurred or was caught.
Q2071mediumcode error
What is the error in the following Java code?
java
class SensorData {
private int readings;
public SensorData(int initialReadings) {
this.readings = initialReadings;
}
public int getReadings() {
return readings;
}
public void resetReadings() {
if (true) {
readings = 0; // Legal modification within the class
}
}
}
public class DataProcessor {
public void processData(SensorData data) {
data.readings = -1; // Attempt to modify private field directly
}
public static void main(String[] args) {
SensorData sensor = new SensorData(5);
new DataProcessor().processData(sensor);
}
}
✅ Correct Answer: A) Compilation error: The field SensorData.readings is not visible.
The 'readings' field in the 'SensorData' class is private. Direct modification of private fields from another class (DataProcessor) is prohibited by Java's access control, leading to a compilation error. A public setter method would be required for external modification.
Q2072hard
What is the primary advantage of calling `intern()` on a `String` literal that has already been implicitly interned by the JVM?
✅ Correct Answer: C) It primarily serves to return the canonical representation, not to re-add to the pool.
A String literal (e.g., `"hello"`) is already interned by the JVM at compile time. Calling `intern()` on such a String simply returns the existing canonical reference from the String pool without any operational change to the pool itself.
Q2073mediumcode error
What is the compilation error in the following Java code?
java
public class MyClass {
public static void main(String[] args) {
int value;
System.out.println("The value is: " + value);
}
}
✅ Correct Answer: A) Variable 'value' might not have been initialized
Local variables in Java must be explicitly initialized before they are used. The compiler detects that 'value' might not have been assigned a value before being used in `println`.
Q2074easy
What is the primary characteristic that defines a 'functional interface' in Java?
✅ Correct Answer: B) It has only one abstract method.
A functional interface is defined as an interface that has exactly one abstract method. This is also known as a Single Abstract Method (SAM) interface.
Q2075medium
Under what circumstances does a Java thread transition into the TERMINATED state?
✅ Correct Answer: B) When its `run()` method completes execution or is terminated by an uncaught exception.
A thread reaches the TERMINATED state when its `run()` method finishes executing normally, or when its execution is abruptly stopped due to an uncaught exception.
Q2076hard
In a traditional Java `switch` *statement*, if the `default` case is placed somewhere in the middle of other `case` labels and no `break` statement is present, what happens when a value matching the `default` case is encountered?
✅ Correct Answer: C) Execution falls through to subsequent `case` labels or `default` if no `break` is encountered.
The placement of the `default` case does not affect fall-through behavior. If the `default` case is matched and no `break` statement is present, execution will continue to fall through to any subsequent `case` labels or `default` blocks that follow it.
Q2077easycode error
What compile-time error will occur in the `MyClass` definition?
java
abstract class MyClass {
abstract MyClass();
}
public class Main {
public static void main(String[] args) {
// No direct instantiation here, error is in class definition
}
}
✅ Correct Answer: A) modifier abstract not allowed here
Constructors cannot be declared `abstract`. The purpose of an abstract method is to be implemented by a subclass, whereas a constructor's purpose is to initialize an object of its own class.
Q2078easycode error
What compilation error will occur in the following Java code?
java
abstract class Document {
public abstract final void open();
}
✅ Correct Answer: C) Error: Illegal combination of modifiers: 'abstract' and 'final'
An abstract method must be implemented by concrete subclasses, while a final method cannot be overridden. These two modifiers are contradictory and thus cannot be used together.
Q2079medium
What methods are primarily used by `HashMap` to determine key equality and bucket placement for entries?
✅ Correct Answer: A) The `equals()` and `hashCode()` methods of the key objects.
HashMap uses `hashCode()` to determine the initial bucket for an entry and `equals()` to find the correct entry within a bucket (e.g., if multiple keys hash to the same bucket).
Q2080mediumcode error
What is the compilation error in the following Java code?
java
public class DuplicateVar {
public static void main(String[] args) {
int number = 5;
String number = "five";
System.out.println(number);
}
}
✅ Correct Answer: A) Variable 'number' is already defined in the scope
Within the same scope (e.g., a method block), you cannot declare two local variables with the exact same name, even if they have different data types.