☕ Java MCQ Questions – Page 166
Questions 3301–3320 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhich statement regarding constructor invocation during object creation and inheritance is TRUE?
What is the runtime error encountered when executing this Java code snippet?
java
import java.util.Set;
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
Set<String> immutableSet = Set.of("apple", "banana"); // Java 9+ immutable set
immutableSet.remove("apple"); // Attempt to modify immutable set
}
}
What is the output of this code?
java
class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("Runnable task executed.");
}
}
public class Main {
public static void main(String[] args) {
MyRunnable r = new MyRunnable();
Thread t = new Thread(r);
t.start();
System.out.println("Main thread finished.");
}
}
A thread is currently executing within a `synchronized` method. Another thread attempts to call a different `synchronized` method on the *same* object. What state does the second thread transition to?
What error will occur when compiling the following Java code?
java
public class SleepUnhandledException {
public void methodWithSleep() {
Thread.sleep(100); // InterruptedException is a checked exception
}
public static void main(String[] args) {
new SleepUnhandledException().methodWithSleep();
}
}
What is the output of this code?
java
public class StringEqualsIgnoreCaseTest {
public static void main(String[] args) {
String s1 = "Straße"; // German sharp s (ß)
String s2 = "STRASSE";
System.out.println(s1.equalsIgnoreCase(s2));
}
}
What is the output of this code?
java
import java.io.BufferedWriter;
import java.io.StringWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
StringWriter stringWriter = new StringWriter();
try (BufferedWriter writer = new BufferedWriter(stringWriter)) {
String data = "Programming Language";
writer.write(data, 0, 11);
writer.flush();
System.out.print(stringWriter.toString());
} catch (IOException e) {
System.out.print("Error");
}
}
}
What is the output of this code?
java
import java.util.LinkedList;
import java.util.Arrays;
public class MyClass {
public static void main(String[] args) {
LinkedList<Object> mixedList = new LinkedList<>(Arrays.asList(100, "Hello", true));
String s = (String) mixedList.get(0); // Attempt to cast Integer to String
System.out.println(s);
}
}
What is the primary purpose of the `finally` block in Java exception handling?
What is the error in the following Java code?
java
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
TreeSet<Integer> numbers = new TreeSet<>();
numbers.add(10);
numbers.add(20);
int first = numbers.get(0);
}
}
Which Java String method would return `true` for a string containing only space characters (e.g., `" "`), but `false` for an empty string (e.g., `""`)?
What is the output of this code?
java
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, String> dictionary = new HashMap<>();
System.out.println(dictionary.isEmpty());
dictionary.put("Hello", "Greeting");
System.out.println(dictionary.isEmpty());
}
}
What error will this Java code produce when executed?
java
public class ArrayError {
public static void main(String[] args) {
int[][] matrix = new int[3][];
System.out.println(matrix[0][0]);
}
}
Which of the following is the primary functional difference between `String.trim()` and `String.strip()`?
If a `switch` expression (Java 14+) has different branches that `yield` values of distinct but compatible types (e.g., one `yield`s an `Integer` and another `yield`s a `Double`), what will be the resulting compile-time type of the `switch` expression?
`FileWriter` constructors do not directly accept a `Charset` object to specify the encoding. If a specific encoding (e.g., UTF-16) is required for a file, how would you best achieve this using the `FileWriter`'s underlying mechanisms?
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<Integer> nums = new ArrayList<>();
nums.add(1);
nums.add(4);
nums.add(2);
nums.add(5);
Iterator<Integer> it = nums.iterator();
while (it.hasNext()) {
Integer num = it.next();
if (num > 2) {
System.out.print(num);
}
}
}
}
Assume a file named 'test.txt' exists with the content "Hello". What is the output of this code?
java
import java.io.FileReader;
import java.io.IOException;
import java.io.File;
public class FileReaderMark {
public static void main(String[] args) throws IOException {
// Assume 'test.txt' exists and contains "Hello"
try (FileReader reader = new FileReader(new File("test.txt"))) {
System.out.println("Mark Supported: " + reader.markSupported());
reader.mark(10); // Attempt to mark
reader.read(); // Read 'H'
reader.reset(); // Attempt to reset
int c = reader.read();
System.out.println("After reset, read: " + (char) c);
}
}
}
What is the output of this Java program?
java
public class MyClass {
public static void main(String[] args) {
int x = 1;
switch (x) {
case 1: System.out.print("One");
case 2: System.out.print("Two");
case 3: System.out.print("Three"); break;
default: System.out.print("Other");
}
}
}
What does this code print when executed?
java
public class Wallet {
private int balance;
public Wallet(int initialBalance) {
this.balance = initialBalance;
}
public void deposit(int amount) {
if (amount > 0) {
balance += amount;
}
}
public int getBalance() {
return balance;
}
public static void main(String[] args) {
Wallet myWallet = new Wallet(100);
myWallet.deposit(50);
myWallet.deposit(-20);
System.out.println(myWallet.getBalance());
}
}