☕ Java MCQ Questions – Page 107

Questions 2121–2140 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2121 hard code output
What does this code print?
java
public class DaemonThreadBehavior {
    public static void main(String[] args) throws InterruptedException {
        Thread daemonThread = new Thread(() -> {
            while (true) {
                try {
                    System.out.println("Daemon running...");
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    System.out.println("Daemon interrupted.");
                    break;
                }
            }
        });
        daemonThread.setDaemon(true);
        daemonThread.start();
        System.out.println("Main thread started daemon.");
        Thread.sleep(250);
        System.out.println("Main thread exiting.");
    }
}
Q2122 easy
Which method removes leading and trailing whitespace from a string?
Q2123 hard code error
What is the result of running this Java code?
java
import java.util.TreeMap;
import java.util.Map;

public class TreeMapError3 {
    public static void main(String[] args) {
        TreeMap<Integer, String> map = new TreeMap<>();
        map.put(10, "Ten");
        map.put(20, "Twenty");
        map.put(30, "Thirty");

        Map<Integer, String> sub = map.subMap(25, 15);
        System.out.println(sub.size());
    }
}
Q2124 medium code error
What is the compilation error in the provided Java code?
java
interface Drivable {
    void drive();
    void stop() { // This line is problematic
        System.out.println("Stopping");
    }
}

public class MyCar implements Drivable {
    public void drive() {
        System.out.println("Driving MyCar");
    }
}
Q2125 easy code output
What is the output of this code?
java
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileReaderTest {
    public static void main(String[] args) {
        // Assume nonexistent.txt does NOT exist
        try {
            FileReader reader = new FileReader("nonexistent.txt");
            System.out.print("File found");
            reader.close(); 
        } catch (FileNotFoundException e) {
            System.out.print("File not found!");
        } catch (IOException e) {
            System.out.print("Other IO Error!");
        }
    }
}
Q2126 medium
How can you provide a custom sorting order for elements in a `TreeSet` that is different from their natural ordering?
Q2127 easy code error
What error will occur when this code is compiled or executed?
java
public class Main {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Buffer");
        String str = sb;
        System.out.println(str);
    }
}
Q2128 medium
In a nested `try-catch-finally` structure, if an exception occurs in the inner `try` block and is successfully caught by its inner `catch` block, what is the execution sequence concerning the `finally` blocks?
Q2129 easy code error
What kind of error will occur when compiling or running the following Java code snippet?
java
import java.io.File;
import java.io.IOException;

public class FileError8 {
    public static void main(String[] args) throws IOException {
        File file = new File("invalid\0name.txt"); // \0 is the null character
        file.createNewFile();
        System.out.println("File created.");
    }
}
Q2130 medium
Why might a custom exception class need to implement `java.io.Serializable` and provide a `serialVersionUID`?
Q2131 easy code output
What is the output of this Java code?
java
public class AnonymousRunnable {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Anonymous thread running.");
            }
        }).start();
        System.out.println("Main method finished.");
    }
}
Q2132 easy
What is the primary characteristic of a Queue data structure?
Q2133 easy code error
What is the compilation error in the `Main` class?
java
class Person {
    String firstName = "John";
    String lastName = "Doe";
}

public class Main {
    public static void main(String[] args) {
        Person p = new Person();
        System.out.println(p.fullName);
    }
}
Q2134 hard code error
What is the result of executing this Java code snippet?
java
public class ChainedPrimitiveCast {
    public static void main(String[] args) {
        int val = 10;
        Number num = val; 
        Byte b = (Byte) num; 
        System.out.println(b);
    }
}
Q2135 hard
Why do `private` methods in a superclass not participate in polymorphism, even if a subclass defines a method with the exact same signature?
Q2136 hard code output
What is the output of this code?
java
public class Q6_BlockedState {
    private static final Object lock = new Object();
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            synchronized (lock) {
                System.out.println("T1 holding lock.");
                try {
                    Thread.sleep(100); // Hold lock
                } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
                System.out.println("T1 releasing lock.");
            }
        });
        Thread t2 = new Thread(() -> {
            System.out.println("T2 trying to acquire lock.");
            synchronized (lock) { // Will block here
                System.out.println("T2 acquired lock.");
            }
            System.out.println("T2 finished.");
        });
        t1.start();
        Thread.sleep(50); // Ensure t1 acquires lock
        t2.start();
        Thread.sleep(20); // Give t2 time to try and block
        System.out.println("T2 State: " + t2.getState());
        t1.join(); // Wait for t1 to finish and release lock
        Thread.sleep(20); // Give t2 time to acquire lock and finish
        System.out.println("T2 Final State: " + t2.getState());
    }
}
Q2137 medium code error
What is the error in this Java code snippet?
java
public class MyClass {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("example");
        sb.replace('e', 'X');
        System.out.println(sb);
    }
}
Q2138 medium
What is the primary effect of declaring a variable with the `final` keyword in Java?
Q2139 medium
Is `java.util.LinkedList` inherently thread-safe?
Q2140 hard code output
What does this code print?
java
import java.util.LinkedList;

public class Test {
    public static void main(String[] args) {
        LinkedList<Integer> dq = new LinkedList<>();
        dq.add(10);
        dq.add(20);
        System.out.print(dq.poll() + " ");
        System.out.print(dq.peekFirst() + " ");
        dq.addFirst(5);
        System.out.print(dq.pollLast() + " ");
        System.out.print(dq.pollLast() + " ");
        System.out.print(dq.pollLast());
    }
}
← Prev 105106107108109 Next → Page 107 of 200 · 3994 questions