public class LoopError {
public static void main(String[] args) {
for (int i = 0; i < 1; i++) {
System.out.println("Inside loop");
return;
}
System.out.println("After loop");
}
}
✅ Correct Answer: A) Compile-time error: unreachable statement
The 'return' statement inside the loop causes the 'main' method to exit after the first iteration. This makes the statement 'System.out.println("After loop");' unreachable, leading to a compile-time error.
Q1282medium
Which of the following best describes the concept of "abstraction" in object-oriented programming?
✅ Correct Answer: C) The process of hiding the implementation details and showing only the essential features of an object.
Abstraction focuses on showing only the necessary details to the user and hiding the complex implementation specifics. It helps in managing complexity by providing a simplified view of an object or system.
Q1283hardcode error
What error will this Java code produce when compiled and run?
java
public class PrimitiveWrapperArrayCast {
public static void main(String[] args) {
int[] primitiveArr = {1, 2, 3};
// Attempt a direct cast from an array of primitives to an array of wrappers
Integer[] wrapperArr = (Integer[]) primitiveArr;
System.out.println(wrapperArr[0]);
}
}
✅ Correct Answer: A) Compile-time error: "incompatible types: int[] cannot be converted to Integer[]"
Even though `int` can be autoboxed to `Integer`, an array of primitive `int` (`int[]`) is a distinct type and not directly assignable or castable to an array of `Integer` objects (`Integer[]`). This is a fundamental type incompatibility at the array level, leading to a compile-time error.
Q1284hard
If a `continue` statement is executed within a `try` block in a loop, what is the effect on any `finally` block associated with that `try` statement before the loop proceeds to its next iteration?
✅ Correct Answer: C) The `finally` block is guaranteed to execute before the loop proceeds to its next iteration.
Similar to `break`, a `finally` block is always executed when its corresponding `try` block is exited, even if the exit is caused by a `continue` statement. Thus, the `finally` block runs before the loop's next iteration begins.
Q1285easycode error
What is the outcome of attempting to compile this Java code?
java
public class Main {
public static void main(String[] args) {
outerLoop: for (int i = 0; i < 5; i++) {
if (i == 2) {
break nonexistentLabel;
}
}
}
}
✅ Correct Answer: A) Compilation fails with an error about an undefined label
A labeled 'break' statement must refer to an existing, enclosing labeled statement. 'nonexistentLabel' is not defined, leading to a compile-time error.
Q1286easycode error
What kind of error will occur when compiling this Java code?
java
public class Main {
public static void main(String[] args) {
String status = "active";
if (status) {
System.out.println("Status is active");
}
}
}
✅ Correct Answer: B) Compile-time Error: Incompatible types: String cannot be converted to boolean.
The 'if' condition must evaluate to a boolean value. A String cannot be directly used as a boolean condition in Java, leading to a compile-time type mismatch error.
Q1287easy
Can a functional interface contain `static` methods?
✅ Correct Answer: C) Yes, any number of static methods are allowed.
Functional interfaces can contain any number of static methods. These methods have implementations and do not count towards the single abstract method rule.
Q1288easycode output
What is the output of this code?
java
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<String> tasks = new LinkedList<>();
tasks.offer("TaskA");
tasks.offer("TaskB");
tasks.poll();
tasks.offer("TaskC");
System.out.println(tasks.size());
}
}
✅ Correct Answer: B) 2
Initially, 'TaskA' and 'TaskB' are added (size 2). `poll()` removes 'TaskA' (size becomes 1). Then 'TaskC' is added (size becomes 2). The final size is 2.
Q1289mediumcode error
What error will occur when compiling the following Java code?
java
public class RunnableStartError {
public static void main(String[] args) {
Runnable task = () -> {
System.out.println("Task running");
};
task.start(); // Runnable interface does not have a start() method
}
}
✅ Correct Answer: A) Compile-time error: cannot find symbol method start()
The `Runnable` interface defines a `run()` method, but not a `start()` method. The `start()` method is defined in the `Thread` class. Attempting to call `start()` on a `Runnable` object will result in a compile-time error.
Q1290hardcode error
When compiling this Java record-based code, what error will be encountered?
java
public record Product(String name, double price) {}
public class RecordModification {
public static void main(String[] args) {
Product laptop = new Product("Laptop", 1200.00);
laptop.price = 1150.00; // Attempt to reassign a record component
System.out.println(laptop.price());
}
}
✅ Correct Answer: B) A compile-time error: 'cannot assign a value to final variable price'.
Components of a Java record are implicitly `final`. Attempting to reassign a record component directly (e.g., `laptop.price = ...`) will result in a compile-time error because it's equivalent to modifying a final field.
Q1291mediumcode error
What exception will be thrown when running this Java code snippet?
java
import java.util.PriorityQueue;
import java.util.Queue;
public class QueueError {
public static void main(String[] args) {
Queue pq = new PriorityQueue(); // Raw type Queue
pq.add("String element");
pq.add(123); // Adding an Integer to a PriorityQueue that already contains a String
}
}
✅ Correct Answer: A) ClassCastException
Using a raw type `Queue` (or `PriorityQueue`) allows adding heterogeneous types. However, a `PriorityQueue` needs to compare elements. When adding the `Integer` after a `String`, the `PriorityQueue` will attempt to compare the `Integer` with the existing `String` element, which results in a `ClassCastException` because `Integer` cannot be cast to `String` (or vice-versa for comparison purposes by `String`'s default comparator logic if it were used).
Q1292mediumcode error
What is the most likely outcome of running the following Java code, specifically due to the deprecated `stop()` method?
java
class StopperThread extends Thread {
public void run() {
System.out.println("StopperThread running...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("StopperThread finished.");
}
}
public class Main {
public static void main(String[] args) {
StopperThread t = new StopperThread();
t.start();
t.stop(); // This line
System.out.println("Main thread exiting.");
}
}
✅ Correct Answer: C) A runtime error: java.lang.ThreadDeath, potentially unhandled.
The Thread.stop() method is deprecated due to its unsafe nature. When invoked, it forcibly stops a thread by throwing a ThreadDeath error, which typically remains uncaught and abruptly terminates the thread.
Q1293easycode error
What is the error in the following Java code?
java
public class Main {
public static void main(String[] args) {
Object obj = "Hello";
Integer num = (Integer) obj;
System.out.println(num);
}
}
✅ Correct Answer: A) Runtime error: java.lang.ClassCastException
Although Object can hold a String, attempting to cast that String object to an Integer at runtime will fail because a String is not an instance of Integer, resulting in a ClassCastException.
Q1294easycode error
What is the outcome of executing this Java code?
java
public class LoopError2 {
public static void main(String[] args) {
int counter = 0;
while (counter < 5) {
System.out.println("Counting...");
// Missing counter++
}
}
}
✅ Correct Answer: C) It will run indefinitely, continuously printing "Counting...".
The loop condition `counter < 5` will always be true because `counter` is never incremented inside the loop, leading to an infinite loop.
Q1295easycode output
What does this Java code print?
java
public class Main {
public static void main(String[] args) {
int age = 15;
String category = "";
if (age < 13) {
category = "Child";
} else if (age < 18) {
category = "Teen";
} else {
category = "Adult";
}
System.out.println(category);
}
}
✅ Correct Answer: B) Teen
The first condition `age < 13` (15 < 13) is false. The second condition `age < 18` (15 < 18) is true, so `category` becomes "Teen". This value is then printed.
Q1296mediumcode error
What is the problem with the `run()` method implementation in the following `Runnable` class?
java
import java.io.IOException;
public class FaultyTask implements Runnable {
@Override
public void run() throws IOException {
// Simulating an operation that might throw IOException
throw new IOException("File not found");
}
}
public class Main {
public static void main(String[] args) {
FaultyTask task = new FaultyTask();
}
}
✅ Correct Answer: A) Compilation error: run() in Runnable cannot throw a checked exception.
The `run()` method of the `Runnable` interface is defined without a `throws` clause for checked exceptions. Therefore, any class implementing `Runnable` cannot declare that its `run()` method throws a checked exception, unless it catches it internally. This will result in a compilation error.
Q1297hard
Consider the two ways to initialize an array:
1. `int[] arr1 = {1, 2, 3};`
2. `int[] arr2 = new int[]{1, 2, 3};`
What is a key difference in their usage contexts?
✅ Correct Answer: B) The first form can only be used when declaring and initializing the array simultaneously, while the second form can be used anywhere an array expression is expected (e.g., as a method argument).
The shorthand `{...}` syntax is restricted to declaration and initialization. The `new Type[]{...}` syntax, known as an anonymous array initializer, creates an array object that can be used as an expression, such as a method argument or assignment to an existing variable.
Q1298mediumcode output
What is the output of this code?
java
import java.util.TreeMap;
public class TreeMapExample {
public static void main(String[] args) {
TreeMap<Integer, String> treeMap = new TreeMap<>();
treeMap.put(3, "C");
treeMap.put(1, "A");
treeMap.put(2, "B");
System.out.println(treeMap);
}
}
✅ Correct Answer: A) {1=A, 2=B, 3=C}
TreeMap stores keys in natural ascending order by default. For Integer keys, this means 1, 2, 3. The `toString()` method prints entries in this sorted order.
Q1299hard
Consider nested `for` loops: `outerLoop: for (...) { innerLoop: for (...) { /* code */ } }`. What is the precise effect of executing `break outerLoop;` from within the innermost part of the `innerLoop`?
✅ Correct Answer: B) It immediately terminates both the `innerLoop` and the `outerLoop`, causing execution to resume after the entire labeled `outerLoop` block.
A labeled `break` statement exits the labeled statement entirely. In this case, `break outerLoop;` terminates both the inner and outer loops, transferring control to the statement immediately following the outer loop's block.
Q1300mediumcode error
Which compile-time error will occur in the `main` method attempting to create an instance of `Singleton`?
java
class Singleton {
private Singleton() {
// Private constructor to prevent direct instantiation
}
public static Singleton getInstance() {
return new Singleton();
}
}
public class Main {
public static void main(String[] args) {
Singleton s = new Singleton();
}
}
✅ Correct Answer: B) Compile-time error: 'Singleton() has private access in Singleton'.
The constructor `Singleton()` is declared as `private`, meaning it can only be accessed from within the `Singleton` class itself. Attempting to call it from an external class (like `Main`) results in a compile-time access error.