What error occurs when executing the following Java code?
java
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> masterList = new ArrayList<>();
masterList.add("A");
masterList.add("B");
masterList.add("C");
masterList.add("D");
List<String> sub = masterList.subList(1, 3); // View of [B, C]
masterList.add("E"); // Modifies masterList
for (String s : sub) { // Iterate over subList after masterList modification
System.out.print(s + " ");
}
}
}
✅ Correct Answer: C) java.util.ConcurrentModificationException
The `subList` method returns a view of a portion of the original list. If the original list is structurally modified (e.g., by adding an element) after the subList is created, any subsequent operation on the `subList` (like iteration) will throw a `ConcurrentModificationException`.
Q782easy
Which operator is used to check if two values are NOT equal in Java?
✅ Correct Answer: B) !=
The inequality operator (!=) compares two operands and returns true if they are not equal, otherwise false.
Q783easycode output
What is the output of this Java code snippet?
java
public class ArrayTest {
public static void main(String[] args) {
double[] temperatures = {25.5, 28.0, 22.3};
System.out.println(temperatures[0] + temperatures[1]);
}
}
✅ Correct Answer: A) 53.5
The code accesses the first two elements (`temperatures[0]` is 25.5, `temperatures[1]` is 28.0) and adds them together, resulting in 53.5.
Q784medium
Which interface does `ArrayDeque` implement, allowing it to function efficiently as both a `Queue` and a `Stack`?
✅ Correct Answer: C) Deque
`ArrayDeque` implements the `Deque` interface, which extends `Queue`. This allows it to support operations at both ends, making it suitable for use as a queue (FIFO) or a stack (LIFO).
Q785hard
Can a `private` method in a superclass be overridden by a subclass?
✅ Correct Answer: B) No, `private` methods are not visible outside their declaring class and thus cannot be overridden; a method with the same signature in the subclass is an independent method.
`private` methods are not inherited by subclasses. Therefore, they cannot be overridden. If a subclass declares a method with the same signature as a `private` method in its superclass, it's considered a new, independent method specific to the subclass, not an override.
Q786easycode error
What is the outcome when compiling and running this Java code?
java
import java.util.List;
public class ImmutableListModification {
public static void main(String[] args) {
List<String> names = List.of("Alice", "Bob");
names.add("Charlie");
System.out.println(names);
}
}
✅ Correct Answer: A) Runtime error: java.lang.UnsupportedOperationException
`List.of()` creates an immutable (unmodifiable) list. Any attempt to add, remove, or modify elements in such a list will result in an `UnsupportedOperationException` at runtime.
Q787hardcode output
What is printed when the `main` method is executed?
java
class A {
static { System.out.println("Static A"); }
{ System.out.println("Instance A"); }
public A() { System.out.println("Constructor A"); }
}
class B extends A {
static { System.out.println("Static B"); }
{ System.out.println("Instance B"); }
public B() { System.out.println("Constructor B"); }
}
public class Main {
public static void main(String[] args) {
new B();
}
}
✅ Correct Answer: A) Static A
Static B
Instance A
Constructor A
Instance B
Constructor B
Static blocks execute first in declaration order across the hierarchy. Then, instance blocks and constructors execute, starting from the topmost superclass down to the subclass for each object creation.
Q788hardcode output
What is the output of this code?
java
public class Test {
public static void main(String[] args) {
int count = 0;
for (int i = 0; i < 5; i++) {
if (i % 2 == 0) { count++; continue; }
if (count > 2) { break; }
count += i;
}
System.out.print(count);
}
}
✅ Correct Answer: B) 3
i=0: count becomes 1. continue.
i=1: count (1) <= 2, so count becomes 1+1=2.
i=2: count becomes 3. continue.
i=3: count (3) > 2, so break. The loop terminates. Final count is 3.
Q789medium
What is the typical average time complexity for the `add()`, `remove()`, and `contains()` operations in a `HashSet`?
✅ Correct Answer: C) O(1)
On average, assuming a good hash function and proper distribution, `HashSet` provides constant-time performance (O(1)) for its basic operations like `add`, `remove`, and `contains`.
Q790easy
Are static fields of a class included in the default serialization process?
✅ Correct Answer: B) No, because static fields belong to the class, not to individual objects.
Static fields are part of the class definition, not the state of a specific object instance, and thus are not serialized by default.
Q791easycode output
What is the output of this code?
java
public class MyClass {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Hello");
sb.reverse();
System.out.print(sb);
}
}
✅ Correct Answer: A) olleH
The `reverse()` method reverses the sequence of characters in the `StringBuffer` object. 'Hello' becomes 'olleH'.
Q792medium
Consider a `while` loop that iterates based on a counter variable. Which action is most critical to prevent an infinite loop?
✅ Correct Answer: C) Modifying the counter variable's value within the loop body.
To prevent an infinite loop, the loop control variable (e.g., a counter) must be modified inside the loop body so that the loop's condition eventually becomes `false`.
Q793easycode output
What does this Java code print?
java
public class LoopTest {
public static void main(String[] args) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 1; j++) {
System.out.print(i + "" + j);
}
}
}
}
✅ Correct Answer: A) 0010
The outer loop runs for i=0 and i=1. The inner loop runs only once for j=0 for each i. Thus, it prints '00' then '10'.
Q794medium
After calling `thread.start()` on a newly created thread, what state does the thread immediately transition to?
✅ Correct Answer: B) RUNNABLE
Invoking `start()` on a thread moves it from the NEW state to the RUNNABLE state. It then becomes eligible to be run by the JVM scheduler.
Q795mediumcode error
What error does this Java code produce when executed?
java
public class Main {
public static void main(String[] args) {
String data = null;
System.out.println(data.toUpperCase());
}
}
✅ Correct Answer: A) java.lang.NullPointerException
Attempting to invoke the toUpperCase() method on a null String reference will result in a NullPointerException at runtime, as there is no object to call the method on.
Q796hard
Consider a scenario where *two different threads* independently iterate over the *same instance* of a standard `ArrayList`'s `Iterator` (e.g., `Iterator<T> it = list.iterator();`). Which statement accurately describes the potential issues or behavior?
✅ Correct Answer: C) The iteration order might become non-deterministic, and operations like `next()` might return elements already returned by the other thread or lead to `NoSuchElementException` prematurely, without `ConcurrentModificationException`.
Standard `Iterator` implementations are not thread-safe for concurrent access to the iterator instance itself. Multiple threads using the same iterator instance will contend for its internal state, leading to non-deterministic element retrieval or premature `NoSuchElementException`, distinct from `ConcurrentModificationException` which addresses external collection modification.
Q797hardcode error
What kind of error will occur when compiling or running the following Java code?
java
public class SBError {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("abcdefgh"); // Length 8
// Attempt to delete from index 5 to 3 (start > end)
sb.delete(5, 3);
System.out.println(sb);
}
}
✅ Correct Answer: C) java.lang.StringIndexOutOfBoundsException
The `delete(int start, int end)` method throws a `StringIndexOutOfBoundsException` if `start` is greater than `end`. In this snippet, `5 > 3`, leading to the exception.
Q798hardcode output
What is the output of this code?
java
class Parent {
String name = "Parent Name";
static String type = "Parent Type";
}
class Child extends Parent {
String name = "Child Name";
static String type = "Child Type";
}
public class Main {
public static void main(String[] args) {
Parent p = new Child();
Child c = new Child();
System.out.println(p.name + " " + p.type);
System.out.println(c.name + " " + c.type);
}
}
✅ Correct Answer: A) Parent Name Parent Type
Child Name Child Type
Instance variables are hidden, not overridden, and accessed based on the reference type. Static variables are also hidden, and accessed based on the reference type for instance access (e.g., `p.type`) or the class name for static access (e.g., `Child.type`).
Q799easycode error
What is the compilation error in the following Java code snippet?
java
public class LoopError {
public static void main(String[] args) {
do {
int x = 0;
System.out.println(x++);
} while (x < 5);
}
}
✅ Correct Answer: B) Variable `x` cannot be found in the `while` condition.
The variable `x` is declared within the `do` block, making it local to that block. It is out of scope and therefore not accessible when the `while` condition `(x < 5)` is evaluated, leading to a 'cannot find symbol' error.
Q800mediumcode error
What is the compile-time or runtime error in the following Java code snippet when `main` method is executed?
java
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
class Item implements Serializable {
String name = "Laptop";
}
public class SerializationQuestion4 {
public static void main(String[] args) throws IOException {
Item myItem = new Item();
// Trying to serialize to a non-existent directory without creating it
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("nonexistentdir/item.ser"))) {
oos.writeObject(myItem);
}
}
}
✅ Correct Answer: C) java.io.FileNotFoundException: nonexistentdir/item.ser (No such file or directory)
The `FileOutputStream` constructor attempts to create a file at the specified path. If the parent directory (`nonexistentdir`) does not exist, a `FileNotFoundException` will be thrown because the system cannot locate or create the specified path segment.