☕ Java MCQ Questions – Page 178

Questions 3541–3560 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3541 hard
A `DelayQueue` is designed to hold elements that implement the `Delayed` interface. What is the primary characteristic governing when elements can be removed from a `DelayQueue`?
Q3542 easy code output
What is the output of this Java code?
java
interface SimplePrinter {
    void print(String message);
}

public class Main {
    public static void main(String[] args) {
        SimplePrinter printer = (msg) -> System.out.println("Hello: " + msg);
        printer.print("World");
    }
}
Q3543 medium code output
What is the output of this Java code?
java
public class MultipleCatchBlocks {
    public static void main(String[] args) {
        try {
            String s = null;
            System.out.println(s.length()); // NullPointerException
        } catch (ArithmeticException e) {
            System.out.println("Caught ArithmeticException");
        } catch (NullPointerException e) {
            System.out.println("Caught NullPointerException");
        } catch (Exception e) {
            System.out.println("Caught general Exception");
        }
    }
}
Q3544 medium code error
What is the error in this Java code snippet?
java
public class MyClass {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Test");
        String s = sb;
        System.out.println(s);
    }
}
Q3545 hard
Which statement is most accurate regarding `Thread.setPriority()` in Java?
Q3546 hard code error
What is the error encountered when running this Java code?
java
import java.io.FileReader;
import java.io.IOException;
import java.io.File;

public class FileReaderError3 {
    public static void main(String[] args) {
        File file = new File("test_fr3.txt");
        try {
            file.createNewFile();
            try (FileReader fr = new FileReader(file)) {
                char[] buffer = new char[10];
                fr.read(buffer, 5, 10); // buffer size 10, offset 5, length 10. 5+10 = 15 > 10
                System.out.println("Read successfully");
            }
        } catch (IOException e) {
            System.err.println(e.getClass().getSimpleName() + ": " + e.getMessage());
        } catch (IndexOutOfBoundsException e) {
            System.err.println(e.getClass().getSimpleName());
        } finally {
            file.delete();
        }
    }
}
Q3547 medium
A `protected` member of a superclass is accessible in a subclass:
Q3548 easy code output
What is the output of this code?
java
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> ages = new HashMap<>();
        ages.put("Alice", 30);
        ages.put("Bob", 25);
        System.out.println(ages.get("Alice"));
    }
}
Q3549 hard code error
What compilation error will occur when compiling this Java code?
java
public class AnonClassContinue {
    interface Task {
        void execute(int val);
    }
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            Task task = new Task() {
                @Override
                public void execute(int val) {
                    if (val == 1) {
                        continue;
                    }
                    System.out.println("Executing for: " + val);
                }
            };
            task.execute(i);
        }
    }
}
Q3550 hard code error
Which line causes a compilation error regarding method visibility?
java
class AccessParent {
    public void methodA() {
        System.out.println("Parent methodA");
    }
}
class AccessChild extends AccessParent {
    @Override
    private void methodA() { // Line 8
        System.out.println("Child methodA");
    }
}
public class Permissions {
    public static void main(String[] args) {
        AccessParent ap = new AccessChild();
        ap.methodA();
    }
}
Q3551 easy
When defining a custom exception, what is a common practice for its constructors?
Q3552 easy code error
What will happen when you try to compile this Java code?
java
public class Main {
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            if (i == 1) {
                continue innerLoop;
            }
            System.out.println(i);
        }
    }
}
Q3553 hard code error
What is the compilation error in the following Java code snippet?
java
import java.io.IOException;
import java.util.function.Consumer;

public class Main {
    public static void main(String[] args) {
        Consumer<String> fileWriter = s -> {
            // This lambda attempts to throw a checked exception
            throw new IOException("Failed to write: " + s);
        };
        System.out.println("Attempting to assign lambda...");
    }
}
Q3554 easy code error
What error will occur during deserialization if `data.ser` was created with `MyData` having `serialVersionUID = 1L`, but the current `MyData` class has `serialVersionUID = 2L`?
java
import java.io.*;

class MyData implements Serializable {
    private static final long serialVersionUID = 2L; // Current version ID
    String value;
    public MyData(String value) { this.value = value; }
}

public class Main {
    public static void main(String[] args) {
        // Assume "data.ser" was created by serializing a MyData object
        // where MyData had serialVersionUID = 1L
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("data.ser"))) {
            MyData data = (MyData) ois.readObject(); // Mismatch occurs here
            System.out.println(data.value);
        } catch (IOException | ClassNotFoundException e) {
            System.err.println(e.getClass().getSimpleName() + ": " + e.getMessage());
        }
    }
}
Q3555 hard code error
What error occurs when attempting to compile the following Java code?
java
public class OrphanElseIf {
    public static void main(String[] args) {
        int score = 85;
        if (score > 90) {
            System.out.println("Grade A");
        }
        // This 'else if' is orphaned as the preceding 'if' block is closed.
        else if (score > 80) { // 'else' without 'if'
            System.out.println("Grade B");
        } else {
            System.out.println("Grade C");
        }
    }
}
Q3556 easy code error
What error will be encountered when compiling the following Java snippet?
java
class OuterClass {
    private class InnerClass {
        String message = "Inner";
    }
}

public class Application {
    public static void main(String[] args) {
        OuterClass outer = new OuterClass();
        OuterClass.InnerClass inner = outer.new InnerClass();
        System.out.println(inner.message);
    }
}
Q3557 hard
When designing a producer-consumer system with a bounded buffer, why is it generally recommended to use `offer()` and `poll()` methods of a `BlockingQueue` rather than `add()` and `remove()`?
Q3558 easy code output
What is the output of this code?
java
class Parent {
    String message = "Hello from Parent";
}

class Child extends Parent {
    String message = "Hello from Child";
    void printMessage() {
        System.out.println(message);
    }
}

public class Main {
    public static void main(String[] args) {
        Child c = new Child();
        c.printMessage();
    }
}
Q3559 medium
Consider a `TreeMap<Integer, String> map`. If `map.headMap(5)` is called, which keys will be included in the returned sub-map?
Q3560 easy code output
What does this code print?
java
public class CurrentThreadInfo {
    public static void main(String[] args) {
        System.out.println("Current thread: " + Thread.currentThread().getName());
    }
}
← Prev 176177178179180 Next → Page 178 of 200 · 3994 questions