public class OverloadDemo5 {
void printDetails(String s, int i) {
System.out.println("String: " + s + ", Int: " + i);
}
void printDetails(int i, String s) {
System.out.println("Int: " + i + ", String: " + s);
}
public static void main(String[] args) {
OverloadDemo5 obj = new OverloadDemo5();
obj.printDetails("Code", 2023);
}
}
✅ Correct Answer: A) String: Code, Int: 2023
Method overloading considers the number and type of parameters, as well as their order. The call `printDetails("Code", 2023)` matches the `printDetails(String s, int i)` signature exactly.
Q2282hard
Which statement accurately describes the relationship between Java Reflection API and encapsulation?
✅ Correct Answer: B) Reflection can bypass access modifiers (like `private`) to inspect and modify internal state, potentially breaking encapsulation.
The Reflection API, particularly methods like `setAccessible(true)`, allows programmatic access and modification of private fields and invocation of private methods, thereby circumventing the normal encapsulation rules enforced by access modifiers.
Q2283easycode output
What does this code print?
java
public class Main {
public static void main(String[] args) {
int x = 5;
int y = x++;
System.out.println("x=" + x + ", y=" + y);
}
}
✅ Correct Answer: C) x=6, y=5
The post-increment operator `x++` uses the current value of `x` (5) for the assignment to `y` first, then increments `x`. So, `y` becomes 5, and `x` becomes 6.
Q2284easycode error
What compile-time error will occur in the `Child` class?
java
class Parent {
private void secretMethod() {
System.out.println("Parent's secret");
}
}
class Child extends Parent {
@Override
public void secretMethod() {
System.out.println("Child's secret");
}
}
✅ Correct Answer: A) Error: Method does not override method from its superclass
Private methods are not inherited by subclasses, meaning they are not visible for overriding. The `@Override` annotation will correctly flag this as not overriding any superclass method.
Q2285easycode error
What error will this Java code produce during compilation?
java
public class ArrayError {
public static void main(String[] args) {
int[][] matrix;
System.out.println(matrix[0][0]);
}
}
✅ Correct Answer: A) Compilation Error: variable matrix might not have been initialized
The `matrix` variable is declared but never initialized with a `new` keyword or an array literal. Java's compiler will prevent usage of uninitialized local variables, resulting in a compile-time error.
✅ Correct Answer: A) Inside problematicMethod
Finally block executed.
Exception in thread "main" java.lang.IllegalArgumentException: Invalid call
The `problematicMethod` throws an `IllegalArgumentException`. Since there is no `catch` block for this exception, it propagates out of the `try` block. However, the `finally` block is always executed regardless of whether an exception occurred. After the `finally` block, the unhandled exception terminates the program.
Q2287easycode output
What is the output of this Java code?
java
public class LoopTest {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 3; i++) {
sum += i;
}
System.out.print(sum);
}
}
✅ Correct Answer: A) 6
The loop adds i to sum for i=1, 2, and 3. So, sum = 0 + 1 + 2 + 3 = 6.
Q2288hard
Given two completely unrelated classes, `ClassA` and `ClassB` (i.e., neither extends the other, and they do not implement a common interface apart from `Object`'s implied lineage), what happens when attempting `(ClassB) new ClassA()`?
✅ Correct Answer: C) A compile-time error occurs because the compiler can determine that the cast is impossible.
If two reference types are not related by inheritance (one is not a supertype of the other) and they do not implement a common interface, the compiler can statically determine that a direct cast between them is impossible and will issue a compile-time error.
Q2289mediumcode error
What will be the outcome when this code is executed?
java
public class LoopTest {
public static void main(String[] args) {
int divisor = 0;
int counter = 0;
while (counter < 5) {
System.out.println(10 / divisor);
counter++;
}
}
}
✅ Correct Answer: C) A java.lang.ArithmeticException: / by zero.
Attempting to divide any integer by zero in Java results in a `java.lang.ArithmeticException`. This occurs at runtime on the first iteration of the `while` loop.
Q2290hardcode error
What is the output of this code?
java
public class Main {
public static void main(String[] args) {
Thread t = null;
System.out.println("Before starting null thread.");
try {
t.start(); // NullPointerException
} catch (Exception e) {
System.out.println("Caught: " + e.getClass().getSimpleName());
}
System.out.println("After starting null thread.");
}
}
✅ Correct Answer: A) Before starting null thread.
Caught: NullPointerException
After starting null thread.
Calling any instance method (like `start()`) on a `null` object reference will result in a `NullPointerException`.
Q2291medium
Which of the following best describes the purpose and behavior of the `Thread.join()` method?
✅ Correct Answer: A) It makes the current thread wait until the thread on which `join()` is called terminates.
The `join()` method allows one thread to wait for the completion of another. When a thread calls `t.join()`, it will block until thread `t` has finished its execution.
Q2292easycode error
What compilation error will occur when compiling this Java code?
java
import java.io.BufferedReader;
import java.io.StringReader;
public class MyClass {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new StringReader("test data"));
String line = br.readLine();
}
}
✅ Correct Answer: A) Unhandled exception: java.io.IOException
The `readLine()` method of `BufferedReader` can throw an `IOException`, which is a checked exception. It must be caught or declared in the method signature.
Q2293easy
Which keyword is used to manually trigger a custom exception instance in Java?
✅ Correct Answer: C) throw
The `throw` keyword is used to explicitly raise an instance of an exception, including custom exceptions.
Q2294medium
For a thread currently in the WAITING state (e.g., due to `object.wait()`), what is typically required for it to potentially transition back to the RUNNABLE state?
✅ Correct Answer: B) Another thread to call `object.notify()` or `object.notifyAll()` and the waiting thread to re-acquire the monitor lock.
A thread in the WAITING state requires another thread to explicitly call `notify()` or `notifyAll()` on the same object to be woken up. After being notified, it must also successfully re-acquire the monitor lock to become RUNNABLE.
Q2295medium
What is the primary purpose of the `join()` method when called on a `Thread` instance (e.g., `myThread.join()`)?
✅ Correct Answer: C) To ensure that the calling thread waits until `myThread` finishes its execution.
The `join()` method causes the current thread to pause its execution until the thread on which `join()` was called has completed its execution or a specified timeout elapses.
Q2296easy
Which of the following is a valid constructor for `FileWriter`?
✅ Correct Answer: A) new FileWriter(java.io.File file, boolean append)
`FileWriter` has a constructor that accepts a `File` object and a boolean to specify append mode, allowing flexible file handling.
Q2297hardcode error
What compilation error will this Java code produce?
java
class Test {
public static void main(String[] args) {
do {
int counter = 0;
counter++;
} while (counter < 5);
System.out.println("Loop finished.");
}
}
✅ Correct Answer: B) Cannot find symbol: variable counter
The variable `counter` is declared within the `do` block, making it local to that block. It is out of scope when referenced in the `while` condition, leading to a "cannot find symbol" error.
Q2298hardcode 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 sentence = "Java (language) is great!";
System.out.println(sentence.replaceAll("(", "X"));
}
}
✅ Correct Answer: B) Throws PatternSyntaxException
The String.replaceAll() method takes a regular expression as its first argument. The '(' character is a special metacharacter in regex for group capturing. Using it unescaped causes a PatternSyntaxException because it's an unclosed group.
Q2299hard
In Java 8 and later, if a class implements two interfaces that both declare a default method with the same signature, what is the outcome?
✅ Correct Answer: C) The class must explicitly override the conflicting default method, providing its own implementation or explicitly calling one of the super interface's default methods.
Java resolves the 'diamond problem' with default methods by requiring the implementing class to explicitly provide an implementation for the conflicting method, thereby disambiguating which default method to use or providing a new one.
Q2300easycode error
What type of error will occur when attempting to compile and run the following Java code?
java
import java.util.Queue;
import java.util.LinkedList;
public class QueueError {
public static void main(String[] args) {
Queue<String> myQueue = new Queue<>();
myQueue.offer("Hello");
System.out.println(myQueue.poll());
}
}
✅ Correct Answer: A) Compile-time error
Queue is an interface and cannot be instantiated directly. An implementing class like LinkedList or ArrayDeque must be used.