☕ Java MCQ Questions – Page 198
Questions 3941–3960 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat does this code print?
java
import java.util.TreeSet;
import java.util.Set;
public class App {
public static void main(String[] args) {
TreeSet<Integer> original = new TreeSet<>();
original.add(10); original.add(20); original.add(30); original.add(40); original.add(50);
Set<Integer> sub = original.subSet(20, true, 40, false); // [20, 40)
System.out.println(sub.size());
sub.add(25); // Add to subset
original.add(35); // Add to original
System.out.println(sub);
}
}
What kind of error will occur when executing the following Java code?
java
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Queue;
public class QError6 {
public static void main(String[] args) {
Queue<Integer> pq = new PriorityQueue<>();
pq.add(10);
pq.add(20);
Iterator<Integer> it = pq.iterator();
while (it.hasNext()) {
it.next();
it.remove();
}
}
}
What error will occur when compiling or running this Java code?
java
public class ArrayError {
public static void main(String[] args) {
String[] words = new String[3];
// words[0] is null by default
System.out.println(words[0].toUpperCase());
}
}
Which statement most accurately describes a potential pitfall or nuance when using `StringBuffer` in a complex multi-threaded application, despite its internal synchronization?
What error will this code produce?
java
public class LoopError {
public static void main(String[] args) {
for (int i = 0 i < 5; i++) {
System.out.println(i);
}
}
}
What is the error in the following Java code?
java
class Configuration {
private static String API_KEY = "abcd123";
public static String getApiKey() {
return API_KEY;
}
}
public class ExternalService {
public static void main(String[] args) {
System.out.println(Configuration.API_KEY); // Direct access to private static field
}
}
What is the output of this Java code?
java
import java.io.*;
class MyNonSerializableObject {
String name;
public MyNonSerializableObject(String name) { this.name = name; }
}
public class DeserializationError1 {
public static void main(String[] args) {
byte[] data = null;
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
oos.writeObject(new MyNonSerializableObject("Test"));
data = bos.toByteArray();
} catch (IOException e) {
System.out.println("Error during serialization: " + e.getClass().getName() + ": " + e.getMessage());
}
if (data != null) {
try (ByteArrayInputStream bis = new ByteArrayInputStream(data);
ObjectInputStream ois = new ObjectInputStream(bis)) {
MyNonSerializableObject obj = (MyNonSerializableObject) ois.readObject();
System.out.println("Deserialized: " + obj.name);
} catch (Exception e) {
System.out.println("Error during deserialization: " + e.getClass().getName() + ": " + e.getMessage());
}
}
}
}
What happens if you attempt to insert a `null` key into a `TreeMap`?
What is the output of this code?
java
class Base {
private void greet() { System.out.println("Base private greet"); }
public void callGreet() { greet(); }
}
class Derived extends Base {
private void greet() { System.out.println("Derived private greet"); }
}
public class Test {
public static void main(String[] args) {
Base b = new Derived();
b.callGreet();
}
}
What does this code print?
java
import java.io.File;
import java.io.IOException;
public class FileRename {
public static void main(String[] args) throws IOException {
File oldFile = new File("original.txt");
oldFile.createNewFile();
File newFile = new File("renamed.txt");
boolean renamed = oldFile.renameTo(newFile);
boolean oldExists = oldFile.exists();
boolean newExists = newFile.exists();
// Cleanup
newFile.delete();
System.out.println(renamed + ", " + oldExists + ", " + newExists);
}
}
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<String> items = new ArrayList<>(List.of("A", "B", "C"));
Iterator<String> it = items.iterator();
try {
it.remove(); // Attempt to remove before calling next()
} catch (IllegalStateException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
What is the output of this code?
java
public class ReentrancyTest {
public synchronized void outerMethod() {
System.out.println(Thread.currentThread().getName() + " inside outerMethod.");
innerMethod(); // Calls another synchronized method on the same object
System.out.println(Thread.currentThread().getName() + " exiting outerMethod.");
}
public synchronized void innerMethod() {
System.out.println(Thread.currentThread().getName() + " inside innerMethod.");
}
public static void main(String[] args) throws InterruptedException {
ReentrancyTest test = new ReentrancyTest();
Thread t = new Thread(test::outerMethod, "TestThread");
t.start();
t.join();
}
}
What is the output of this Java code snippet?
java
public class ArrayTest {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40};
System.out.println(numbers[2]);
}
}
What is an 'infinite loop' in the context of a `while` loop?
Does this Java code compile? If not, what is the compile-time error?
java
import java.io.IOException;
public class MainMethodThrows {
public static void doSomething() throws IOException {
throw new IOException("Something went wrong");
}
public static void main(String[] args) {
doSomething();
System.out.println("Done.");
}
}
What does this code print?
java
public class MyClass {
public static void checkValue(int value) {
if (value < 0) {
throw new IndexOutOfBoundsException("Value cannot be negative");
}
System.out.println("Value is: " + value);
}
public static void main(String[] args) {
try {
checkValue(-5);
} catch (IndexOutOfBoundsException e) {
System.out.println("Handled error: " + e.getMessage());
}
}
}
What is wrong with this Java code?
java
import java.util.function.Runnable;
public class Test {
public static void main(String[] args) {
Runnable r = -> System.out.println("Running");
}
}
What is the output of this Java code?
java
public class LoopTest {
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
if (i == 1) {
continue;
}
System.out.print(i);
}
}
}
What is the output of this code?
java
public class PrePostContinueWhile {
public static void main(String[] args) {
int i = 0;
int sum = 0;
while (i++ < 5) {
if (i % 2 == 0) {
continue;
}
sum += i;
}
System.out.println(sum + ", " + i);
}
}
What kind of error will occur when this Java code is executed?
java
public class Test {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("HelloWorld");
sb.delete(5, 2);
System.out.println(sb);
}
}