☕ Java MCQ Questions – Page 115

Questions 2281–2300 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2281 easy code output
What is the output of the following Java program?
java
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);
    }
}
Q2282 hard
Which statement accurately describes the relationship between Java Reflection API and encapsulation?
Q2283 easy code 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);
    }
}
Q2284 easy code 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");
    }
}
Q2285 easy code 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]);
    }
}
Q2286 medium code output
What does the following Java code print?
java
class NoCatchFinally {
    static void problematicMethod() throws IllegalArgumentException {
        System.out.println("Inside problematicMethod");
        throw new IllegalArgumentException("Invalid call");
    }

    public static void main(String[] args) {
        try {
            problematicMethod();
        } finally {
            System.out.println("Finally block executed.");
        }
        System.out.println("Program continues."); // Unreachable
    }
}
Q2287 easy code 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);
    }
}
Q2288 hard
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()`?
Q2289 medium code 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++;
        }
    }
}
Q2290 hard code 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.");
    }
}
Q2291 medium
Which of the following best describes the purpose and behavior of the `Thread.join()` method?
Q2292 easy code 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();
    }
}
Q2293 easy
Which keyword is used to manually trigger a custom exception instance in Java?
Q2294 medium
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?
Q2295 medium
What is the primary purpose of the `join()` method when called on a `Thread` instance (e.g., `myThread.join()`)?
Q2296 easy
Which of the following is a valid constructor for `FileWriter`?
Q2297 hard code 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.");
    }
}
Q2298 hard code 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"));
    }
}
Q2299 hard
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?
Q2300 easy code 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());
    }
}
← Prev 113114115116117 Next → Page 115 of 200 · 3994 questions