☕ Java MCQ Questions – Page 109
Questions 2161–2180 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhich of the following statements correctly describes how `PriorityQueue` handles elements when no explicit `Comparator` is provided?
What is the output of this Java code snippet using a `switch` expression with a `null` case (Java 17+)?
java
public class SwitchTest {
public static void main(String[] args) {
String input = null;
String output;
try {
output = switch (input) {
case "VALID" -> "Processed";
case null -> "Null Input";
default -> "Other";
};
} catch (NullPointerException e) {
output = "NPE Caught";
}
System.out.println(output);
}
}
What is the compile-time error in this Java code?
java
public class TaskWithCheckedException implements Runnable {
@Override
public void run() throws InterruptedException { // Line 3
Thread.sleep(100);
System.out.println("Task finished.");
}
public static void main(String[] args) {
new Thread(new TaskWithCheckedException()).start();
}
}
What is the error in the following Java code snippet?
java
public class ArrayError {
public static void main(String[] args) {
double[] prices = new double[5];
prices[0] = 10.5;
prices[1] = 20;
prices[2] = "thirty";
}
}
What is the output of this Java code?
java
class PriorityTask implements Runnable {
public void run() {
System.out.println("Child priority: " + Thread.currentThread().getPriority());
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(new PriorityTask(), "HighPri");
t.setPriority(Thread.MAX_PRIORITY);
t.start();
t.join();
System.out.println("Main priority: " + Thread.currentThread().getPriority());
}
}
What does this code print?
java
abstract class Logger {
final void logMessage(String message) {
System.out.println("Log: " + message);
}
abstract void logDetails();
}
class ConsoleLogger extends Logger {
@Override
void logDetails() {
System.out.println("Details logged to console.");
}
}
public class Test {
public static void main(String[] args) {
ConsoleLogger cl = new ConsoleLogger();
cl.logMessage("Application started");
}
}
When an `int` array is declared and initialized with a specific size (e.g., `new int[3]`) but without explicit values, what are the default values of its elements?
What will happen when this Java code is compiled?
java
public class MyClass {
public static void main(String[] args) {
boolean isValid = true;
int status = isValid;
System.out.println(status);
}
}
In an inheritance hierarchy, what is the correct order of execution for instance initialization blocks and constructors when a subclass object is instantiated?
What is the compilation error in the provided Java code?
java
abstract class Component {
abstract void render();
}
public class Button extends Component {
void render() {
System.out.println("Rendering Button");
}
public static abstract void click(); // Problematic method
}
What error will this Java code generate during compilation?
java
public class LoopTest {
public static void main(String[] args) {
boolean flag = false;
while (flag = "true") {
System.out.println("Inside loop");
}
}
}
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<Character, Integer> counts = new HashMap<>();
counts.put('A', 1);
counts.put('B', 2);
counts.put('C', 3);
counts.remove('B');
System.out.println(counts.size());
}
}
Which characteristic typically leads to an infinite `for` loop in Java?
What kind of error will occur when compiling this Java code?
java
abstract class Employee {
abstract void calculateSalary();
public void details() {
System.out.println("Employee details");
}
}
class Manager extends Employee {
// Manager class does not implement calculateSalary()
public void report() {
System.out.println("Manager report");
}
}
public class Main {
public static void main(String[] args) {
// ... (code would try to create Manager object if no compile error)
}
}
What does this code print?
java
import java.util.TreeSet;
public class App {
static class Item implements Comparable<Item> {
int id; String description;
public Item(int id, String description) { this.id = id; this.description = description; }
@Override public int compareTo(Item other) { return Integer.compare(this.id, other.id); } // Orders by ID
@Override public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Item)) return false;
Item item = (Item) obj;
return id == item.id && description.equals(item.description);
}
@Override public String toString() { return id + "-" + description; }
}
public static void main(String[] args) {
TreeSet<Item> items = new TreeSet<>();
items.add(new Item(1, "Pen"));
items.add(new Item(2, "Book"));
items.remove(new Item(1, "Pencil")); // Same ID, different description
System.out.println(items.size());
}
}
What is the output of this code?
java
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
System.out.println(names.contains("Bob"));
}
}
What error will occur when compiling the following Java code?
java
public class Main {
public static void main(String[] args) {
int value = 5;
(value + 10) = 20;
System.out.println(value);
}
}
When using the `throw` keyword, which of the following types of objects must be thrown?
What is the output of this code?
java
public class OperatorChallenge {
public static void main(String[] args) {
int x = -10;
System.out.println(x >> 2);
System.out.println(x >>> 2);
}
}
Which of the following is a key characteristic that distinguishes a `while` loop from a `do-while` loop?