☕ Java MCQ Questions – Page 19
Questions 361–380 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhy are `String` objects immutable in Java?
What does this code print?
java
public class MyClass {
public static void generateError() {
System.out.println("Generating error...");
throw new NullPointerException("Object is null");
}
public static void main(String[] args) {
try {
generateError();
} catch (Exception e) {
System.out.println("Caught generic exception: " + e.getClass().getSimpleName());
}
System.out.println("Process complete.");
}
}
Consider the following code snippet:
java
interface MyFunc1 { void apply(String s); }
interface MyFunc2 { void apply(Object o); }
public class Ambiguity {
public static void process(MyFunc1 func) { System.out.println("Processing MyFunc1"); }
public static void process(MyFunc2 func) { System.out.println("Processing MyFunc2"); }
public static void main(String[] args) {
// Which lambda expression would cause a compile-time error due to ambiguity?
}
}
Which lambda expression, if used in the `main` method, would cause a compile-time error due to ambiguity?
In which scenario would `java.util.LinkedList` typically offer better performance than `java.util.ArrayList`?
What does this code print to the console?
java
public class Test {
public static void main(String[] args) {
boolean[] flags = new boolean[2];
flags[0] = true;
System.out.println(flags[1]);
}
}
What error will occur when compiling or running this Java code?
java
public class ArrayError {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < numbers.length(); i++) {
System.out.println(numbers[i]);
}
}
}
What is the error in the following Java code?
java
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
TreeSet rawSet = new TreeSet();
rawSet.add(10);
rawSet.add("Twenty");
}
}
What is the output of this Java code?
java
import java.util.function.Supplier;
public class Test {
public static void main(String[] args) {
int x = 10;
Supplier<Integer> closure = () -> {
return x * 2;
};
x = 15;
System.out.println(closure.get());
System.out.println(x);
}
}
What is the output of this code?
java
public class Main {
public static void main(String[] args) {
try {
System.out.print("Outer Try ");
try {
System.out.print("Inner Try ");
} finally {
System.out.print("Inner Finally ");
}
System.out.print("After Inner Blocks ");
} finally {
System.out.print("Outer Finally ");
}
}
}
What is a primary advantage of using immutable objects in a multi-threaded Java application?
What does this code print?
java
public class Test {
public static int modifyValue() {
int x = 0;
try {
x = 10;
System.out.print("Try ");
return x;
} finally {
x = 20;
System.out.print("Finally ");
}
}
public static void main(String[] args) {
System.out.println(modifyValue());
}
}
What is the output of this code? (Assuming Java 14+)
java
public class SwitchTest {
public static void main(String[] args) {
int day = 7;
String dayType = switch (day) {
case 1, 7 -> "Weekend";
case 2, 3, 4, 5, 6 -> "Weekday";
default -> "Invalid Day";
};
System.out.println(dayType);
}
}
What does this code print to the console?
java
class Product { public String getItem() { return "Generic Product"; } }
class Book extends Product { public String getTitle() { return "My Book"; } }
class MyStore { public Product sell() { return new Product(); } }
class Bookstore extends MyStore { @Override public Book sell() { return new Book(); } }
public class Main {
public static void main(String[] args) {
MyStore store = new Bookstore();
Product p = store.sell();
System.out.println(p.getClass().getSimpleName());
}
}
What will be printed to the console by this Java program?
java
public class StringBufferTest {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Start");
sb.append(10).insert(3, 'X').reverse();
System.out.print(sb);
}
}
What does this Java code print?
java
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Set<String> fruits = new HashSet<>();
fruits.add("apple");
fruits.add("banana");
fruits.add("cherry");
fruits.remove("banana");
System.out.println(fruits.contains("banana"));
}
}
What does this code print?
java
import java.util.TreeMap;
public class TreeMapRemoval {
public static void main(String[] args) {
TreeMap<Integer, String> map = new TreeMap<>();
map.put(10, "A");
map.put(20, "B");
map.put(30, "C");
map.remove(20);
System.out.println(map.containsKey(20) + "," + map.size());
}
}
What is the output of this program?
java
class ConfigurationError extends Error {
public ConfigurationError(String message) {
super(message);
}
}
public class Main {
public static void loadConfig() {
throw new ConfigurationError("Missing config file");
}
public static void main(String[] args) {
try {
loadConfig();
} catch (ConfigurationError e) {
System.out.println("Caught Error: " + e.getMessage());
} catch (Exception e) {
System.out.println("Caught Exception: " + e.getMessage());
}
}
}
What is the compilation error in the following Java code snippet?
java
public class LoopError {
public static void main(String[] args) {
int num = 0;
do {
System.out.println(num);
num++;
}
while (num < 2);
}
}
Can an `ArrayList` store `null` values?
Which of the following `BlockingQueue` implementations offers a constructor parameter to configure 'fairness', influencing the order in which waiting producer and consumer threads acquire access to the queue?