☕ Java MCQ Questions – Page 200

Questions 3981–3994 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3981 medium
How does `java.util.TreeMap` primarily maintain the order of its keys?
Q3982 medium
What is the primary purpose of a `Semaphore` in Java concurrency?
Q3983 hard
Given classes `Animal`, `Dog extends Animal`, and `Poodle extends Dog`. If an `Object obj` is known to be an instance of `Poodle`, and the following casts are attempted: 1. `(Animal) obj` 2. `(Dog) obj` 3. `(Poodle) obj` 4. `(Cat) obj` (where `Cat` is another direct subclass of `Animal`) Which of these casts will result in a `ClassCastException` at runtime?
Q3984 easy code output
What is the output of this Java code?
java
class MyThreadTask implements Runnable {
    private String message;

    public MyThreadTask(String msg) {
        this.message = msg;
    }

    public void run() {
        System.out.println(message + " from " + Thread.currentThread().getName());
    }
}

public class MultiThreadExample {
    public static void main(String[] args) {
        Thread t1 = new Thread(new MyThreadTask("Hello"));
        Thread t2 = new Thread(new MyThreadTask("World"));
        t1.start();
        t2.start();
    }
}
Q3985 medium code output
What does this code print?
java
import java.io.*;

class Config implements Serializable {
    public static String VERSION = "1.0";
    private String name;

    public Config(String name) {
        this.name = name;
    }
    public String getInfo() {
        return name + ":" + VERSION;
    }
}

public class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Config config1 = new Config("App1");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(config1); 
        oos.close();

        Config.VERSION = "2.0"; // Static field changed AFTER serialization

        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        Config deserializedConfig = (Config) ois.readObject();
        ois.close();
        System.out.println(deserializedConfig.getInfo());
    }
}
Q3986 hard
Consider a scenario where you have a functional interface `Processor` that has a method `process(T t)` which can throw a custom *checked* exception, `ProcessingFailureException`. You want to use this `Processor` within a Java Stream's `map` operation. What is the most robust and idiomatic pattern to handle `ProcessingFailureException` without cluttering the stream pipeline with `try-catch` blocks for each element or resorting to unchecked wrappers for every call?
Q3987 hard code output
What is the output of this code?
java
import java.io.*;

public class Test {
    public static void main(String[] args) throws IOException {
        String data = "12345";
        BufferedReader br = new BufferedReader(new StringReader(data));
        br.read(); // Read '1'
        if (br.ready()) {
            System.out.print("READY");
        } else {
            System.out.print("NOT_READY");
        }
        br.read(); // Read '2'
        System.out.print((char)br.read());
        br.close();
    }
}
Q3988 easy code output
What does this code print?
java
public class StringTest {
    public static void main(String[] args) {
        String s = "apple";
        System.out.println(s.toUpperCase());
    }
}
Q3989 hard
What is the fundamental difference in control flow impact between an unlabeled `break` statement and a `return` statement when both are encountered within an inner loop of a method?
Q3990 easy code error
What is the error in this code?
java
import java.util.TreeMap;

public class Test {
    public static void main(String[] args) {
        TreeMap<String, String> map = new TreeMap<>();
        map.put("present", "value");
        String result = map.get("absent");
        System.out.println(result.length());
    }
}
Q3991 medium code error
What error will occur when running this Java code snippet?
java
import java.util.HashMap;
import java.util.Map;

public class Test {
    @SuppressWarnings({"rawtypes", "unchecked"})
    public static void main(String[] args) {
        Map rawMap = new HashMap(); // Using raw type HashMap
        rawMap.put("number", 123);
        rawMap.put("text", "hello");

        Integer num = (Integer) rawMap.get("number");
        String value = (String) rawMap.get("number"); // Incorrect cast
        
        System.out.println(num);
    }
}
Q3992 easy code error
What will happen when this Java code is compiled?
java
public class MyClass {
    public static void main(String[] args) {
        1number = 10;
        System.out.println(1number);
    }
}
Q3993 hard
Suppose you use mutable objects as keys in a `TreeMap` and then modify a key's relevant field (used in `compareTo`) *after* it has been inserted into the map. What is the most likely consequence?
Q3994 medium
Which of the following statements regarding an interface in Java is INCORRECT?
← Prev 198199200 Page 200 of 200 · 3994 questions