☕ Java MCQ Questions – Page 185

Questions 3681–3700 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3681 hard code output
What is the output of this Java program?
java
public class WaitNoNotify {
    private static final Object monitor = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread waitingThread = new Thread(() -> {
            synchronized (monitor) {
                try {
                    System.out.println("Waiting thread: Waiting...");
                    monitor.wait(); // Will wait indefinitely unless interrupted or notified
                    System.out.println("Waiting thread: Woke up.");
                } catch (InterruptedException e) {
                    System.out.println("Waiting thread: Interrupted!");
                }
            }
        });
        waitingThread.start();
        Thread.sleep(200);
        System.out.println("Main thread: Waiting thread state: " + waitingThread.getState());
        System.out.println("Main thread: Exiting.");
    }
}
Q3682 medium code error
What compilation error will this Java code produce?
java
public class SwitchError {
    public static void main(String[] args) {
        double value = 1.0;
        switch (value) {
            case 1.0:
                System.out.println("One");
                break;
            default:
                System.out.println("Other");
        }
    }
}
Q3683 easy
Why can all standard Java collections (like `ArrayList`, `HashSet`) be iterated using an `Iterator`?
Q3684 medium code output
What does this code print to the console?
java
public class AutoboxingTest {
    void display(int i) {
        System.out.println("Primitive int: " + i);
    }
    void display(Integer i) {
        System.out.println("Wrapper Integer: " + i);
    }
    public static void main(String[] args) {
        AutoboxingTest abt = new AutoboxingTest();
        abt.display(50);
    }
}
Q3685 hard
Under what specific conditions does an explicit downcast `(SubType) superRef` *guarantee* a `ClassCastException` at runtime, assuming `superRef` is not `null`?
Q3686 medium code error
What error will occur when running this Java code snippet?
java
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
        Map<String, String> originalMap = new HashMap<>();
        originalMap.put("key1", "value1");
        originalMap.put("key2", "value2");

        Map<String, String> unmodifiableMap = Collections.unmodifiableMap(originalMap);
        
        System.out.println(unmodifiableMap.get("key1"));
        unmodifiableMap.put("key3", "value3"); // Attempt to modify
        System.out.println(unmodifiableMap.size());
    }
}
Q3687 hard code output
What is the output of this code?
java
public class ArrayCopyOverlap {
    public static void main(String[] args) {
        int[] source = {1, 2, 3, 4, 5};
        System.arraycopy(source, 0, source, 2, 3);
        for (int i : source) {
            System.out.print(i + " ");
        }
        System.out.println();
    }
}
Q3688 medium code output
What does this Java code print?
java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class MethodRefTest {
    public static void main(String[] args) {
        List<String> names = Arrays.asList("alice", "bob", "charlie");
        List<String> upperNames = names.stream()
                                     .map(String::toUpperCase)
                                     .collect(Collectors.toList());
        System.out.println(String.join(", ", upperNames));
    }
}
Q3689 hard
Which of the following is the most effective and idiomatic way to achieve a thread-safe, sorted set similar to `TreeSet` in concurrent Java applications?
Q3690 hard
Why do primitive specialized functional interfaces like `IntPredicate`, `LongFunction`, and `DoubleConsumer` exist in Java's `java.util.function` package, rather than relying solely on their generic counterparts (`Predicate<Integer>`, `Function<Long, T>`, `Consumer<Double>`)?
Q3691 easy code output
What is the output of this Java code snippet?
java
import java.io.File;

public class FileTest {
    public static void main(String[] args) {
        File file = new File("nonExistentFile.txt");
        System.out.println(file.exists() + " " + file.isFile());
    }
}
Q3692 hard code output
What is the output of this Java code?
java
import java.util.ArrayDeque;
import java.util.Deque;

public class QueueTest {
    public static void main(String[] args) {
        Deque<String> deque = new ArrayDeque<>();
        String result = "";
        try {
            deque.add("First");
            deque.add(null);
            deque.offer("Second");
            result += deque.poll();
        } catch (NullPointerException e) {
            result += "NPE caught. Queue size: " + deque.size();
        } catch (Exception e) {
            result += "Other exception: " + e.getClass().getSimpleName();
        }
        System.out.println(result);
    }
}
Q3693 easy
Can a Java `switch` statement have multiple `default` labels?
Q3694 medium code output
What is the output of this code?
java
import java.util.List;

public class ListOfTest {
    public static void main(String[] args) {
        List<String> immutableList = List.of("Red", "Green", "Blue");
        try {
            immutableList.add("Yellow");
        } catch (UnsupportedOperationException e) {
            System.out.println("Modification not allowed.");
        }
        System.out.println(immutableList.size());
    }
}
Q3695 medium code error
What is the compile-time or runtime error in the following Java code snippet when `main` method is executed?
java
import java.io.*;

class Outer implements Serializable {
    private String name = "Outer";
    // Inner class is not explicitly Serializable
    private Inner inner = new Inner(); 

    class Inner { // Non-static inner class
        String value = "InnerValue";
    }
}

public class SerializationQuestion3 {
    public static void main(String[] args) throws IOException {
        Outer outer = new Outer();
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("outer.ser"))) {
            oos.writeObject(outer);
        }
    }
}
Q3696 hard code output
What does this code print?
java
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class StreamReduceHard {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

        Optional<Integer> result = numbers.stream()
                                        .reduce((acc, x) -> acc + x + 1);

        System.out.println(result.orElse(-1));
    }
}
Q3697 easy code error
What compile-time error will occur in the `MyClass` definition?
java
class MyClass {
    static MyClass() {
        System.out.println("Static constructor");
    }
}
public class Main {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
    }
}
Q3698 easy code output
What does this code print?
java
import java.io.FileReader;
import java.io.IOException;

public class FileReaderTest {
    public static void main(String[] args) {
        // Assume test.txt exists and contains "OK"
        try (FileReader reader = new FileReader("test.txt")) {
            int c;
            while ((c = reader.read()) != -1) {
                System.out.print((char) c);
            }
        } catch (IOException e) {
            System.out.print("Error");
        }
    }
}
Q3699 hard code error
What is the result of running this Java code?
java
import java.util.TreeMap;
import java.util.NavigableMap;

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

        NavigableMap<Integer, String> navMap = map;
        System.out.println(navMap.floorEntry(null)); 
    }
}
Q3700 medium code error
Which compile-time error will prevent the `Shape` class from compiling?
java
class Shape {
    public abstract double calculateArea(); 
}

public class Main {
    public static void main(String[] args) {
        // Code won't reach here due to compile error
    }
}
← Prev 183184185186187 Next → Page 185 of 200 · 3994 questions