☕ Java MCQ Questions – Page 28
Questions 541–560 of 3994 total — Java interview practice
▶ Practice All Java QuestionsIf you want to create a custom exception that callers are *required* to handle or declare, which class should it extend?
What is the compilation issue in this Java code where method overloading is attempted using only different parameter names?
java
public class OverloadChecker {
public void combine(int num1, int num2) {
System.out.println("Combination 1: " + (num1 + num2));
}
public void combine(int x, int y) { // This line causes the error
System.out.println("Combination 2: " + (x * y));
}
public static void main(String[] args) {
OverloadChecker oc = new OverloadChecker();
oc.combine(5, 10);
}
}
What is the output of this code?
java
class MyMutableClass {
private String value;
public MyMutableClass(String value) {
this.value = value;
}
public String getValue() { return value; }
public void setValue(String value) { this.value = value; }
}
public class TestMutable {
public static void main(String[] args) {
MyMutableClass obj = new MyMutableClass("Initial");
MyMutableClass obj2 = obj;
obj.setValue("Modified");
System.out.println(obj2.getValue());
}
}
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> scores = new HashMap<>();
scores.put("John", 90);
scores.put("Mike", 85);
System.out.println(scores.get("Sarah"));
}
}
What is the output of this code?
java
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;
public class Test {
public static void main(String[] args) throws IOException {
StringWriter sw = new StringWriter();
BufferedWriter bw = new BufferedWriter(sw);
bw.append("Hello ").append("World").write("!");
bw.close();
System.out.println(sw.toString());
}
}
What compilation error will occur in the `ResourceManager` class?
java
public class ResourceNotFoundException extends Exception {
public ResourceNotFoundException(String message) {
super(message);
}
}
public class ResourceManager {
public String findResource(String id) throws ResourceNotFoundException {
if (id.equals("invalid")) {
throw new ResourceNotFoundException("Resource with ID " + id + " not found.");
}
return "Resource data for " + id;
}
public void processResource(String id) {
String data = findResource(id);
System.out.println("Processed: " + data);
}
public static void main(String[] args) {
ResourceManager manager = new ResourceManager();
manager.processResource("invalid");
}
}
What error will be thrown when the `main` method tries to submit the second task after calling `executor.shutdownNow()`?
java
import java.util.concurrent.*;
public class ExecutorShutdown {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.submit(() -> System.out.println("Task 1 executed"));
executor.shutdownNow(); // Immediately shuts down the executor
try {
// Attempt to submit a new task after shutdown
executor.submit(() -> System.out.println("Task 2 executed"));
} catch (RejectedExecutionException e) {
System.err.println("Caught: " + e.getClass().getName());
}
}
}
Which of the following statements accurately describes an intermediate operation in the Java Stream API?
In an inheritance relationship, what is the class that is being inherited from called?
Given the interface `Predicate<String>` (which has `boolean test(String s)`), consider the following methods:
java
class StringUtils {
public static boolean isPalindrome(String s) { return new StringBuilder(s).reverse().toString().equals(s); }
public boolean containsVowel(String s) { return s.toLowerCase().matches(".*[aeiou].*"); }
}
Which of the following method references correctly instantiates a `Predicate<String>`?
What error occurs when running this Java code?
java
public class MyClass {
public static void main(String[] args) {
StringBuffer sb = null;
sb.append("Hello");
System.out.println(sb);
}
}
Consider the following Java generic method signature: `public static void processList(List<? extends Number> list)`. Which types of lists can be passed to this method?
How can you create an effectively immutable `List` in Java 9+ that explicitly prevents any subsequent modifications?
`java.util.concurrent.TransferQueue` extends `BlockingQueue` and introduces a unique method `transfer(E e)`. What is the primary semantic difference of `transfer(E e)` compared to `put(E e)`?
What is the primary benefit of immutability in a multi-threaded environment beyond simply preventing data corruption (race conditions)?
What does this code print?
java
import java.util.LinkedList;
public class MyClass {
public static void main(String[] args) {
LinkedList<Integer> numbers = new LinkedList<>();
numbers.add(10);
numbers.addFirst(5);
numbers.addLast(20);
System.out.println(numbers);
}
}
What is the expected error when compiling and running the following Java code?
java
import java.util.LinkedList;
public class MyClass {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("One");
String item = list.get(1); // Accessing index 1
}
}
Which type of method reference can be used to refer to a static method of a class?
What is the output of this Java code?
java
import java.util.PriorityQueue;
import java.util.Comparator;
class MyInt {
int value;
MyInt(int v) { this.value = v; }
@Override public String toString() { return String.valueOf(value); }
}
public class QueueTest {
public static void main(String[] args) {
PriorityQueue<MyInt> pq = new PriorityQueue<>(
Comparator.comparingInt((MyInt mi) -> mi.value).reversed()
);
pq.offer(new MyInt(10));
pq.offer(new MyInt(5));
pq.offer(new MyInt(10));
pq.offer(new MyInt(20));
pq.offer(new MyInt(5));
String result = "";
while (!pq.isEmpty()) {
result += pq.poll().value + " ";
}
System.out.println(result.trim());
}
}
What is the output of the following Java program?
java
public class MultiArrayQ6 {
public static void main(String[] args) {
Object[][] objMatrix = new Object[2][2];
String[] sArray = {"Hello", "World"};
objMatrix[0] = sArray;
try {
objMatrix[0][0] = 123; // Incompatible type for underlying array
} catch (ArrayStoreException e) {
System.out.println("ArrayStoreException caught.");
}
System.out.println(objMatrix[0][1]);
}
}