import java.io.*;
public class Test {
public static void main(String[] args) throws IOException {
String data = "Line1";
BufferedReader br = new BufferedReader(new StringReader(data));
br.mark(10);
br.read(); // L
br.read(); // i
br.skip(2);
br.reset();
System.out.print(br.readLine());
br.close();
}
}
✅ Correct Answer: A) Line1
The `mark(10)` sets the mark position at the beginning of the stream ('L') with a read limit of 10 characters. After `read()` 'L', `read()` 'i', and `skip(2)` ('n', 'e'), the stream is at '1'. However, `reset()` restores the stream to the marked position (beginning of "Line1"). Therefore, the subsequent `readLine()` reads and returns "Line1".
Q482easy
What does the `=` operator do when used with variables in Java?
✅ Correct Answer: B) Assigns a value to a variable.
The single equals sign (`=`) is the assignment operator in Java, used to store the value on its right into the variable on its left. For comparison, `==` is used.
Q483hard
Consider the following Java code snippet: `double d1 = 0.1; double d2 = 0.2; double d3 = 0.3; System.out.println(d1 + d2 == d3);` What will be the output and why?
✅ Correct Answer: B) `false`, because `0.1` and `0.2` cannot be represented exactly in binary floating-point, leading to `d1 + d2` being slightly different from `0.3`.
Floating-point numbers like `0.1`, `0.2`, and `0.3` cannot be represented exactly in binary. Their sum `0.1 + 0.2` will have a slightly different binary representation than the direct binary representation of `0.3`, causing the direct equality comparison to return `false`.
Q484easy
What defines an immutable object in Java?
✅ Correct Answer: B) Its state cannot be changed after creation.
An immutable object is one whose state cannot be modified once it has been created. Any operation that appears to modify it actually returns a new object.
Q485easy
Is `FileReader` inherently buffered for efficient reading of large files?
✅ Correct Answer: C) No, it typically reads characters one by one or in small arrays directly.
`FileReader` itself is not buffered. For improved performance with large files, it should be wrapped in a `BufferedReader`.
Q486easy
What state is a thread in after its `run()` method has completed its execution?
✅ Correct Answer: C) TERMINATED
Once a thread's `run()` method finishes, either normally or due to an unhandled exception, the thread enters the TERMINATED state and can no longer be run.
Q487easycode error
What is the result of running this Java code?
java
public class StringError {
public static void main(String[] args) {
String text = "coding";
System.out.println(text.substring(0, 7));
}
}
✅ Correct Answer: C) java.lang.StringIndexOutOfBoundsException: String index out of range: 7
The substring(beginIndex, endIndex) method requires endIndex to be less than or equal to the length of the string. 'coding' has a length of 6, so an endIndex of 7 is out of bounds, causing a StringIndexOutOfBoundsException.
Q488mediumcode output
What is the output of the following Java program?
java
public class WideningAutoboxing {
void operate(long l) {
System.out.println("Long: " + l);
}
void operate(Integer i) {
System.out.println("Integer: " + i);
}
public static void main(String[] args) {
WideningAutoboxing wab = new WideningAutoboxing();
wab.operate(100);
}
}
✅ Correct Answer: A) Long: 100
Primitive widening (e.g., `int` to `long`) takes precedence over autoboxing (`int` to `Integer`) when the compiler resolves overloaded methods.
Q489hard
In the context of immutability, why might using the Builder pattern be particularly advantageous for constructing complex immutable objects?
✅ Correct Answer: B) It provides a clear, step-by-step process for constructing an object with many optional parameters, ensuring that the immutable object is fully initialized in a single atomic step upon calling `build()`.
The Builder pattern allows for the construction of complex immutable objects with numerous parameters in a clean and readable way, ensuring the object's validity before its final, immutable state is sealed by the `build()` method.
Q490mediumcode error
Which exception will be thrown when executing this Java code?
java
public class StringError3 {
public static void main(String[] args) {
String numStr = "123a";
int num = Integer.parseInt(numStr);
System.out.println(num);
}
}
✅ Correct Answer: C) java.lang.NumberFormatException
The Integer.parseInt() method expects a string containing only digits. '123a' is not a valid integer format, thus it throws a NumberFormatException.
Q491easy
Which method returns the number of characters in a String object?
✅ Correct Answer: B) length()
The `length()` method returns the total number of characters present in the String object.
Q492medium
Given an array `int[] data = {10, 20, 30};`, what would be the result of attempting to access `data[3]`?
✅ Correct Answer: B) Runtime error: ArrayIndexOutOfBoundsException
Array indices in Java are 0-based. For an array of length 3, valid indices are 0, 1, and 2. Accessing index 3 will result in an ArrayIndexOutOfBoundsException at runtime.
Q493hardcode error
What is the most likely error when executing this Java code snippet?
✅ Correct Answer: A) Runtime Error: java.lang.NullPointerException
The `BufferedWriter.append(CharSequence csq)` method does not handle `null` gracefully; it directly calls `csq.length()` which will throw a `NullPointerException` if `csq` is `null`.
Q494hard
Given the following Java class:
java
class Overload {
void perform(int value) { System.out.println("Fixed-arity"); }
void perform(int... values) { System.out.println("Varargs"); }
}
What will be printed when the following code is executed?
`new Overload().perform(100);`
✅ Correct Answer: A) Fixed-arity
A fixed-arity method is always preferred over a varargs method if the arguments exactly match the fixed-arity signature. The compiler will choose the `perform(int value)` method.
Q495easy
If the condition in a simple `if` statement (without an `else`) evaluates to `false`, what happens?
✅ Correct Answer: C) The code block immediately following the `if` statement is skipped.
If the `if` condition is false, the code block associated with that `if` statement is simply bypassed, and execution continues with the statement after the `if` block.
Q496medium
What is the primary advantage of using a `HashSet` over an `ArrayList` when you frequently need to check for the existence of an element?
✅ Correct Answer: C) `HashSet` provides faster average-case `contains()` operation (O(1) vs O(n)).
`HashSet` is optimized for fast lookups (average O(1) for `contains()`) due to its hash table implementation, whereas `ArrayList` requires iterating through elements (O(n) for `contains()`).
Q497easycode error
What kind of error will occur when compiling this Java code?
java
class Calculator {
public static void add(int a, int b) {
System.out.println(a + b);
}
}
class AdvancedCalculator extends Calculator {
public void add(int a, int b) {
System.out.println("Advanced Sum: " + (a + b));
}
}
public class Main {
public static void main(String[] args) {
AdvancedCalculator ac = new AdvancedCalculator();
ac.add(5, 3);
}
}
In Java, static methods cannot be overridden. If a subclass declares an instance method with the same signature as a static method in its superclass, it results in a compile-time error as a non-static method cannot override a static method.
Q498hard
Consider the nested `if` structure: `if (x > 0) { if (x > 5) { /* logic A */ } else { /* logic B */ } } else { /* logic C */ }`. If `logic A` is reached, what can be definitively said about `x`'s value?
✅ Correct Answer: B) `x` is greater than 5.
To reach `logic A`, both the outer condition `(x > 0)` and the inner condition `(x > 5)` must evaluate to `true`. If `x > 5` is true, it inherently satisfies `x > 0`, meaning `x` must be greater than 5.
Q499hardcode error
What is the compilation error in the following Java code?
java
public class ReportGenerator {
private class ReportData {
String content = "Secret Report";
}
public void generate() {
ReportData data = new ReportData(); // This is allowed
System.out.println(data.content);
}
}
public class TestReport {
public static void main(String[] args) {
ReportGenerator generator = new ReportGenerator();
// Attempt to instantiate a private inner class from outside
ReportGenerator.ReportData invalidData = generator.new ReportData();
}
}
✅ Correct Answer: B) Error: The constructor ReportGenerator.ReportData() is not visible.
The `ReportData` class is a `private` inner class of `ReportGenerator`. Its constructor is implicitly private to outside classes, even if it has a public enclosing instance. Therefore, it cannot be instantiated directly from `TestReport`, leading to a compile-time visibility error.
Q500mediumcode output
What does this code print?
java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class UnmodifiableListTest {
public static void main(String[] args) {
List<String> mutableList = new ArrayList<>();
mutableList.add("Apple");
List<String> unmodList = Collections.unmodifiableList(mutableList);
mutableList.add("Banana");
System.out.println(unmodList.size());
}
}
✅ Correct Answer: A) 2
`Collections.unmodifiableList()` returns a read-only *view* of the original list. While `unmodList` itself cannot be modified directly, modifications to the underlying `mutableList` will still be reflected in the `unmodList` view, thus its size changes.