☕ Java MCQ Questions – Page 124

Questions 2461–2480 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2461 easy
What is one key capability that `ListIterator` offers but `Iterator` does not?
Q2462 medium
Which of the following represents the correct syntax for a lambda expression that takes no arguments and performs an action (returns nothing), for example, implementing `Runnable`?
Q2463 hard
In a design where multiple implementations share common state initialization logic and some non-abstract methods, but require distinct implementations for other methods, which Java construct is generally preferred for achieving this abstraction?
Q2464 medium
Which of the following operations is NOT directly supported by the standard `java.util.Iterator` interface?
Q2465 medium
If a custom exception `MyCheckedException` extends `java.lang.Exception`, what is the implication for a method `foo()` that might throw it?
Q2466 medium
What is a key advantage of implementing the `Externalizable` interface over `Serializable`?
Q2467 easy
What happens if a `break` statement is omitted from a `case` block in a Java `switch` statement?
Q2468 medium code error
What is the compilation error in the following Java record definition?
java
public record Person(String name, int age) {
    public void setName(String newName) {
        this.name = newName; // Attempt to modify a record component
    }
}
Q2469 medium
How can you initialize a `TreeMap` to store its keys in a custom, non-natural order (e.g., descending order for `Integer` keys)?
Q2470 easy
Which keyword is used to create a new object (instantiate a class) in Java?
Q2471 medium code error
What error occurs when compiling this code?
java
import java.io.IOException;

public class MyClass {
    public static void main(String[] args) {
        try {
            FileReader fr = new FileReader("mydata.txt"); // Missing import for FileReader
            int data = fr.read();
            System.out.println(data);
            fr.close();
        } catch (IOException e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
}
Q2472 easy
What does the `continue` keyword do when encountered inside a loop in Java?
Q2473 medium code output
What is the output of this code?
java
import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;

public class BufferedReaderTest {
    public static void main(String[] args) {
        String data = "Hello";
        BufferedReader br = new BufferedReader(new StringReader(data));
        try {
            System.out.println(br.readLine());
            br.close();
            System.out.println(br.readLine());
        } catch (IOException e) {
            System.out.println("Caught: " + e.getMessage());
        }
    }
}
Q2474 easy code error
What compilation error will occur in the following Java code?
java
abstract class LivingThing {
    public static abstract void breathe();
}
Q2475 medium
Which of the following is true regarding `String` objects in Java?
Q2476 hard code error
What does this code print?
java
import java.io.*;

public class BufferMarkTest {
    public static void main(String[] args) {
        String data = "Hello, World!";
        try (StringReader sr = new StringReader(data);
             BufferedReader br = new BufferedReader(sr)) {
            br.mark(5); // Mark at 5 characters
            for (int i = 0; i < 7; i++) { // Read more than 5 characters
                br.read();
            }
            br.reset(); // Should fail
            System.out.println((char)br.read());
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}
Q2477 medium
Which method should be used to retrieve, but not remove, the head of the queue, returning `null` if the queue is empty?
Q2478 medium code output
What does this Java program output?
java
import java.util.function.Supplier;

public class Test {
    public static void main(String[] args) {
        String greeting = "Hello";
        Supplier<String> lazyGreeting = () -> {
            System.out.println("Generating greeting...");
            return greeting + " World!";
        };

        System.out.println("Before get()");
        String message = lazyGreeting.get();
        System.out.println(message);
    }
}
Q2479 hard
How does the execution of a `finally` block interact with control flow statements like `break` or `continue` that might be present in the `try` or `catch` blocks?
Q2480 hard
When designing a custom collection class that implements the `java.lang.Iterable` interface, what is the most crucial consideration for the `iterator()` method's implementation to ensure robust and predictable behavior, especially in scenarios involving concurrent access or resource management?
← Prev 122123124125126 Next → Page 124 of 200 · 3994 questions