☕ Java MCQ Questions – Page 108

Questions 2141–2160 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2141 medium
What is the main benefit of providing a `Throwable cause` in a custom exception's constructor?
Q2142 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("A");
        list1.add(null);
        list1.add("B");
        list1.add(null);
        list1.add("C");
        LinkedList<String> list2 = new LinkedList<>();
        list2.add(null);
        list2.add("B");
        list2.add("D");
        list1.removeAll(list2);
        System.out.println(list1);
    }
}
Q2143 hard
You are developing a reactive microservice using `CompletableFuture` for asynchronous operations. A custom checked exception, `DataProcessingException`, can be thrown by a future. What is the most appropriate way to ensure `DataProcessingException` is propagated and handled correctly at the end of an asynchronous chain, rather than being wrapped in an `CompletionException` or `ExecutionException` without preserving its original type?
Q2144 easy code error
What will happen when this Java code is compiled?
java
public class MyClass {
    public static void main(String[] args) {
        double salary = 50000.75;
        int annualSalary = salary;
        System.out.println(annualSalary);
    }
}
Q2145 hard code output
What is the output of the following Java code?
java
class Super {
    public final void foo() { System.out.println("Super foo"); }
    public static void bar() { System.out.println("Super bar"); }
}
class Sub extends Super {
    // Cannot override foo() - it's final
    public static void bar() { System.out.println("Sub bar"); }
}
public class Main {
    public static void main(String[] args) {
        Super s = new Sub();
        s.foo();
        s.bar();
        Sub.bar();
    }
}
Q2146 hard code error
What is the output of this code?
java
public class Main {
    public static void main(String[] args) {
        Object lock = new Object();
        System.out.println("Attempting to wait...");
        try {
            lock.wait(); // Calling wait() without owning the monitor
        } catch (InterruptedException e) {
            System.out.println("Interrupted");
        }
        System.out.println("Done.");
    }
}
Q2147 easy
What is the requirement for the values used in `case` labels?
Q2148 medium code error
What will be the compilation error in this code?
java
import java.io.IOException;

class Reader {
    public void readData() throws IOException {
        System.out.println("Reading data...");
    }
}

class FileReader extends Reader {
    @Override
    public void readData() throws Exception {
        System.out.println("Reading file data...");
    }
}
Q2149 medium code error
What is the compile-time error in the following Java code?
java
class SuperClass {
    public SuperClass(int x) {
        System.out.println("SuperClass constructor: " + x);
    }
}

class SubClass extends SuperClass {
    public SubClass() {
        System.out.println("SubClass constructor");
    }
}

public class ConstructorError1 {
    public static void main(String[] args) {
        new SubClass();
    }
}
Q2150 easy
Which keyword is used to refer to the current object within an instance method or constructor of a class?
Q2151 medium code error
What compilation error will this Java code produce?
java
public class SwitchError {
    public static void main(String[] args) {
        char grade = 'A';
        if (grade == 'A' || grade == 'B') {
            int result = switch (grade) {
                case 'A' -> {
                    System.out.println("Excellent!");
                    yield 100;
                }
                case 'B' -> {
                    System.out.println("Good!");
                    break; // Incorrect usage within a switch expression
                }
                default -> 0;
            };
            System.out.println("Score: " + result);
        }
    }
}
Q2152 hard
Why would a developer choose `appendCodePoint(int codePoint)` over `append(char c)` or `append(String str)` when working with `StringBuffer`?
Q2153 medium
Under what condition does the Java compiler automatically provide a default constructor for a class?
Q2154 medium code error
What is the error in this Java code?
java
import java.util.ArrayList;
import java.util.List;

public class MyClass {
    public static ArrayList getMixedList() {
        ArrayList rawList = new ArrayList();
        rawList.add("Hello");
        rawList.add(123); // Integer
        return rawList;
    }

    public static void main(String[] args) {
        List<String> myStrings = getMixedList(); // Unchecked warning, not an error
        String first = myStrings.get(0);
        String second = myStrings.get(1); // Error occurs here
        System.out.println(first + second);
    }
}
Q2155 medium code error
What error will occur when compiling this Java code?
java
public class ConditionCheck {
    public static void main(String[] args) {
        String message;
        if (true) {
            String greeting = "Hello";
            message = greeting;
        }
        System.out.println(greeting);
    }
}
Q2156 medium code error
Which of the following describes the error in the given Java code?
java
public class Test {
    public static void main(String[] args) {
        myBlock: {
            for (int i = 0; i < 5; i++) {
                if (i == 2) {
                    continue myBlock;
                }
                System.out.print(i);
            }
        }
    }
}
Q2157 easy
Which of the following interfaces does `TreeMap` implement to guarantee its sorted nature?
Q2158 hard code output
What is the output of this code?
java
import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("A");
        list.add(null);
        list.add("B");
        list.add(null);
        list.add("C");

        boolean removed1 = list.remove(null);
        boolean removed2 = list.remove("B");
        boolean removed3 = list.remove(null);

        System.out.println(list.size() + ":" + removed1 + ":" + removed2 + ":" + removed3);
    }
}
Q2159 hard
Given `Number num = 10;`, which of the following explicit casts will compile successfully but throw a `ClassCastException` at runtime?
Q2160 hard
`java.util.concurrent.locks.StampedLock` introduces an "optimistic read" mode. What is the primary characteristic of this mode?
← Prev 106107108109110 Next → Page 108 of 200 · 3994 questions