☕ Java MCQ Questions – Page 119
Questions 2361–2380 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat error will occur when compiling and running this Java code?
java
import java.util.HashSet;
import java.util.Set;
public class HashSetError4 {
public static void main(String[] args) {
Set<Integer> numbers = new HashSet<>();
numbers.add(10);
numbers.add("Twenty"); // ERROR line
System.out.println(numbers);
}
}
What does this code print?
java
import java.util.HashMap;
public class Test {
public static void main(String[] args) {
HashMap<String, Double> map = new HashMap<>();
map.put("PI", 3.14);
map.put("E", 2.71);
map.remove("PI");
System.out.println(map.get("PI"));
}
}
What is the output of this code?
java
public class BoxingNullPointer {
public static void main(String[] args) {
Integer i1 = null;
Integer i2 = 10;
try {
System.out.println(i1 == i2);
System.out.println(i1 < i2);
} catch (NullPointerException e) {
System.out.println("NPE caught!");
} catch (Exception e) {
System.out.println("Other exception caught!");
}
}
}
The `final` keyword, when applied to an object reference variable in Java, guarantees which of the following?
A thread `T1` calls `t2.join(long timeout)` on another thread `T2`. If `T2` completes its execution (i.e., its `run()` method finishes) *before* the specified `timeout` expires, what state does `T1` (the joining thread) immediately transition to?
What error will this Java code produce?
java
public class ArrayError {
public static void main(String[] args) {
int[][][] cube = new int[][][];
cube[0][0][0] = 5;
System.out.println(cube[0][0][0]);
}
}
What is the result of running this Java code?
java
public class StringError {
public static void main(String[] args) {
String value = new String(100);
System.out.println(value);
}
}
What is the smallest integer primitive data type in Java by memory size?
What is the compilation error in `Main.java` if `problematicMethod()` is called from `main`?
java
class MyCheckedException extends Exception {}
public class Main {
public static void problematicMethod() {
throw new MyCheckedException();
}
public static void main(String[] args) {
problematicMethod(); // This line causes the compilation error
}
}
What kind of error will occur when compiling the following Java code?
java
import java.util.ArrayDeque;
import java.util.Queue;
public class QError7 {
public static void main(String[] args) {
Queue<String> myQueue = new ArrayDeque<>();
myQueue.push("First");
System.out.println(myQueue.poll());
}
}
What does this code print?
java
import java.io.*;
public class Test {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new StringReader("hello"))) {
System.out.print((char)br.read());
br.close(); // Explicitly close inside try-with-resources
System.out.print((char)br.read());
} catch (IOException e) {
System.out.print("ERROR");
}
}
}
What is the result of executing this Java code snippet?
java
interface MyInterface {}
class A implements MyInterface {}
class B extends A {}
public class InterfaceDowncasting {
public static void main(String[] args) {
MyInterface obj = new A();
B b = (B) obj;
System.out.println("Cast successful");
}
}
What is the result of `(0xFFFFFFFF) >>> 1` in Java, assuming `0xFFFFFFFF` is represented as an `int`?
What is the output of this Java code?
java
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
import java.util.Queue;
class DelayedEvent implements Delayed {
private long startTime;
private String message;
public DelayedEvent(String msg, long delayMillis) {
this.message = msg;
this.startTime = System.currentTimeMillis() + delayMillis;
}
@Override public long getDelay(TimeUnit unit) {
long diff = startTime - System.currentTimeMillis();
return unit.convert(diff, TimeUnit.MILLISECONDS);
}
@Override public int compareTo(Delayed o) {
long diff = this.startTime - ((DelayedEvent) o).startTime;
return diff == 0 ? 0 : (diff < 0 ? -1 : 1);
}
@Override public String toString() { return message; }
}
public class QueueTest {
public static void main(String[] args) throws InterruptedException {
DelayQueue<DelayedEvent> dq = new DelayQueue<>();
dq.put(new DelayedEvent("Event A", 200));
dq.put(new DelayedEvent("Event B", 100));
String result = "";
System.out.println("Queue size: " + dq.size());
result += dq.poll() + ",";
Thread.sleep(150);
result += dq.poll() + ",";
Thread.sleep(100);
result += dq.poll();
System.out.println(result);
}
}
What does this code print?
java
import java.util.function.Predicate;
public class GenericLambdaInference {
static <T> void test(Predicate<T> p, T value) {
System.out.println("Predicate A: " + p.test(value));
}
static void test(Predicate<Integer> p, Integer value) {
System.out.println("Predicate B (Integer): " + p.test(value));
}
public static void main(String[] args) {
test(i -> i > 0, 10);
}
}
What exception will be thrown when running the following Java code?
java
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
public class QueueError {
public static void main(String[] args) {
Queue<String> limitedQueue = new ArrayBlockingQueue<>(1);
limitedQueue.add("First");
limitedQueue.add("Second");
}
}
What is the compile-time error in the provided Java code snippet related to the switch expression?
java
public class SwitchYieldError {
public static void main(String[] args) {
int x = 1;
String result = switch (x) {
case 1 -> yield "One"; // Error expected here
case 2 -> "Two";
default -> "Other";
};
System.out.println(result);
}
}
What is the runtime error encountered when executing this Java code snippet?
java
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
HashSet rawSet = new HashSet(); // Raw type declaration
rawSet.add("Hello");
rawSet.add(123);
// Attempt to iterate and cast elements
for (String s : (Iterable<String>) rawSet) {
System.out.println(s.toUpperCase());
}
}
}
What is the compilation error in the following code?
java
class SuperClass {
SuperClass(String name) {
System.out.println("SuperClass constructor: " + name);
}
}
class SubClass extends SuperClass {
SubClass() {
// Implicit super() call here
}
}
Which statement about method overriding in Java is true?