☕ Java MCQ Questions – Page 117

Questions 2321–2340 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2321 hard
When an exception is caught and then re-thrown wrapped within a new, different exception (i.e., exception chaining), how does the `finally` block interact with this process?
Q2322 medium
Regarding local variables accessed within a lambda expression, what is the key restriction they must adhere to?
Q2323 hard code error
What is the error in the following Java code?
java
class MyData {
    int value;
    public MyData(int value) { this.value = value; }

    @Override
    public boolean equals(MyData other) { // Wrong parameter type for equals()
        if (other == null || getClass() != other.getClass()) return false;
        return this.value == other.value;
    }
}
Q2324 hard
In a `switch` expression using pattern matching (Java 17+), what is the primary purpose of adding a `when` clause to a `case` label (e.g., `case Integer i when i > 0 -> ...`)?
Q2325 easy code output
What will be the output of this nested loop structure?
java
public class Main {
    public static void main(String[] args) {
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 3; j++) {
                if (j == 1) {
                    break;
                }
                System.out.print("(" + i + "," + j + ")");
            }
        }
    }
}
Q2326 hard code error
What is the error in the following Java code?
java
class SecretAgent {
    private void confidential() {
        System.out.println("Top Secret");
    }
}

class DoubleAgent extends SecretAgent {
    @Override
    public void confidential() { // Attempting to override private method
        System.out.println("Compromised Secret");
    }
}
Q2327 hard code output
What does this code print?
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));
        char[] buf = new char[10];
        int bytesRead = br.read(buf, 2, 3);
        System.out.println("Read: " + bytesRead);
        System.out.println("Buf: " + new String(buf, 0, 5));
        br.close();
    }
}
Q2328 easy
How would you define a lambda expression that takes no parameters and prints 'Hello World' to the console?
Q2329 medium
What is the primary purpose of the `java.io.FileWriter` class?
Q2330 easy code output
What does this code print?
java
class DataProcessingException extends Exception {
    public DataProcessingException(String message) {
        super(message);
    }
}

public class Main {
    public static void process(int value) throws DataProcessingException {
        if (value < 0) {
            throw new DataProcessingException("Negative value not allowed");
        }
        System.out.println("Data processed successfully: " + value);
    }

    public static void main(String[] args) {
        try {
            process(10);
        } catch (DataProcessingException e) {
            System.out.println("Caught exception: " + e.getMessage());
        }
    }
}
Q2331 hard
Consider an `Iterator<Integer> it = new ArrayList<>(Arrays.asList(1, 2, 3)).iterator();`. If, within the lambda expression passed to `it.forEachRemaining()`, a structural modification to the underlying `ArrayList` is attempted (e.g., `list.add(4)`), what is the expected outcome?
Q2332 medium code output
What does this Java code snippet print, or what exception does it throw?
java
import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("A");
        list.add("B");
        list.add("C");
        try {
            for (String s : list) {
                if (s.equals("B")) {
                    list.remove(s);
                }
            }
            System.out.println("No exception: " + list);
        } catch (Exception e) {
            System.out.println(e.getClass().getSimpleName());
        }
    }
}
Q2333 hard
What is the primary reason an `ArrayList`'s standard `Iterator` is considered 'fail-fast'?
Q2334 hard
Consider a scenario where a producer thread needs to hand off a single item to a consumer thread, blocking until the consumer is ready to receive it, and vice versa. Which `BlockingQueue` implementation is specifically designed for this synchronous hand-off without buffering any elements?
Q2335 hard
Given an `ArrayList<String> list = new ArrayList<>(Arrays.asList("A", "B", "C"));` and a `ListIterator<String> lit = list.listIterator();`. If the sequence of operations is `lit.next(); lit.add("D"); lit.next();`, what will be the value returned by `lit.previous()` immediately after this sequence?
Q2336 medium
What is the primary implication of declaring a class as `final`?
Q2337 hard code error
What error occurs when running this Java code?
java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class SubListCME {
    public static void main(String[] args) {
        List<String> mainList = new ArrayList<>(Arrays.asList("A", "B", "C", "D"));
        List<String> subList = mainList.subList(1, 3); // View of [B, C]
        
        Iterator<String> subListIterator = subList.iterator();
        
        mainList.add("E"); // Modifies mainList, impacting subList's expected size/structure
        
        while (subListIterator.hasNext()) {
            System.out.println(subListIterator.next()); // Error on next()
        }
    }
}
Q2338 hard code output
What is the output of this code?
java
public class VolatileSyncTest {
    private volatile int count = 0;
    private final Object lock = new Object();

    public void increment() {
        for (int i = 0; i < 1000; i++) {
            synchronized (lock) { // Synchronizing 'this' (the object instance)
                count++;
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        VolatileSyncTest test = new VolatileSyncTest();
        Thread t1 = new Thread(test::increment);
        Thread t2 = new Thread(test::increment);

        t1.start(); t2.start();
        t1.join(); t2.join();

        System.out.println(test.count);
    }
}
Q2339 medium code error
What kind of error will occur when compiling this Java code?
java
public class ArrayError {
    public static void main(String[] args) {
        double[] dArr = {1.0, 2.0};
        int[] iArr = dArr;
        System.out.println(iArr[0]);
    }
}
Q2340 easy code error
What kind of error will occur when compiling this Java code?
java
public class Main {
    public static void main(String[] args) {
        break;
    }
}
← Prev 115116117118119 Next → Page 117 of 200 · 3994 questions