☕ Java MCQ Questions – Page 102
Questions 2021–2040 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat is the compilation error, if any, for the provided Java code snippet?
java
class Overloader {
public void handle(String s) {}
public void handle(Integer i) {}
public static void main(String[] args) {
Overloader o = new Overloader();
o.handle(null); // Line X
}
}
Which method is commonly used to write a `String` to a file using `FileWriter`?
What does this code print?
java
import java.util.function.Function;
public class FunctionComposition {
public static void main(String[] args) {
Function<Integer, Integer> multiplyByTwo = x -> {
System.out.println("Multiply by 2: " + x);
return x * 2;
};
Function<Integer, Integer> addThree = x -> {
System.out.println("Add 3: " + x);
return x + 3;
};
Function<Integer, Integer> composed = multiplyByTwo.compose(addThree);
Function<Integer, Integer> andThen = multiplyByTwo.andThen(addThree);
System.out.println("--- Composed (addThree then multiplyByTwo) ---");
System.out.println("Result: " + composed.apply(5));
System.out.println("--- AndThen (multiplyByTwo then addThree) ---");
System.out.println("Result: " + andThen.apply(5));
}
}
What is the error in the following Java code?
java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class DataStore {
private final List<String> items;
public DataStore(List<String> initialItems) {
this.items = Collections.unmodifiableList(new ArrayList<>(initialItems));
}
public List<String> getItems() {
return items;
}
}
public class MainProcessor {
public static void main(String[] args) {
List<String> original = new ArrayList<>();
original.add("Item A");
DataStore store = new DataStore(original);
store.getItems().add("Item B"); // Attempt to modify unmodifiable list
}
}
What is the output of this code?
java
class MessageQueue {
private String message;
private boolean empty = true;
public synchronized void put(String msg) {
while (!empty) {
try { wait(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
}
this.message = msg;
this.empty = false;
notifyAll();
}
public synchronized String take() {
while (empty) {
try { wait(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
}
empty = true;
notifyAll();
return message;
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
MessageQueue queue = new MessageQueue();
Thread producer = new Thread(() -> queue.put("Hello"));
Thread consumer = new Thread(() -> System.out.println(queue.take() + " World"));
consumer.start();
producer.start();
producer.join();
consumer.join();
}
}
What does this code print?
java
public class Test {
public static void main(String[] args) {
for (int i = 0; i < 2; i++) {
try {
System.out.print("Try " + i + " ");
if (i == 0) {
continue;
}
System.out.print("After continue " + i + " ");
} finally {
System.out.print("Finally " + i + " ");
}
System.out.print("End loop " + i + " ");
}
}
}
What is the output of this code?
java
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add(null);
set.add("Hello");
set.add("World");
System.out.println(set.contains(null));
System.out.println(set.remove("Hello"));
System.out.println(set.size());
System.out.println(set.remove(new String("World")));
System.out.println(set.contains(null));
System.out.println(set.size());
}
}
What is the output of this code?
java
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<Integer> numbers = new LinkedList<>();
numbers.add(1);
numbers.add(2);
numbers.remove();
numbers.clear();
System.out.println(numbers.size());
}
}
What does this code print?
java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<String> emptyList = new ArrayList<>();
Iterator<String> it = emptyList.iterator();
if (it.hasNext()) {
System.out.println("Found: " + it.next());
} else {
System.out.println("List is empty.");
}
}
}
What is the output of this code?
java
public class ThreadStateDemo2 {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> {
try {
Thread.sleep(200); // Simulate work
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("Child thread finished.");
});
t.start();
Thread.sleep(50); // Give thread time to enter sleep
System.out.println("1: " + t.getState());
Thread.sleep(200); // Wait for the thread to finish
System.out.println("2: " + t.getState());
}
}
When you create a `new File("myFile.txt")` object in Java, what happens on the file system?
What happens when you run this Java code?
java
public class LoopTest {
public static void main(String[] args) {
int[] arr = {10, 20};
int index = 0;
do {
System.out.println(arr[index]);
index++;
} while (index <= arr.length);
}
}
When iterating over a `java.util.List` using its `Iterator` and calling `remove()` for an element, what happens to the subsequent elements' indices within the original `List`?
Which statement about `String` objects in Java is most accurate regarding their data type characteristics?
What is the output of this code?
java
String sentence = "Java is a great language, Java!";
int first = sentence.indexOf("Java");
int last = sentence.lastIndexOf("Java");
int missing = sentence.indexOf("Python");
System.out.println(first + "," + last + "," + missing);
What is the error in this Java code?
java
import java.util.List;
import java.util.ArrayList;
public class MyClass {
public static void main(String[] args) {
// Attempt to instantiate an interface directly
List<String> shoppingList = new List<>();
shoppingList.add("Milk");
System.out.println(shoppingList);
}
}
What kind of error will occur when executing the following Java code?
java
import java.util.Comparator;
import java.util.PriorityQueue;
class MyItem {
String value;
MyItem(String v) { this.value = v; }
}
public class QError9 {
public static void main(String[] args) {
Comparator<MyItem> comparator = (a, b) -> a.value.compareTo(b.value);
PriorityQueue<MyItem> pq = new PriorityQueue<>(comparator);
pq.add(new MyItem("alpha"));
pq.add(new MyItem(null));
System.out.println(pq.poll().value);
}
}
What is the output of this code?
java
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
TreeSet<Character> ts = new TreeSet<>();
ts.add('c');
ts.add('a');
ts.add('b');
System.out.println(ts);
}
}
What is the result of executing this Java code snippet?
java
public class ArrayComponentCast {
public static void main(String[] args) {
Object[] stringObjects = new String[2];
stringObjects[0] = "Hello";
stringObjects[1] = "World";
Integer[] integers = (Integer[]) stringObjects;
System.out.println(integers[0]);
}
}
Consider a custom functional interface:
java
@FunctionalInterface
interface Transformer<T, R> {
R transform(T t);
}
If we instantiate this interface with `Transformer<String, ? extends Number>`, which of the following lambda expressions would be valid?