☕ Java MCQ Questions – Page 44
Questions 861–880 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat is a primary benefit of using functional interfaces in conjunction with lambda expressions?
In which scenario is a `do-while` loop *semantically mandatory* over a standard `while` loop to guarantee correct behavior, assuming the loop body must execute at least once, and the initial condition for loop continuation is derived from an operation performed *within* the first iteration of the loop body itself?
What is the output of this code?
java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<Integer> nums = new ArrayList<>(List.of(1, 2, 3));
Iterator<Integer> it1 = nums.iterator();
Iterator<Integer> it2 = nums.iterator();
it1.next(); // it1 advances to 2
it2.next(); // it2 advances to 2
it2.next(); // it2 advances to 3
System.out.println("it1: " + it1.next() + ", it2: " + it2.next());
}
}
What is the error in this Java code?
java
class Parent {
public final void display() {
System.out.println("Parent display");
}
}
class Child extends Parent {
public void display() { // Line 7
System.out.println("Child display");
}
}
public class Main {
public static void main(String[] args) {
Child c = new Child();
c.display();
}
}
Consider an `if-else` structure in Java. If the `if` condition evaluates to `false`, what is guaranteed to happen next?
Which of the following statements about Java daemon threads is true?
What error will occur when this code is executed?
java
public class Main {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("World");
sb.delete(0, 6);
System.out.println(sb);
}
}
What does this Java code print?
java
public class StringTest {
public static void main(String[] args) {
String s = "Hello";
s.concat(" World");
System.out.println(s);
}
}
A junior developer argues that wrapping a `FileReader` with a `BufferedReader` is only necessary for improving performance on extremely large files. A senior developer corrects them, stating it's beneficial for files of all sizes. What is the most compelling reason for the senior developer's stance beyond just raw performance for large files?
Which of the following method call resolution rules has the highest precedence when Java's compiler tries to match an overloaded method?
What is the compilation error in the following Java code snippet?
java
import java.util.concurrent.Callable;
public class Main {
public static void main(String[] args) throws Exception {
int counter = 0;
Callable<Integer> incrementer = () -> {
return counter + 1;
};
counter = 10; // This line is added after the lambda declaration
System.out.println(incrementer.call());
}
}
What is the output of this code?
java
class CustomRuntimeException extends RuntimeException {
public CustomRuntimeException(String message) {
super(message);
}
}
public class Main {
public static void executeTask() {
throw new CustomRuntimeException("Task failed!");
}
public static void main(String[] args) {
try {
executeTask();
System.out.println("Task completed.");
} catch (RuntimeException e) {
System.out.println("Handling runtime exception: " + e.getMessage());
} catch (Exception e) {
System.out.println("Handling general exception: " + e.getMessage());
}
}
}
What is the output of this code?
java
public class Main {
private static String modify(String s) {
s = s + " modified"; // Reassigns local 's'
return s;
}
public static void main(String[] args) {
String original = "initial";
String result = modify(original);
System.out.println(original + "|" + result);
}
}
What is the output of this code?
java
import java.util.TreeSet;
public class App {
public static void main(String[] args) {
TreeSet set = new TreeSet(); // Raw type TreeSet
set.add(10);
set.add(new Object()); // Incompatible type
System.out.println(set.size());
}
}
What is the output of this code?
java
import java.util.HashMap;
public class Test {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("A", 100);
map.put("B", 200);
boolean checkKey = map.containsKey("A");
boolean checkValue = map.containsValue(300);
System.out.println(checkKey + ", " + checkValue);
}
}
What is the most likely output of this code?
java
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class CounterLock {
private int count = 0;
private final Lock lock = new ReentrantLock();
public void increment() {
lock.lock();
try {
count++;
} finally {
lock.unlock();
}
}
public int getCount() {
return count;
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
CounterLock counter = new CounterLock();
Runnable task = () -> {
for (int i = 0; i < 5000; i++) {
counter.increment();
}
};
Thread t1 = new Thread(task);
Thread t2 = new Thread(task);
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(counter.getCount());
}
}
What is the content of 'combined_ops.txt' after this code executes?
java
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class FileObjectCombinedWrite {
public static void main(String[] args) {
File file = new File("combined_ops.txt");
try {
FileWriter writer1 = new FileWriter(file); // Overwrite mode
writer1.write("Initial content.\n");
writer1.close();
FileWriter writer2 = new FileWriter(file, true); // Append mode
writer2.write("Appended content.\n");
writer2.close();
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Is it permissible to have an `if` statement in Java without an accompanying `else` clause?
What is the output of this Java code snippet?
java
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Set<String> letters = new HashSet<>();
letters.add("X");
letters.add("Y");
letters.remove("X");
letters.add("Z");
letters.add("X");
System.out.println(letters.size());
}
}
What type of runtime error will occur when executing this Java code?
java
import java.util.ArrayList;
import java.util.List;
public class ClassCastLoop {
public static void main(String[] args) {
List<Object> mixedList = new ArrayList<>();
mixedList.add("Hello");
mixedList.add(123); // Integer object
mixedList.add("World");
int i = 0;
while (i < mixedList.size()) {
String s = (String) mixedList.get(i); // Potential ClassCastException
System.out.println(s.toUpperCase());
i++;
}
}
}