☕ Java MCQ Questions – Page 113

Questions 2241–2260 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2241 medium
In an inheritance hierarchy, when is the constructor of a superclass invoked relative to the subclass constructor?
Q2242 hard
Consider a Java array declared as `String[] names;`. Which statement accurately describes `names`?
Q2243 easy
What is the primary purpose of the `java.io.File` class in Java?
Q2244 medium code error
What is the output of this Java code?
java
public class ExceptionFlow2 {
    public static void main(String[] args) {
        System.out.println(testMethod());
    }

    public static String testMethod() {
        try {
            System.out.println("In try");
            Integer.parseInt("abc"); // Throws NumberFormatException
            return "Success";
        } catch (NumberFormatException e) {
            System.out.println("In catch");
            return "Caught";
        } finally {
            System.out.println("In finally");
        }
    }
}
Q2245 easy code error
What is the result of compiling and executing this Java code?
java
public class FinalParameterReassignment {
    public void modify(final String param) {
        param = "new value";
        System.out.println(param);
    }
    public static void main(String[] args) {
        new FinalParameterReassignment().modify("original");
    }
}
Q2246 easy code error
What will be the result of attempting to compile and run this Java code?
java
public class MyClass {
    public static void main(String[] args) {
        String status = null;
        switch (status) {
            case "READY":
                System.out.println("Status is Ready");
                break;
            default:
                System.out.println("Unknown Status");
        }
    }
}
Q2247 easy code error
What compile-time error will occur when compiling this Java code?
java
public class MyClass {
    public static void main(String[] args) {
        long id = 100L;
        switch (id) {
            case 100L:
                System.out.println("ID is 100");
                break;
            default:
                System.out.println("Other ID");
        }
    }
}
Q2248 hard code error
What compilation error will this Java code produce?
java
class Test {
    public static void main(String[] args) {
        int j = 0;
        do {
            j++;
            if (j == 2) {
                continue OUTER_LOOP;
            }
        } while (j < 5);
        System.out.println("Final j: " + j);
    }
}
Q2249 medium code error
What error will this Java code produce?
java
public class ArrayError {
    public static void main(String[] args) {
        int[][] arr = new int[2][2] { {1,2}, {3,4} };
        System.out.println(arr[0][0]);
    }
}
Q2250 medium code output
What does this code print?
java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>(List.of("Alice", "Bob", "Charlie", "David"));
        Iterator<String> it = names.iterator();
        it.next(); // Advance once
        it.next(); // Advance twice
        StringBuilder sb = new StringBuilder();
        it.forEachRemaining(name -> sb.append(name).append(" "));
        System.out.println(sb.toString().trim());
    }
}
Q2251 easy
Can `TreeMap` store `null` values?
Q2252 easy code output
What is the output of the following Java program?
java
public class Main {
    public static void main(String[] args) {
        int count = 0;
        while (count < 5) {
            if (count == 3) {
                break;
            }
            System.out.print(count + "");
            count++;
        }
    }
}
Q2253 hard code error
What is the error encountered when compiling this Java code?
java
public class ArraySyntaxError {
    public static void main(String[] args) {
        int[] numbers;
        numbers = {1, 2, 3, 4, 5};
        System.out.println(numbers[0]);
    }
}
Q2254 easy code error
What is the error in the following Java code?
java
public class Main {
    public static void main(String[] args) {
        int x = 100;
        Integer y = (Integer) x;
        System.out.println(y);
    }
}
Q2255 easy code output
What is the output of this code?
java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>();
        names.add("Alice");
        names.add("Bob");
        Iterator<String> it = names.iterator();
        System.out.print(it.next());
        System.out.print("-" + it.next());
    }
}
Q2256 medium
Consider `ClassA` and `ClassB` where `ClassB extends ClassA`. If you have `ClassA obj = new ClassB();`, which statement is true?
Q2257 hard code error
What kind of runtime error or unexpected behavior will occur when running this Java code, primarily related to thread lifecycle?
java
public class InterruptionTest implements Runnable {
    public void run() {
        while (!Thread.currentThread().isInterrupted()) {
            try {
                Thread.sleep(100); // TIMED_WAITING state
                System.out.println("Working...");
            } catch (InterruptedException e) {
                // Swallowing the exception without handling or re-interrupting
                System.out.println("Interrupted, but continuing...");
            }
        }
        System.out.println("Thread finished.");
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(new InterruptionTest());
        t.start();
        Thread.sleep(200); // Give it time to work
        t.interrupt(); // Interrupt the thread
        Thread.sleep(300); // Give it time to react (or not)
    }
}
Q2258 hard
When an immutable class needs to be serializable, what special consideration must be taken regarding its `readObject` method (or `readResolve`) to preserve immutability during deserialization?
Q2259 medium
Which statement accurately describes the element order in a `HashSet`?
Q2260 medium
In which scenario is `orElseThrow()` the most suitable method to use with an `Optional`?
← Prev 111112113114115 Next → Page 113 of 200 · 3994 questions