☕ Java MCQ Questions – Page 182
Questions 3621–3640 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat does this code print?
java
class MyExecutor {
static void executeTask() throws RuntimeException {
System.out.println("Task started");
throw new NullPointerException("Simulated NPE");
}
public static void main(String[] args) {
try {
executeTask();
} catch (NullPointerException e) {
System.out.println("Caught NPE: " + e.getMessage());
} catch (RuntimeException e) {
System.out.println("Caught RuntimeException: " + e.getMessage());
}
}
}
What is the output of this Java code?
java
import java.io.File;
public class FileTest {
public static void main(String[] args) {
File file = new File("path/to/myFile.txt");
System.out.println(file.getName());
}
}
What is the result of compiling the following Java code, assuming 'somefile.txt' may or may not exist?
java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class TestClass {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new FileReader("somefile.txt"));
System.out.println("File opened.");
try {
br.close();
} catch (IOException e) {
// Handle close exception
}
}
}
When a `break` statement is encountered inside a Java `for` loop, what is its immediate effect?
What is the compile-time error in this Java program?
java
public class Test {
public static void main(String[] args) {
outerLoop: for (int i = 0; i < 3; i++) {
innerLoop: for (int j = 0; j < 3; j++) {
if (i * j == 4) {
break unknownLabel;
}
}
}
}
}
What compile-time error will occur when compiling this Java code?
java
public class MyClass {
public static void main(String[] args) {
double value = 3.14;
switch (value) {
case 3.14:
System.out.println("PI");
break;
default:
System.out.println("Other value");
}
}
}
What does this code print?
java
import java.util.LinkedList;
public class MyClass {
public static void main(String[] args) {
LinkedList<String> cities = new LinkedList<>();
cities.add("London");
cities.add("Paris");
cities.add("Rome");
System.out.println(cities.get(0));
}
}
What does this code print?
java
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
final List<String> names = new ArrayList<>();
names.add("Alice");
names.remove(0);
names.add("Bob");
System.out.println(names.size());
}
}
What is the output of this Java code snippet?
java
import java.util.function.Predicate;
public class Test {
public static void main(String[] args) {
Predicate<Integer> isEven = num -> num % 2 == 0;
Predicate<Integer> isPositive = num -> num > 0;
Predicate<Integer> isLessThanTen = num -> num < 10;
Predicate<Integer> combined = isEven.and(isPositive).or(isLessThanTen);
System.out.println(combined.test(4));
System.out.println(combined.test(-2));
System.out.println(combined.test(11));
}
}
What is the output of this code?
java
import java.util.HashSet;
import java.util.Objects;
class ItemA {
int id;
String name;
public ItemA(int id, String name) { this.id = id; this.name = name; }
@Override public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ItemA itemA = (ItemA) o;
return id == itemA.id; // Only compares id
}
@Override public int hashCode() { return Objects.hash(name); } // Only hashes name
}
public class Main {
public static void main(String[] args) {
HashSet<ItemA> set = new HashSet<>();
set.add(new ItemA(1, "Apple"));
set.add(new ItemA(1, "Orange"));
System.out.println(set.size());
}
}
What kind of error will occur when executing this Java code?
java
import java.util.TreeSet;
import java.util.SortedSet;
public class Main {
public static void main(String[] args) {
TreeSet<Integer> originalSet = new TreeSet<>();
originalSet.add(10);
originalSet.add(20);
originalSet.add(30);
SortedSet<Integer> sub = originalSet.subSet(25, 15);
System.out.println(sub.size());
}
}
In Java, when are nested `if` statements typically most appropriate?
Functional interfaces are primarily designed to be target types for which Java 8 feature?
What is the result of compiling and running the following Java code snippet?
java
import java.util.List;
public class StringError {
public static void main(String[] args) {
List<Integer> numbers = List.of(1, 2, 3);
String result = String.join("-", numbers);
System.out.println(result);
}
}
What is the output of this code?
java
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
public class FilterRuntimeException {
public static void main(String[] args) {
File dir = new File("."); // Current directory
FilenameFilter brokenFilter = (currentDir, name) -> {
if (name.contains("error")) {
throw new RuntimeException("Filter processing failed for: " + name);
}
return true;
};
try {
File errorFile = new File(dir, "file_with_error_in_name.txt");
errorFile.createNewFile();
String[] files = dir.list(brokenFilter); // This call will trigger the filter
System.out.println("Files listed: " + (files != null ? files.length : "null"));
errorFile.delete();
} catch (RuntimeException e) {
System.out.println("Caught RuntimeException: " + e.getMessage());
} catch (IOException e) {
System.out.println("Caught IOException: " + e.getClass().getSimpleName());
}
}
}
Consider this Java code. What is the output?
java
public class ThreadLocalRunnable {
private static ThreadLocal<String> threadLocalVar = ThreadLocal.withInitial(() -> "Default");
public static void main(String[] args) throws InterruptedException {
Runnable task = () -> {
System.out.println(Thread.currentThread().getName() + " Initial: " + threadLocalVar.get());
threadLocalVar.set(Thread.currentThread().getName() + " Value");
try { Thread.sleep(50); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
System.out.println(Thread.currentThread().getName() + " Final: " + threadLocalVar.get());
};
Thread t1 = new Thread(task, "Thread-1");
Thread t2 = new Thread(task, "Thread-2");
t1.start();
t2.start();
t1.join();
t2.join();
}
}
What does the following Java code print to the console?
java
import java.util.HashSet;
import java.util.Set;
class MutableItem {
private int value;
public MutableItem(int value) { this.value = value; }
public void setValue(int value) { this.value = value; }
@Override public int hashCode() { return value; }
@Override public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MutableItem that = (MutableItem) o;
return value == that.value;
}
}
public class Main {
public static void main(String[] args) {
Set<MutableItem> items = new HashSet<>();
MutableItem item1 = new MutableItem(10);
items.add(item1);
item1.setValue(20);
System.out.println(items.contains(item1));
}
}
What is the output of this code?
java
import java.io.*;
public class Test {
public static void main(String[] args) throws IOException {
String data = "ABC\r\nDEF\nGH\rI";
BufferedReader br = new BufferedReader(new StringReader(data));
System.out.println(br.readLine());
System.out.println(br.readLine());
System.out.println(br.readLine());
System.out.println(br.readLine());
br.close();
}
}
In Java polymorphism, can a reference variable of a superclass type hold an object of its subclass?
What does this code print?
java
import java.util.TreeMap;
import java.util.Map;
public class TreeMapNullValue {
public static void main(String[] args) {
TreeMap<String, String> map = new TreeMap<>();
map.put("one", "1");
map.put("two", null);
map.put("three", "3");
System.out.println(map.containsKey("two") + "," + map.containsValue(null));
}
}