☕ Java MCQ Questions – Page 90

Questions 1781–1800 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1781 easy code output
What is the output of this Java code?
java
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        TreeMap<Integer, String> treeMap = new TreeMap<>();
        try {
            treeMap.put(null, "Null Key");
            System.out.println("No exception");
        } catch (NullPointerException e) {
            System.out.println("NullPointerException caught");
        } catch (Exception e) {
            System.out.println("Other exception caught");
        }
    }
}
Q1782 medium code error
What error will this Java code produce when executed?
java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>(Arrays.asList("Alice", "Bob", "Charlie"));
        for (String name : names) {
            if (name.equals("Bob")) {
                names.remove(name); // Modifying collection directly during for-each iteration
            }
        }
    }
}
Q1783 hard code output
What does this code print before the program terminates due to an uncaught exception?
java
public class Test {
    public static void main(String[] args) {
        try {
            System.out.print("Try ");
            throw new IllegalArgumentException("First");
        } catch (IllegalArgumentException e) {
            System.out.print("Catch ");
            throw new IllegalStateException("Second");
        } finally {
            System.out.print("Finally ");
        }
        System.out.print("End");
    }
}
Q1784 medium
When a `StringBuffer` exceeds its current capacity, how does it typically manage to accommodate more characters?
Q1785 medium
Consider two overloaded methods: `void process(Object obj)` and `void process(String str)`. What happens if you call `process(null)`?
Q1786 easy
Which class must a custom unchecked exception in Java directly or indirectly extend?
Q1787 hard code output
What is the output of this Java code snippet?
java
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class DeleteOnExitBehavior {
    public static void main(String[] args) throws IOException {
        Path tempFile = Files.createTempFile("delete_on_exit", ".txt");
        File fileObj = tempFile.toFile();
        
        System.out.println("File exists before deleteOnExit(): " + fileObj.exists());
        fileObj.deleteOnExit();
        System.out.println("File exists immediately after deleteOnExit(): " + fileObj.exists());
        
        // Simulate some work, but the JVM is still running
        try { Thread.sleep(100); } catch (InterruptedException e) { /* ignore */ }
        
        System.out.println("File exists before JVM shutdown: " + fileObj.exists());
        
        // Cleanup (this won't run before deleteOnExit hook)
        // Files.delete(tempFile); // This would delete it directly
    }
}
Q1788 easy code output
What is the output of this code?
java
interface Multiplier {
    int multiply(int a, int b);
}

public class LambdaTest {
    public static void main(String[] args) {
        Multiplier myMultiplier = (a, b) -> {
            return a * b;
        };
        System.out.println(myMultiplier.multiply(6, 2));
    }
}
Q1789 hard
Consider a scenario with two nested `while` loops. Which statement allows for directly breaking out of the *outermost* `while` loop from within the innermost loop's body, without affecting any intermediate loops?
Q1790 hard code output
What is the output of this code?
java
public class Q10_InterruptAndState {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            System.out.println("T: Start. isInterrupted (before sleep): " + Thread.currentThread().isInterrupted());
            try {
                Thread.sleep(500);
                System.out.println("T: Sleep completed.");
            } catch (InterruptedException e) {
                System.out.println("T: Interrupted during sleep. isInterrupted (in catch): " + Thread.currentThread().isInterrupted());
            }
            System.out.println("T: End.");
        });
        
        t.start();
        t.interrupt(); // Interrupt immediately after start
        Thread.sleep(50); // Give time for t to react
        System.out.println("Main: T state mid-execution: " + t.getState());
        Thread.sleep(500); // Wait for t to fully finish
        System.out.println("Main: T state final: " + t.getState());
    }
}
Q1791 easy
Method overriding always occurs in the context of what relationship between classes?
Q1792 medium code output
What is the output of this Java code snippet?
java
public class StringTest {
    public static void main(String[] args) {
        String str = "apple,banana,orange";
        String[] fruits = str.split(",");
        System.out.println(fruits[1].toUpperCase());
    }
}
Q1793 medium
Consider a lambda expression `(x, y) -> x + y`. If this lambda is used in a context where `x` and `y` are inferred to be `int` primitives, what is the inferred return type of the expression?
Q1794 easy code error
What kind of error will occur when compiling this Java code?
java
import java.io.BufferedWriter;
// Missing import for FileWriter

public class Main {
    public static void main(String[] args) {
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"));
            bw.write("Hello");
            bw.close();
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }
    }
}
Q1795 easy code output
What is the output of this code?
java
import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        ArrayList<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");
        System.out.println(fruits.get(1));
    }
}
Q1796 hard code error
What is the output of this code?
java
public class Main {
    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            try {
                Thread.sleep(500); // Simulate work
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt(); // Restore interrupt status
            }
            System.out.println("Worker thread finished.");
        });
        t.start(); // Start the thread
        
        try {
            t.setDaemon(true); // Attempt to change daemon status after starting
        } catch (IllegalThreadStateException e) {
            System.out.println("Caught: " + e.getClass().getSimpleName());
        }
    }
}
Q1797 hard
Given a superclass method declared as `public List<Number> processData()`, which of the following return types would be valid for an overriding method in a subclass, demonstrating a covariant return type?
Q1798 medium
A running thread `T` can be interrupted by calling `T.interrupt()`. Which of the following is the most appropriate way for thread `T` to respond to this interruption?
Q1799 hard code error
What is the compile-time error in this Java code?
java
public class InvalidThrowsType {
    public void testMethod() throws String {
        System.out.println("Test method");
    }
    public static void main(String[] args) {
        // new InvalidThrowsType().testMethod();
    }
}
Q1800 easy
Can a `static` method be overridden in Java?
← Prev 8889909192 Next → Page 90 of 200 · 3994 questions