☕ Java MCQ Questions – Page 71

Questions 1401–1420 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1401 hard
A thread explicitly calls `java.util.concurrent.locks.LockSupport.park()`. Assuming no unparking event or interrupt has occurred, what is the immediate state of this thread?
Q1402 medium code error
What error occurs when compiling this Java code?
java
public class LoopTest {
    public static void main(String[] args) {
        int i = 0;
        do {
            System.out.println(i);
            i++;
        } while (i < 3) 
        System.out.println("Finished");
    }
}
Q1403 easy
What happens if an explicit cast from a `long` to an `int` results in a value larger than `int` can hold?
Q1404 medium
You have an `Optional<Integer>` and you only want to proceed with further operations if the integer value is greater than 10. Which `Optional` method should you use for this conditional check?
Q1405 hard
Relative to `HashMap` storing the same number of entries, `TreeMap` typically has:
Q1406 hard code output
What is the output of this code?
java
public class Q4_InterruptWait {
    private static final Object lock = new Object();
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            synchronized (lock) {
                try {
                    System.out.println("T: Waiting.");
                    lock.wait();
                } catch (InterruptedException e) {
                    System.out.println("T: Interrupted while waiting.");
                    Thread.currentThread().interrupt();
                }
                System.out.println("T: Resumed/Finished.");
            }
        });
        t.start();
        Thread.sleep(50);
        System.out.println("Main: T state (before interrupt): " + t.getState());
        t.interrupt();
        Thread.sleep(50);
        System.out.println("Main: T state (after interrupt): " + t.getState());
    }
}
Q1407 hard
When constructing a `BufferedReader` with an `InputStreamReader`, what is the most robust way to ensure correct character decoding from the underlying byte stream, especially when dealing with non-platform-default encodings?
Q1408 easy
What is the effect of calling the `delete()` method on a `File` object?
Q1409 medium
Which built-in functional interface is primarily used for evaluating a boolean condition on a given input?
Q1410 medium code error
What is the compile-time error in this Java code?
java
import java.util.function.Consumer;

public class LambdaError {
    public static void main(String[] args) {
        Object obj = (String s) -> System.out.println(s);
    }
}
Q1411 easy code output
What does this code print?
java
import java.util.HashSet;

public class Test {
    public static void main(String[] args) {
        HashSet<String> cities = new HashSet<>();
        cities.add("New York");
        cities.add("London");
        cities.clear();
        System.out.println(cities.isEmpty());
    }
}
Q1412 hard code output
What is the output of this code?
java
public class Q1_ThreadStateWaitNotify {
    private static final Object lock = new Object();
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            synchronized (lock) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        });
        t.start();
        Thread.sleep(50); // Give t time to enter WAITING
        System.out.println(t.getState()); // State 1
        synchronized (lock) {
            lock.notify();
        }
        Thread.sleep(50); // Give t time to resume and possibly terminate
        System.out.println(t.getState()); // State 2
    }
}
Q1413 easy code output
What does this code print?
java
public class DataTypeTest {
    public static void main(String[] args) {
        boolean isActive = true;
        System.out.println(isActive);
    }
}
Q1414 medium
If you have two 2D integer arrays, `int[][] arrayA = new int[2][2];` and `int[][] arrayB;`, and then execute `arrayB = arrayA;`, what is the relationship between `arrayA` and `arrayB`?
Q1415 hard
Consider a custom exception `ServiceFailureException` that needs to wrap various underlying `Throwable` causes while preserving the full stack trace of the original cause. Which of the following approaches is the most appropriate and idiomatic way to achieve this, especially when the cause is not known at the `ServiceFailureException` constructor call site?
Q1416 medium
Which of the following statements best describes the primary goal of abstraction in Java?
Q1417 medium code error
What type of error is caused by the generic method overloading in this Java code?
java
import java.util.ArrayList;
import java.util.List;

public class OverloadChecker {
    public void displayList(List<String> list) {
        System.out.println("List of Strings");
    }

    public void displayList(List<Integer> list) { // This line causes the error
        System.out.println("List of Integers");
    }

    public static void main(String[] args) {
        OverloadChecker oc = new OverloadChecker();
        oc.displayList(new ArrayList<String>());
    }
}
Q1418 medium code error
Which exception will be thrown when running the provided Java code?
java
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Writer;

public class Test {
    public static void main(String[] args) {
        Writer nullWriter = null;
        try (BufferedWriter writer = new BufferedWriter(nullWriter)) {
            writer.write("Hello");
        } catch (IOException e) {
            System.out.println("Caught IOException: " + e.getMessage());
        } catch (NullPointerException e) {
            System.out.println("Caught NullPointerException: " + e.getMessage());
        }
    }
}
Q1419 easy
Can a class be considered fully encapsulated if all its instance variables are declared `public`?
Q1420 hard code output
What is the output of this code?
java
import java.util.LinkedList;

public class Test {
    public static void main(String[] args) {
        LinkedList<String> list1 = new LinkedList<>();
        list1.add("X");
        list1.add("Y");
        list1.add(null);
        list1.add("X");
        list1.add("Z");

        LinkedList<String> list2 = new LinkedList<>();
        list2.add("X");
        list2.add(null);
        list2.add("A"); 

        list1.retainAll(list2);
        System.out.println(list1);
    }
}
← Prev 6970717273 Next → Page 71 of 200 · 3994 questions