What types of methods can an abstract class in Java contain?
✅ Correct Answer: A) Both abstract methods and concrete (non-abstract) methods.
An abstract class can have a mix of abstract methods (without implementation) and concrete methods (with full implementation), allowing for partial abstraction.
Q762hard
Which statement regarding explicit constructor invocation using `this()` and `super()` is *false*?
✅ Correct Answer: A) A constructor can have both an explicit `this()` call and an explicit `super()` call, as long as they are the first two statements.
A constructor can only contain one explicit constructor invocation (`this()` or `super()`), and it must always be the very first statement. They cannot coexist.
Q763easy
Is `ArrayList` synchronized (thread-safe) by default?
✅ Correct Answer: B) No, it is not thread-safe by default.
`ArrayList` is not synchronized, meaning multiple threads can access and modify it concurrently, which can lead to data inconsistency. For thread-safe operations, one can use `Collections.synchronizedList()` or `CopyOnWriteArrayList`.
Q764medium
Which of the following is the most modern and recommended way to ensure a `FileReader` is closed automatically, even if exceptions occur?
✅ Correct Answer: B) Using a `try-with-resources` statement.
The `try-with-resources` statement automatically closes any resources that implement `AutoCloseable` (which `FileReader` does) when the `try` block exits, regardless of whether it completes normally or abruptly.
Q765hard
Under what condition is an object's constructor NOT invoked during its creation process?
✅ Correct Answer: B) When an object is created using `Object.clone()`.
`Object.clone()` creates a shallow copy of an existing object without invoking any constructor of the cloned object's class. It bypasses the constructor chain entirely.
Q766hardcode error
What is the result of compiling and running the following Java code snippet?
java
public class StringError {
public static void main(String[] args) {
String value = "hello";
String formatted = String.format("The number is: %d", value);
System.out.println(formatted);
}
}
✅ Correct Answer: B) Throws IllegalFormatConversionException
The String.format() method expects arguments that match the specified format specifiers. '%d' is for integer types, but a String value is provided, which leads to an IllegalFormatConversionException at runtime.
Q767easy
How does a HashMap determine if two keys are equal?
✅ Correct Answer: B) By using the `equals()` method and ensuring their `hashCode()` are the same.
HashMap uses both the `hashCode()` and `equals()` methods of the key objects to determine equality and locate elements.
Q768mediumcode error
What will be printed to the console when the following code is executed, assuming 'nonexistent.txt' does not exist?
java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class TestClass {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader("nonexistent.txt"));
br.readLine();
br.close();
} catch (IOException e) {
System.err.println("Caught exception: " + e.getClass().getSimpleName());
}
}
}
✅ Correct Answer: C) Caught exception: FileNotFoundException
When `new FileReader("nonexistent.txt")` is called for a file that does not exist, it throws a `FileNotFoundException`, which is a subclass of `IOException`. The `catch` block will execute and print the simple name of the specific exception caught.
Q769mediumcode error
What compile-time error will occur in this Java code snippet?
java
public class LoopError {
public static void main(String[] args) {
int limit = 5;
for (int i = 0; i < limit; i++) {
System.out.println(j);
}
}
}
✅ Correct Answer: B) Compile-time error: cannot find symbol variable j.
The variable 'j' is used within the loop body but has not been declared or initialized anywhere in the scope, leading to a 'cannot find symbol' compile-time error.
Q770easycode 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> messages = new LinkedList<>();
messages.offer("Hello");
messages.offer("World");
messages.poll();
System.out.println(messages.isEmpty());
}
}
✅ Correct Answer: B) false
"Hello" and "World" are added. "Hello" is removed by `poll()`. The queue still contains "World", so `isEmpty()` returns `false`.
Q771medium
Which method is the most idiomatic and often most efficient way to check if a `String` contains a specific sequence of characters in Java?
✅ Correct Answer: B) `str.contains("sequence")`
The `contains()` method is specifically designed for this purpose, making the code readable and semantically clear. It's often optimized internally for simple substring checks, while `matches()` involves regular expression overhead.
Q772mediumcode output
What is the result when running the following Java code?
java
import java.io.BufferedWriter;
import java.io.StringWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
StringWriter sw = new StringWriter();
BufferedWriter bw = new BufferedWriter(sw);
bw.write("Initial content");
bw.close();
try {
bw.write("Attempting to write after close");
} catch (IOException e) {
System.out.print(e.getMessage());
}
}
}
✅ Correct Answer: A) Stream closed
Once a `BufferedWriter` is closed, any subsequent attempts to write to it will result in an `IOException`, typically with the message 'Stream closed'. The `StringWriter` will only contain the data written before the stream was closed.
Q773easycode error
What is the error in the following Java code?
java
import java.util.TreeSet;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
TreeSet<String> set = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
set.add("Apple");
set.add("Banana");
set.remove(123);
}
}
✅ Correct Answer: C) The code will compile but throw a ClassCastException at runtime.
The `remove(Object o)` method of `TreeSet` uses its internal `Comparator` (or natural ordering) to find and remove the element. In this case, it attempts to compare an `Integer` (123) with `String` elements using `String.CASE_INSENSITIVE_ORDER`, which results in a `ClassCastException` at runtime.
Q774hard
What happens if a `finally` block executes a `return` statement after an exception has been thrown in the `try` block and caught in a `catch` block, or is pending propagation?
✅ Correct Answer: B) The `return` statement in the `finally` block will override any pending exception or return value, causing the method to exit normally.
A `return` statement in a `finally` block takes precedence, discarding any pending exception or return value from the `try` or `catch` blocks, causing the method to exit normally.
Q775hard
Under which set of circumstances is a `finally` block NOT guaranteed to execute in Java?
✅ Correct Answer: B) If the JVM crashes or an infinite loop occurs in the `try` or `catch` block before `finally` is reached, or if `System.exit()` is called.
The `finally` block is almost always executed. Exceptions include JVM crashes, `System.exit()` calls that terminate the application immediately, or an infinite loop that prevents control from ever reaching the `finally` block.
Q776medium
`BufferedWriter` is an example of which common object-oriented design pattern?
✅ Correct Answer: C) Decorator pattern.
`BufferedWriter` wraps an existing `Writer` object and adds new functionality (buffering) without altering its structure. This is a classic example of the Decorator pattern, which attaches additional responsibilities to an object dynamically.
Q777easy
When you perform an operation that seems to modify a `String` object (e.g., `str.toUpperCase()`), what actually happens?
✅ Correct Answer: B) A new `String` object is created with the modified content.
Because `String` is immutable, any method that modifies its content (like `toUpperCase()`) will return a brand new `String` object containing the result, leaving the original unchanged.
Q778easy
What must be true about the method signature (name and parameters) for a method to correctly override a superclass method?
✅ Correct Answer: B) Both the name and the number and type of parameters must be exactly the same.
For correct method overriding, the overriding method must have the exact same name and the exact same number and type of parameters as the overridden superclass method.
Q779mediumcode error
What error will this Java code produce when executed?
java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> original = new ArrayList<>();
original.add("A");
List<String> unmodifiableList = Collections.unmodifiableList(original);
Iterator<String> it = unmodifiableList.iterator();
if (it.hasNext()) {
it.next();
it.remove(); // Attempt to remove from an unmodifiable list
}
}
}
✅ Correct Answer: B) java.lang.UnsupportedOperationException
An `UnsupportedOperationException` is thrown when an `Iterator` obtained from an unmodifiable collection attempts to perform a modifying operation like `remove()`.
Q780easycode error
What compilation error will occur when compiling this Java code?
java
// No import for java.io.BufferedReader;
public class MyClass {
public static void main(String[] args) {
BufferedReader br;
}
}
✅ Correct Answer: A) BufferedReader cannot be resolved to a type
Without `import java.io.BufferedReader;`, the Java compiler cannot find the `BufferedReader` class, leading to a 'cannot be resolved to a type' error.