What is the underlying data structure used by `java.util.TreeMap` to store its entries and maintain sorted order?
✅ Correct Answer: C) A Red-Black Tree
`TreeMap` uses a Red-Black Tree, which is a self-balancing binary search tree. This ensures efficient storage and retrieval of elements while maintaining the sorted order.
Q3162easycode output
What is the final output of the following code?
java
class Box {
int length;
public Box(int l) {
this.length = l;
}
}
public class Main {
public static void main(String[] args) {
Box box1 = new Box(10);
Box box2 = box1;
box2.length = 20;
System.out.print("Box1 Length: " + box1.length);
}
}
✅ Correct Answer: A) Box1 Length: 20
When `Box box2 = box1;` is executed, both `box1` and `box2` reference the *same* object in memory. Therefore, changing `box2.length` also changes the `length` property of the object referenced by `box1`.
Q3163mediumcode error
What is the error in this Java code snippet?
java
public class MyClass {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Bug");
sb.setCharAt(3, 's');
System.out.println(sb);
}
}
✅ Correct Answer: A) A runtime error: StringIndexOutOfBoundsException.
The StringBuilder 'sb' has a length of 3, so valid indices are 0 to 2. Attempting to set a character at index 3 (which is out of bounds) will result in a StringIndexOutOfBoundsException at runtime.
Q3164medium
How can a method declare that it might throw multiple distinct checked exceptions?
✅ Correct Answer: B) By separating the exception types with commas in a single `throws` clause.
A method can declare multiple distinct checked exceptions by listing them comma-separated after the `throws` keyword in its signature, for example: `void myMethod() throws IOException, SQLException`.
Q3165hard
What is the consequence if a *new* checked exception is thrown from within a `finally` block, especially if there was already a pending exception from the `try` or `catch` block?
✅ Correct Answer: B) The original pending exception will be discarded, and only the new checked exception from `finally` will propagate, requiring it to be declared or caught.
If a new exception is thrown from a `finally` block, it takes precedence over any previously pending exception (from `try` or `catch`), effectively discarding the original. A checked exception from `finally` must be handled or declared.
Q3166hard
Regarding thread safety, what is the default behavior of `BufferedWriter` instances when multiple threads attempt to write to the same instance concurrently?
✅ Correct Answer: B) `BufferedWriter` is not inherently thread-safe; concurrent access without external synchronization can lead to data corruption or inconsistent output.
`BufferedWriter` does not implement internal synchronization for its write operations. If multiple threads write to the same `BufferedWriter` instance concurrently without external synchronization, race conditions can occur, leading to corrupted or interleaved data.
Q3167mediumcode output
What does this code print?
java
import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;
public class BufferedReaderTest {
public static void main(String[] args) {
String data = "Line1\nLine2";
try (BufferedReader br = new BufferedReader(new StringReader(data))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line.length());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
✅ Correct Answer: A) 5
5
`readLine()` reads 'Line1' (length 5), then 'Line2' (length 5). After 'Line2', it reaches the end of the stream and returns `null`, terminating the loop.
Q3168hard
Which types of methods in Java inherently cannot participate in runtime polymorphism through overriding?
✅ Correct Answer: D) `private` methods, `static` methods, and constructors.
`private` methods are not inherited and cannot be overridden. `static` methods belong to the class and are hidden, not overridden. Constructors are special methods for object creation and are not subject to overriding.
Q3169easycode output
What does this Java code print?
java
public class LoopTest {
public static void main(String[] args) {
int countdown = 3;
String result = "";
do {
result += countdown;
countdown--;
} while (countdown > 1);
System.out.print(result);
}
}
✅ Correct Answer: A) 32
The loop runs for 'countdown' = 3 (result becomes '3', countdown becomes 2). Then it runs for 'countdown' = 2 (result becomes '32', countdown becomes 1). The condition 'countdown > 1' (1 > 1) is then false, terminating the loop. The final '32' is printed.
Q3170medium
What is the primary advantage of using the enhanced `for` loop (for-each loop) in Java compared to a traditional `for` loop?
✅ Correct Answer: B) It automatically handles index tracking, reducing boilerplate code for iteration
The enhanced `for` loop simplifies iteration over arrays and collections by handling index management internally, making the code cleaner and less prone to off-by-one errors.
Q3171easycode output
What does this code print?
java
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<Boolean> flags = new LinkedList<>();
flags.add(true);
flags.add(false);
flags.remove();
System.out.println(flags.remove());
}
}
✅ Correct Answer: B) false
True and False are added to the queue. The first `remove()` removes `true`. The second `remove()` then removes and returns `false`, which is printed.
Q3172easy
If `int[][] grid = new int[5][7];` is declared, what will `grid.length` return?
✅ Correct Answer: A) 5
`grid.length` returns the number of rows (the length of the outer array) in a 2D array.
Q3173mediumcode output
What is the output of this Java code snippet?
java
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
System.out.println(list.get(0) + list.get(1));
}
}
✅ Correct Answer: A) 30
The `get(index)` method retrieves the Integer objects at the specified indices. The `+` operator then performs integer addition due to autoboxing, resulting in 10 + 20 = 30.
Q3174easycode output
What does this Java code print?
java
import java.util.function.Function;
public class Main {
public String toUpper(String input) {
return input.toUpperCase();
}
public static void main(String[] args) {
Main obj = new Main();
Function<String, String> converter = obj::toUpper;
System.out.println(converter.apply("hello"));
}
}
✅ Correct Answer: A) HELLO
The method reference `obj::toUpper` refers to the instance method `toUpper` on the `obj` instance. Applying it to 'hello' converts it to uppercase, resulting in 'HELLO'.
Q3175medium
Which type of method reference is represented by the syntax `ClassName::staticMethodName`?
✅ Correct Answer: C) Static method reference
The syntax `ClassName::staticMethodName` is used to refer to a static method of a class. The method is invoked directly on the class itself.
Q3176hard
Regarding the `throws` clause, which of these is generally considered an anti-pattern or bad practice?
✅ Correct Answer: B) Declaring `throws Exception` for methods that might throw various specific checked exceptions.
Declaring `throws Exception` is often considered an anti-pattern because it forces callers to handle a broad, unspecified set of checked exceptions, hindering precise error handling and obscuring the method's actual exception contracts.
Q3177easy
Which class is primarily used to write a serialized object to an `OutputStream`?
✅ Correct Answer: B) java.io.ObjectOutputStream
`ObjectOutputStream` is the class that handles converting Java objects into a byte stream and writing them to an underlying `OutputStream`.
Q3178easycode output
What is the output of the following Java code?
java
public class WhileLoopDemo {
public static void main(String[] args) {
int i = 0;
while (i < 4) {
i++;
if (i == 2) {
continue;
}
System.out.print(i);
}
}
}
✅ Correct Answer: A) 134
The loop iterates, `i` is incremented first. When `i` becomes 2, the `continue` statement skips the `System.out.print(i)` for that iteration, immediately moving to the next loop cycle. So, 2 is not printed.
Q3179easycode error
What error will occur when compiling and running the following Java code?
java
public class Main {
public static void main(String[] args) {
int x = 10;
int y = 0;
int result = x / y;
System.out.println(result);
}
}
✅ Correct Answer: B) Runtime Error: `java.lang.ArithmeticException: / by zero`
Integer division by zero in Java results in a `java.lang.ArithmeticException` at runtime. Java does not allow division by zero for integer types.
Q3180medium
Which of the following statements about method references is true?
✅ Correct Answer: C) A method reference is essentially a compact form of a lambda expression and its execution is deferred until the functional interface's abstract method is invoked.
Method references, like lambda expressions, are not executed immediately upon declaration. They define a functional interface instance, and the referenced method is called only when the abstract method of that functional interface is invoked.