☕ Java MCQ Questions – Page 178
Questions 3541–3560 of 3994 total — Java interview practice
▶ Practice All Java QuestionsA `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`?
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");
}
}
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");
}
}
}
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);
}
}
Which statement is most accurate regarding `Thread.setPriority()` in Java?
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();
}
}
}
A `protected` member of a superclass is accessible in a subclass:
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"));
}
}
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);
}
}
}
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();
}
}
When defining a custom exception, what is a common practice for its constructors?
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);
}
}
}
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...");
}
}
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());
}
}
}
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");
}
}
}
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);
}
}
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()`?
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();
}
}
Consider a `TreeMap<Integer, String> map`. If `map.headMap(5)` is called, which keys will be included in the returned sub-map?
What does this code print?
java
public class CurrentThreadInfo {
public static void main(String[] args) {
System.out.println("Current thread: " + Thread.currentThread().getName());
}
}