☕ Java MCQ Questions – Page 148

Questions 2941–2960 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2941 medium
Under which specific circumstance will a `finally` block in a `try-catch-finally` construct *not* be executed?
Q2942 medium code error
What kind of error will occur when compiling this Java code?
java
public class ArrayError {
    public static void main(String[] args) {
        int[] myNumbers;
        myNumbers = {1, 2, 3};
        System.out.println(myNumbers[0]);
    }
}
Q2943 hard code output
What is the output of this code?
java
class BusinessLogicException extends Exception { public BusinessLogicException(String msg) { super(msg); } }
class ResourceCloseException extends RuntimeException { public ResourceCloseException(String msg) { super(msg); } }
class MyResource implements AutoCloseable {
    public void doWork() throws BusinessLogicException { System.out.println("Working"); throw new BusinessLogicException("Error in work"); }
    public void close() { System.out.println("Closing"); throw new ResourceCloseException("Failed to close"); }
}
public class Main {
    public static void main(String[] args) {
        try (MyResource res = new MyResource()) { res.doWork(); }
        catch (BusinessLogicException e) {
            System.out.println("Main: " + e.getMessage());
            for (Throwable t : e.getSuppressed()) { System.out.println("Suppressed: " + t.getMessage()); }
        }
    }
}
Q2944 medium code error
What type of error will occur when executing this Java code?
java
import java.util.*;
public class LoopTest {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>(Arrays.asList("A", "B", "C"));
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String element = iterator.next();
            if (element.equals("B")) {
                list.add("D"); // Modifying collection directly during iteration
            }
            System.out.println(element);
        }
    }
}
Q2945 medium
Why is it generally recommended to use immutable objects as keys in a `HashMap`?
Q2946 hard code error
What is the compilation error in the following Java code?
java
public class InitExample {
    public static void main(String[] args) {
        int value;
        boolean condition = false;
        if (condition) {
            value = 10;
        }
        System.out.println(value);
    }
}
Q2947 hard code output
What is the output of this code?
java
public class EffectivelyFinal {
    public static void main(String[] args) {
        int counter = 0;
        Runnable task = new Runnable() {
            @Override
            public void run() {
                System.out.println("Task counter: " + counter);
            }
        };
        counter = 5; // This line modifies 'counter'
        task.run();
    }
}
Q2948 easy code error
Which exception will be thrown when running the following Java code snippet?
java
import java.util.ArrayList;
import java.util.Arrays;

public class IteratorError {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>(Arrays.asList("A", "B", "C"));
        for (String s : list) { // Enhanced for loop uses an Iterator internally
            if (s.equals("B")) {
                list.remove(s); // Modifying list directly inside enhanced for loop
            }
        }
    }
}
Q2949 hard code output
What does this code print?
java
public class StringReplaceTest {
    public static void main(String[] args) {
        String text = "abracadabra";
        String result = text.replaceAll("(a|b)", "$1$1");
        System.out.println(result);
    }
}
Q2950 easy
Consider the following method signature: `public void processFile() throws IOException`. What does `throws IOException` signify?
Q2951 easy
When a subclass inherits from a superclass, which members of the superclass are generally NOT inherited?
Q2952 medium
What is the key difference in memory allocation between static variables and instance variables in Java?
Q2953 hard
In Java 14 and later, when using a `switch` *expression* with an `int` selector, what happens if not all possible `int` values are covered by `case` labels and there is no `default` branch?
Q2954 easy
Which method of the `Thread` class should be overridden to define the code that the thread will execute?
Q2955 easy code error
What compile-time error will occur in the `MyClass()` constructor?
java
class MyClass {
    int x;
    MyClass() {
        x = 10;
        this(20); // Not the first statement
    }
    MyClass(int x) {
        this.x = x;
    }
}
public class Main {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
    }
}
Q2956 easy
Which of the following correctly declares and initializes a 2D array `matrix` with specified values?
Q2957 medium
What is a fundamental difference between a `switch` *statement* and a `switch` *expression* (introduced in Java 14)?
Q2958 medium code output
What is the expected output of the following Java program?
java
public class LambdaThread {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            System.out.println("Thread is running");
        });
        thread.start();
        thread.join(); // Ensures the thread finishes before main exits
    }
}
Q2959 hard code error
What is the compile-time error in this Java code?
java
public class DirectRunnableInstantiation {
    public static void main(String[] args) {
        Runnable myTask = new Runnable(); // Line 3
        myTask.run();
    }
}
Q2960 easy code error
What will happen when this Java code is compiled and executed?
java
class MyClass {
    public void testMethod() {
        try {
            // No code here that throws IOException
        } catch (java.io.IOException e) {
            System.out.println("Caught an IO Exception");
        }
    }
}
← Prev 146147148149150 Next → Page 148 of 200 · 3994 questions