☕ Java MCQ Questions – Page 86

Questions 1701–1720 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1701 hard
Under what conditions can a lambda expression be serialized in Java?
Q1702 hard code output
What is the output of this code?
java
import java.io.PrintStream;
class MyCustomException extends Exception {
    public MyCustomException(String message) { super(message); }
    @Override
    public void printStackTrace(PrintStream s) {
        s.println("--- Custom Stack Trace Start ---");
        super.printStackTrace(s);
        s.println("--- Custom Stack Trace End ---");
    }
}
public class Main {
    public static void thrower() throws MyCustomException { throw new MyCustomException("Something went wrong!"); }
    public static void main(String[] args) {
        try { thrower(); }
        catch (MyCustomException e) { e.printStackTrace(System.out); }
    }
}
Q1703 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 Engine {
    String type = "V8";
}

class Car implements Serializable {
    private String model = "Ferrari";
    private Engine engine = new Engine(); // Engine is not Serializable
}

public class SerializationQuestion1 {
    public static void main(String[] args) throws IOException {
        Car myCar = new Car();
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("car.ser"))) {
            oos.writeObject(myCar);
        }
    }
}
Q1704 medium code error
Examine the Java code below. What kind of compilation error will it yield?
java
public class TernaryTypeMismatch {
    public static void main(String[] args) {
        String message = (10 > 5) ? "Greater" : 123;
        System.out.println(message);
    }
}
Q1705 medium
Which type of argument does the `BufferedWriter` constructor typically accept to wrap another stream or writer?
Q1706 medium
To insert characters into an existing `StringBuffer` at a specified index, which method should be used?
Q1707 easy
When `break` is executed inside a nested loop without any labels, which loop does it affect?
Q1708 easy
What is the recommended way to ensure a `BufferedReader` or `BufferedWriter` is closed after use, especially in the presence of exceptions?
Q1709 medium code output
What does this code print to the console?
java
public class StringBuilderTest {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Fast Code");
        sb.replace(0, 4, "Quick");
        System.out.println(sb);
    }
}
Q1710 easy code output
What is the output of this Java code snippet?
java
public class LoopTest {
    public static void main(String[] args) {
        int i = 0;
        do {
            System.out.print(i);
            i++;
        } while (i < 0);
    }
}
Q1711 hard
In Java, due to type erasure, what is the consequence of attempting to cast a `List<Object>` to a `List<String>` at runtime, assuming it's done via a raw type or an `Object` reference to circumvent compile-time checks?
Q1712 easy code output
What does this code print?
java
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        TreeSet<String> ts = new TreeSet<>();
        ts.add("hello");
        ts.add(null);
        System.out.println(ts);
    }
}
Q1713 hard code output
What is the output of this code?
java
public class UncaughtExceptionHandlerTest {
    public static void main(String[] args) throws InterruptedException {
        Thread.setDefaultUncaughtExceptionHandler((t, e) -> {
            System.out.println("Handled by default handler for " + t.getName() + ": " + e.getMessage());
        });

        Thread buggyThread = new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + " starting.");
            throw new NullPointerException("Intentional NPE");
        }, "BuggyWorker");

        buggyThread.start();
        buggyThread.join();
        System.out.println("Main thread finished.");
    }
}
Q1714 medium code output
What is the output of this code?
java
import java.util.HashMap;

public class Test {
    public static void main(String[] args) {
        HashMap<String, Integer> scores = new HashMap<>();
        scores.put("Alice", 90);
        scores.put("Bob", 85);
        scores.put("Charlie", 92);
        int totalScore = 0;
        for (int score : scores.values()) {
            totalScore += score;
        }
        System.out.println(totalScore);
    }
}
Q1715 easy
Which method is used to get the number of elements currently stored in an `ArrayList`?
Q1716 hard
Which of the following statements about multi-dimensional arrays in Java is true?
Q1717 hard code output
What is the output of this code?
java
public class SwitchTest {
    public static void main(String[] args) {
        int x = 1;
        String msg = "Initial";
        switch (x) {
            case 1:
                String temp = "Case 1";
                msg = temp;
                break;
            case 2:
                String temp = "Case 2"; // This line is problematic
                msg = temp;
                break;
            default:
                msg = "Default";
        }
        System.out.println(msg);
    }
}
Q1718 hard code output
What is the output of this code?
java
public class SideEffectWhile {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;
        while (i++ < 5) {
            sum += i;
        }
        System.out.println(sum + ", " + i);
    }
}
Q1719 easy
Which of the following is a key benefit of using encapsulation in Java?
Q1720 hard code output
What is the output of this code?
java
import java.io.IOException;

class Parent {
    public void method() throws IOException {
        System.out.println("Parent method");
    }
}

class Child extends Parent {
    @Override
    public void method() throws java.io.FileNotFoundException {
        System.out.println("Child method");
    }
}

class GrandChild extends Child {
    @Override
    public void method() {
        System.out.println("GrandChild method");
    }
}

public class Test {
    public static void main(String[] args) {
        Parent p = new GrandChild();
        try {
            p.method();
        } catch (IOException e) {
            System.out.println("Caught IOException");
        }
    }
}
← Prev 8485868788 Next → Page 86 of 200 · 3994 questions