What compilation error will occur when compiling this Java code?
java
public class LabeledBlockContinue {
public static void main(String[] args) {
outer: {
for (int i = 0; i < 2; i++) {
innerBlock: {
if (i == 1) {
continue innerBlock;
}
System.out.println("i: " + i);
}
}
}
}
}
✅ Correct Answer: A) `label innerBlock not a loop statement`
A labeled `continue` statement in Java can only target an enclosing loop statement. `innerBlock` is a simple labeled block, not a loop, making it an invalid target for `continue`.
Q3262easycode output
What is the output of this Java code?
java
import java.util.LinkedList;
public class MyClass {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
System.out.println(list);
}
}
✅ Correct Answer: A) [Apple, Banana, Cherry]
When a LinkedList is printed directly, its `toString()` method is called, which typically formats the output as elements enclosed in square brackets and separated by commas and spaces.
Q3263hardcode output
What is the output of this Java code?
java
import java.util.ArrayDeque;
import java.util.Deque;
public class QueueTest {
public static void main(String[] args) {
Deque<Integer> deque = new ArrayDeque<>();
deque.addFirst(1);
deque.addLast(2);
deque.addFirst(0);
deque.addLast(3);
System.out.println(deque.removeFirst());
System.out.println(deque.removeLast());
System.out.println(deque.peekFirst());
System.out.println(deque.size());
}
}
✅ Correct Answer: A) 0
3
1
2
`addFirst` and `removeFirst` operate on the head, while `addLast` and `removeLast` operate on the tail. `peekFirst` retrieves the head without removal. The sequence of operations leads to the printed output.
Q3264medium
When resolving overloaded method calls, if both primitive widening and autoboxing could apply to an argument, which one does the Java compiler prefer?
✅ Correct Answer: B) Primitive widening conversion.
The Java compiler prioritizes primitive widening conversions over autoboxing when resolving overloaded method calls to maintain backward compatibility and avoid unexpected behavior.
Q3265mediumcode error
What compilation error will this Java code produce? (Assume Java 17 environment)
java
public class SwitchError {
enum Color { RED, GREEN, BLUE }
public static void main(String[] args) {
Color c = Color.RED;
String colorName = switch (c) {
case RED -> "Red";
case GREEN -> "Green";
};
System.out.println(colorName);
}
}
✅ Correct Answer: B) Error: the switch expression does not cover all possible input values
When using a switch expression (Java 14+) with an enum, it must be exhaustive, meaning all enum constants must be covered by case labels, or a default case must be provided. Here, `BLUE` is not covered.
Q3266hardcode error
Does this Java code compile? If not, what is the compile-time error?
java
import java.io.IOException;
import java.sql.SQLException;
class Parent {
public void process() throws IOException {}
}
class Child extends Parent {
@Override
public void process() throws SQLException {}
}
public class Main {
public static void main(String[] args) {}
}
✅ Correct Answer: C) Error: 'process()' in 'Child' cannot override 'process()' in 'Parent'; attempting to throw a new checked exception not declared by the superclass.
An overridden method in a subclass cannot declare checked exceptions that are new and not part of the superclass method's `throws` clause. `SQLException` is a new checked exception not related to `IOException`.
Q3267easy
When `continue` is executed inside a nested loop without any labels, which loop does it affect?
✅ Correct Answer: A) It skips the current iteration of the innermost loop.
Without a label, `continue` always skips the current iteration of the most immediate enclosing loop.
Q3268easycode error
What is the compilation or runtime error in the following Java code?
java
public class Main {
public static void main(String[] args) {
String message = "hello";
String upperMessage = message.upperCase();
System.out.println(upperMessage);
}
}
✅ Correct Answer: C) Compilation error: cannot find symbol 'upperCase()'
Java's String class has a method named `toUpperCase()`, not `upperCase()`. Attempting to call a non-existent method results in a 'cannot find symbol' compilation error.
Q3269hardcode output
What is the output of this code?
java
class TestCtor {
String message;
TestCtor(String msg) throws IllegalArgumentException {
System.out.print("Ctor started: " + msg + ". ");
if (msg.equals("error")) { throw new IllegalArgumentException("Error in ctor"); }
this.message = msg;
System.out.print("Ctor finished: " + msg + ". ");
}
}
public class Main {
public static void main(String[] args) {
TestCtor t1 = null;
try { t1 = new TestCtor("success"); new TestCtor("error"); }
catch (IllegalArgumentException e) { System.out.print("Caught: " + e.getMessage() + ". "); }
finally { if (t1 != null) { System.out.print("t1.msg:" + t1.message + ". "); }}
System.out.print("End.");
}
}
✅ Correct Answer: A) Ctor started: success. Ctor finished: success. Ctor started: error. Caught: Error in ctor. t1.msg:success. End.
The first constructor call completes successfully. The second call throws an `IllegalArgumentException` after printing 'Ctor started: error.', preventing the 'Ctor finished: error.' print. The `catch` block executes, and the `finally` block always executes, accessing `t1` which was successfully initialized.
Q3270easy
Can a private method in a superclass be overridden in a subclass in Java?
✅ Correct Answer: B) No, private methods are not inherited
Private methods are not inherited by subclasses, meaning they are not visible outside their defining class and therefore cannot be overridden.
Q3271medium
What happens if you try to declare a `private abstract` method within an abstract class in Java?
✅ Correct Answer: B) A compile-time error will occur.
An abstract method must be overridden by its concrete subclasses. A `private` method cannot be overridden, making `private abstract` a contradictory and invalid combination, resulting in a compile-time error.
Q3272easycode output
What does this code print?
java
public class MyClass {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Java");
sb.append("World");
System.out.print(sb);
}
}
✅ Correct Answer: A) JavaWorld
The `append()` method adds the specified string to the end of the `StringBuffer` content. So 'World' is added after 'Java'.
Q3273hardcode error
What is the error in this Java code, specifically regarding the last statement?
java
public class UnreachableCodeLoop {
public static void main(String[] args) {
int count = 0;
while (true) {
System.out.println("Looping...");
count++;
if (count > 2) {
// break; // Intentionally missing break
}
}
System.out.println("Loop finished.");
}
}
✅ Correct Answer: A) A compile-time error: unreachable statement
The `while (true)` loop without a `break` statement makes the `System.out.println("Loop finished.")` line unreachable. Java's compiler detects this and produces a compile-time error.
Q3274hard
Which statement accurately describes an 'effectively immutable' object?
✅ Correct Answer: B) An object whose state cannot be observed to change after its creation, even if it's technically mutable (e.g., due to not being strictly immutable by design, but usage patterns ensure no changes).
An 'effectively immutable' object is one whose state does not change after construction, even if its class doesn't strictly adhere to all immutability rules. Its immutability is guaranteed by convention or usage, rather than strictly by design.
Q3275easycode output
What does this Java code print?
java
public class LoopTest {
public static void main(String[] args) {
int count = 1;
do {
System.out.println(count);
count++;
} while (count <= 3);
}
}
✅ Correct Answer: A) 1
2
3
The loop runs for count = 1, 2, and 3. When count becomes 4, the condition 'count <= 3' is false, and the loop terminates. Each print statement adds a new line.
Q3276mediumcode error
What compile-time error will occur in this Java code snippet?
java
public class LoopError {
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
if (i == 1) {
System.out.println("Skipping 1");
}
}
continue;
}
}
✅ Correct Answer: A) Compile-time error: 'continue' statement without loop or switch.
The `continue` statement is placed outside of any loop structure. It can only be used within a loop or switch statement, leading to a compile-time error.
Q3277easy
What is the primary advantage of using an 'enhanced for loop' (or 'for-each loop') in Java?
✅ Correct Answer: C) It simplifies iteration over arrays and collections, making the code more readable for simple traversals.
The enhanced for loop simplifies iterating through elements of arrays and collections, resulting in cleaner and more readable code when an index is not required.
Q3278easy
Which method must be called to transition a thread from the NEW state to the RUNNABLE state?
✅ Correct Answer: C) start()
The `start()` method is essential to begin thread execution; it registers the thread with the scheduler and calls its `run()` method implicitly. Directly calling `run()` executes it in the current thread.
Q3279mediumcode output
What does this code print?
java
import java.util.LinkedList;
public class Test {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("X");
list.add("Y");
System.out.println(list.isEmpty());
list.clear();
System.out.println(list.size());
}
}
✅ Correct Answer: C) false
0
Initially, the list contains elements, so `list.isEmpty()` returns `false`. After `list.clear()`, all elements are removed, making the list empty. Therefore, `list.size()` will be 0. The output is 'false' followed by '0' on a new line.
Q3280medium
Which of the following is generally considered a good practice when working with `Optional` in modern Java?
✅ Correct Answer: D) Chaining methods like `map`, `filter`, `orElse`, `orElseGet`, `orElseThrow` to process the value functionally, rather than relying on `isPresent()` and `get()` for conditional logic.
The power of `Optional` lies in its fluent API, allowing developers to express complex logic through method chaining (`map`, `filter`, `orElse`, etc.) without explicit `null` checks or `isPresent().get()` calls, which often negate its benefits.