☕ Java MCQ Questions – Page 138
Questions 2741–2760 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat is the output of this code?
java
public class Main {
public static void main(String[] args) {
Thread t = new Thread(() -> System.out.println("Worker thread."));
try {
t.setPriority(Thread.MAX_PRIORITY + 1); // Invalid priority
} catch (SecurityException e) {
System.out.println("Security Exception");
}
t.start();
}
}
Which of the following statements about `java.util.Arrays.copyOf(original, newLength)` is true?
What is the output of this code?
java
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<String> colors = new ArrayList<>();
colors.add("Red");
colors.add("Green");
colors.add("Blue");
colors.remove(0);
System.out.println(colors.get(0));
}
}
Which Java String method is primarily used to compare the actual character sequence content of two String objects for equality, regardless of their memory addresses?
Which statement about constructors in abstract classes is *true*?
What is the primary purpose of the `java.util.concurrent.BlockingQueue` interface?
Which of the following assignments is *not* legal in Java (resulting in a compile-time error)?
Consider the following Java code. What will be printed to the console?
java
public class SwitchTest {
public static void main(String[] args) {
char code = 'C';
String description = "";
switch (code) {
case 'A':
case 'E':
case 'I':
description = "Vowel";
break;
case 'O':
description = "Round Vowel";
break;
case 'U':
description = "Long Vowel";
break;
default:
description = "Consonant";
}
System.out.println(description);
}
}
What happens when a `continue` statement is executed within a `do-while` loop?
What is the output of this code?
java
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderTest {
public static void main(String[] args) {
// Assume test.txt exists and contains a single character 'Z'
File file = new File("test.txt");
try (FileReader reader = new FileReader(file)) {
int charCode = reader.read();
System.out.print((char) charCode);
} catch (IOException e) {
System.out.print("Error");
}
}
}
What kind of error will occur when compiling the following Java code, assuming 'input.txt' exists?
java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class TestClass {
public static void main(String[] args) {
BufferedReader br;
try (br = new BufferedReader(new FileReader("input.txt"))) {
String line = br.readLine();
System.out.println(line);
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
}
}
}
Which statement highlights a key distinction in how abstract classes and interfaces (post-Java 8 with default methods) interact with inheritance hierarchies?
What is the output of this code?
java
import java.util.ArrayList;
import java.util.List;
final class MyImmutableList {
private final List<String> data;
public MyImmutableList(List<String> data) {
this.data = new ArrayList<>(data); // Defensive copy
}
public List<String> getData() {
return new ArrayList<>(data); // Defensive copy
}
}
public class Main {
public static void main(String[] args) {
List<String> mutableList = new ArrayList<>();
mutableList.add("A");
MyImmutableList immutableList = new MyImmutableList(mutableList);
mutableList.add("B"); // Modify original list
immutableList.getData().add("C"); // Modify returned copy
System.out.println(immutableList.getData().size());
}
}
Which keyword is used to define a block of code that should be executed if no other `case` label matches the `switch` expression?
What is the output of this code?
java
import java.util.HashSet;
public class Test {
public static void main(String[] args) {
HashSet<Character> chars = new HashSet<>();
chars.add('A');
chars.add('B');
chars.add('C');
chars.remove('B');
System.out.println(chars.size());
}
}
What is the output of this Java code?
java
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class FileWriterToDirectory {
public static void main(String[] args) {
String dirname = "my_directory";
File dir = new File(dirname);
try {
Files.createDirectory(Path.of(dirname));
try (FileWriter fw = new FileWriter(dir)) {
fw.write("This should fail.");
}
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
} finally {
try { Files.deleteIfExists(Path.of(dirname)); } catch (IOException ignored) {}
}
}
}
What error occurs when the `start()` method is called multiple times on the same `Thread` object?
java
class MyRunnable implements Runnable {
public void run() {
System.out.println("Thread running.");
}
}
public class Main {
public static void main(String[] args) {
MyRunnable r = new MyRunnable();
Thread t = new Thread(r);
t.start();
t.start(); // Second call to start()
}
}
What error will occur when running this Java code snippet?
java
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;
public class Test {
public static void main(String[] args) {
Map<String, Integer> counts = new HashMap<>();
counts.put("apple", 10);
// Attempt to merge with a null remapping function
// when both old and new values are non-null
counts.merge("apple", 5, null);
System.out.println(counts.get("apple"));
}
}
What will be the output of this Java code?
java
import java.util.function.Supplier;
class Product {
String name;
public Product() { this.name = "Default Product"; }
public Product(String name) { this.name = name; }
}
public class ConstructorReference {
public static void main(String[] args) {
Supplier<Product> productSupplier = Product::new; // References no-arg constructor
Product p = productSupplier.get();
System.out.println(p.name);
}
}
What error occurs when executing the following Java code?
java
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> immutableColors = List.of("Cyan", "Magenta", "Yellow");
immutableColors.add("Black"); // Attempt to add to an immutable list
System.out.println(immutableColors);
}
}