What is the average time complexity for adding an element to the beginning or end of a `java.util.LinkedList`?
✅ Correct Answer: C) O(1)
Adding an element at either the beginning or end of a `LinkedList` involves only updating a few pointers, resulting in constant time complexity O(1).
Q242medium
In nested Java `for` loops, how does the inner loop complete its iterations relative to the outer loop?
✅ Correct Answer: A) The inner loop completes all its iterations for each single iteration of the outer loop
For every single iteration of the outer loop, the inner loop executes completely from start to finish before the outer loop proceeds to its next iteration.
Q243easy
What is the typical time complexity for basic operations like `get`, `put`, and `remove` in a `TreeMap`?
✅ Correct Answer: C) O(log n)
`TreeMap` is implemented as a Red-Black Tree, which guarantees `O(log n)` time complexity for basic operations like `get`, `put`, and `remove`.
Q244mediumcode output
What is the content of 'chars.txt' after this code executes?
The `write(char[] cbuf, int off, int len)` method writes `len` characters starting from index `off`. Here, it writes 3 characters ('len') starting from index 1 ('off') of the buffer, which are 'B', 'C', and 'D'.
Q245easy
What does 'data hiding' primarily refer to in the context of encapsulation?
✅ Correct Answer: B) Restricting direct access to an object's internal state from outside the class.
Data hiding means keeping the internal details of an object private, preventing external code from directly manipulating its state and forcing interaction through public methods.
Q246easy
If a static method is declared `synchronized`, which lock is acquired?
✅ Correct Answer: B) The intrinsic lock of the `Class` object representing the class.
When a static method is `synchronized`, the lock acquired is on the `Class` object itself, ensuring mutual exclusion for all synchronized static methods of that class.
Q247mediumcode error
What will be the outcome when executing this Java code snippet?
java
import java.util.concurrent.LinkedBlockingQueue;
public class QueueError {
public static void main(String[] args) throws InterruptedException {
LinkedBlockingQueue<String> lbq = new LinkedBlockingQueue<>();
System.out.println("Attempting to take element...");
String element = lbq.take(); // This method blocks if the queue is empty
System.out.println("Element taken: " + element);
}
}
✅ Correct Answer: A) The program will block indefinitely.
The `take()` method of `BlockingQueue` implementations (like `LinkedBlockingQueue`) is a blocking operation. If the queue is empty, the calling thread will wait indefinitely until an element becomes available or the thread is interrupted.
Q248hardcode error
What error occurs when attempting to compile the following Java code?
java
public class SemicolonAfterIf {
public static void main(String[] args) {
int value = 15;
if (value > 10); // Empty statement
System.out.println("Value is large.");
else { // 'else' without 'if'
System.out.println("Value is small.");
}
}
}
✅ Correct Answer: C) Compilation error: 'else' without 'if'
The semicolon after `if (value > 10)` creates an empty statement as the body of the `if`. This means the `System.out.println("Value is large.");` line is executed unconditionally, and the subsequent `else` block is left without an associated `if` statement, causing a compile-time error.
Q249mediumcode output
What is the output of this code?
java
import java.util.LinkedList;
public class Test {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("Hello");
list.add(null);
list.add("World");
list.remove(null);
System.out.println(list.size() + " " + list.contains(null));
}
}
✅ Correct Answer: B) 2 false
Initially, the list is ["Hello", null, "World"]. `list.remove(null)` removes the first occurrence of `null`. The list then becomes ["Hello", "World"]. So, `list.size()` is 2 and `list.contains(null)` is `false`.
Q250medium
Which statement best describes the purpose of the ClassLoader in Java?
✅ Correct Answer: C) To dynamically load Java classes into the JVM's memory as they are needed.
The ClassLoader is a core component of the JVM responsible for locating, loading, and linking `.class` files into the Java Virtual Machine. It loads classes on demand when they are first referenced.
Q251easy
What happens if the condition in a `while` loop is initially `false`?
✅ Correct Answer: C) The loop body is never executed.
Since the condition is checked before execution, if it's initially false, the loop body is skipped entirely.
Q252easycode error
What type of error will occur when compiling this Java code?
java
public class PrimitiveSync {
private int count = 0;
public void increment() {
synchronized (count) { // Attempting to synchronize on a primitive
count++;
System.out.println(count);
}
}
public static void main(String[] args) {
new PrimitiveSync().increment();
}
}
✅ Correct Answer: C) Compile-time error: incompatible types: int cannot be converted to Object
The `synchronized` block requires an object reference as its argument to acquire a lock. Primitive types like `int` cannot be used directly, leading to a compile-time error.
Q253easy
In which of the following contexts can the `continue` keyword be used in Java?
✅ Correct Answer: A) Only within loops (for, while, do-while).
The `continue` keyword is specifically designed to control the flow within loops, skipping the remainder of the current iteration.
Q254easycode output
What is the output of this Java code?
java
public class Main {
public static void main(String[] args) {
final int MAX_ATTEMPTS = 3;
System.out.println(MAX_ATTEMPTS);
}
}
✅ Correct Answer: A) 3
The `final` keyword makes `MAX_ATTEMPTS` a constant, preventing re-assignment. Its initial value of 3 is printed correctly.
Q255hardcode output
What is the output of this code?
java
public class StringSplitTest {
public static void main(String[] args) {
String data = "one,,two,three,";
String[] parts = data.split(",", 3);
System.out.println(parts.length);
System.out.println(parts[2]);
}
}
✅ Correct Answer: C) 3
two,three,
The limit parameter (3) specifies that the pattern will be applied at most 3-1=2 times, resulting in at most 3 parts. The last element will contain the remainder of the string. So parts are 'one', '', 'two,three,'. Its length is 3, and parts[2] is 'two,three,'.
Q256easycode error
What error will this Java code produce when executed?
java
public class ArrayError {
public static void main(String[] args) {
int[][] matrix = new int[1][2];
matrix[0][0] = 10;
matrix[0][1] = 20;
System.out.println(matrix[0][2]);
}
}
✅ Correct Answer: A) ArrayIndexOutOfBoundsException
The inner array `matrix[0]` has a size of 2 (indices 0 and 1). Accessing `matrix[0][2]` goes beyond its bounds, leading to an `ArrayIndexOutOfBoundsException` at runtime.
Q257medium
An abstract class in Java can have all of the following EXCEPT:
✅ Correct Answer: D) Objects (instances) of itself
An abstract class cannot be instantiated directly, meaning you cannot create an object of an abstract class. It can, however, contain concrete methods, constructors, and static methods.
Q258hard
Which statement about Java's `Integer` wrapper class and autoboxing behavior is *true*?
✅ Correct Answer: A) For `Integer` values between -128 and 127 (inclusive), two `Integer` objects created through autoboxing or `Integer.valueOf()` with the same value are guaranteed to be the same object (reference equality `==`).
`Integer.valueOf()` (used by autoboxing) caches `Integer` objects for values in the range -128 to 127 for performance. Therefore, within this range, identical values will often refer to the same object instance.
Q259mediumcode error
What is the primary issue with this Java code snippet when executed?
java
public class LoopError {
public static void main(String[] args) {
int count = 0;
for (int i = 0; i < 3;) {
System.out.println("Iteration: " + i);
// Missing increment for i
}
System.out.println("Done");
}
}
✅ Correct Answer: C) The program will run indefinitely, leading to an infinite loop.
The loop variable 'i' is never incremented, causing the condition 'i < 3' to always be true if 'i' starts at 0. This results in an infinite loop.
Q260hardcode error
What exception is thrown when the `ois.readObject()` line is executed during deserialization?
java
import java.io.*;
class ExternalizableClass implements Externalizable {
private String data;
private int id;
// Missing public no-arg constructor required by Externalizable
public ExternalizableClass(String data, int id) {
this.data = data;
this.id = id;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeUTF(data);
out.writeInt(id);
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
this.data = in.readUTF();
this.id = in.readInt();
}
}
public class SerializationError3 {
public static void main(String[] args) throws Exception {
ExternalizableClass original = new ExternalizableClass("Test Data", 123);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(original); // Serialization succeeds
byte[] serializedBytes = bos.toByteArray();
oos.close();
ByteArrayInputStream bis = new ByteArrayInputStream(serializedBytes);
ObjectInputStream ois = new ObjectInputStream(bis);
ExternalizableClass deserialized = (ExternalizableClass) ois.readObject(); // Deserialization fails here
ois.close();
System.out.println("Deserialized: " + deserialized.data + ", " + deserialized.id);
}
}
✅ Correct Answer: A) java.io.InvalidClassException: ExternalizableClass; no valid constructor
Classes implementing `Externalizable` must provide a public no-argument constructor for the deserialization mechanism to create a new instance before calling `readExternal`. Without this constructor, an `InvalidClassException` is thrown.