☕ Java MCQ Questions – Page 70
Questions 1381–1400 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat does this code print?
java
import jakarta.validation.*;
import jakarta.validation.constraints.NotNull;
import java.util.Set;
public class Test {
static class Item {
@NotNull public String name;
public Item(String name) { this.name = name; }
}
static class Box {
@Valid public Item item;
public Box(Item item) { this.item = item; }
}
public static void main(String[] args) {
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Box box = new Box(new Item(null));
Set<ConstraintViolation<Box>> violations = validator.validate(box);
System.out.println("Violations: " + violations.size());
}
}
How is a `Runnable` object typically executed in a separate thread?
What is the output of this code?
java
public class WhileFinally {
public static void main(String[] args) {
int count = 0;
int x = 0;
while (x < 3) {
try {
x++;
if (x == 2) {
throw new RuntimeException("Error at 2");
}
count++;
} catch (RuntimeException e) {
count += 10;
continue;
} finally {
count += 1;
}
}
System.out.println(count);
}
}
Which operator checks if two operands are equal in Java?
Which operator provides a shorthand for `x = x + y`?
What does this code print?
java
public class Main {
public static void main(String[] args) {
boolean p = true;
boolean q = false;
System.out.println(p && q);
}
}
Which method is used to retrieve, but not remove, the head of a Queue, returning null if the queue is empty?
Under ideal conditions (good hash function, few collisions), what is the average time complexity for `get()` and `put()` operations in a `HashMap`?
What is the main purpose of the `volatile` keyword in Java?
What is the fundamental data structure that `ArrayList` uses internally to store its elements?
What is the expected outcome of running this code?
java
public class DeadlockExample {
private final Object lockA = new Object();
private final Object lockB = new Object();
public void task1() {
synchronized (lockA) {
System.out.println(Thread.currentThread().getName() + " acquired LockA");
try { Thread.sleep(50); } catch (InterruptedException e) {}
synchronized (lockB) {
System.out.println(Thread.currentThread().getName() + " acquired LockB");
}
}
}
public void task2() {
synchronized (lockB) {
System.out.println(Thread.currentThread().getName() + " acquired LockB");
try { Thread.sleep(50); } catch (InterruptedException e) {}
synchronized (lockA) {
System.out.println(Thread.currentThread().getName() + " acquired LockA");
}
}
}
public static void main(String[] args) throws InterruptedException {
DeadlockExample example = new DeadlockExample();
new Thread(example::task1, "Thread-1").start();
new Thread(example::task2, "Thread-2").start();
Thread.sleep(500);
System.out.println("Main thread exiting.");
}
}
What is the output of this Java program?
java
import java.io.*;
class Nullable implements Serializable {
private String data;
private Integer number;
public Nullable(String data, Integer number) {
this.data = data;
this.number = number;
}
public String toString() {
return "Data: " + data + ", Number: " + number;
}
}
public class Main {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Nullable obj = new Nullable(null, null);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.close();
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
Nullable deserializedObj = (Nullable) ois.readObject();
ois.close();
System.out.println(deserializedObj);
}
}
What is the result of attempting to run the following Java code?
java
public class Main {
public static void main(String[] args) {
Object lock = new Object();
try {
lock.notify(); // This line
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Notified.");
}
}
What is the output of this code?
java
public class Q2_ThreadJoinState {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
t.start();
Thread.sleep(10); // Ensure t is running
System.out.println(t.getState()); // State 1
t.join(); // Main thread waits for t to finish
System.out.println(t.getState()); // State 2
}
}
What is the output of this Java code, specifically the sequence of messages related to `Thread.join()`?
java
public class ThreadJoinRunnable {
public static void main(String[] args) throws InterruptedException {
Runnable task = () -> {
try {
System.out.println(Thread.currentThread().getName() + " started.");
Thread.sleep(200);
System.out.println(Thread.currentThread().getName() + " finished.");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
};
Thread t1 = new Thread(task, "T1");
Thread t2 = new Thread(task, "T2");
t1.start();
t2.start();
System.out.println("Main waiting for T1.");
t1.join();
System.out.println("T1 joined, Main waiting for T2.");
t2.join();
System.out.println("T2 joined, Main finished.");
}
}
What does this code print?
java
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<Integer> numbers = new LinkedList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.remove();
System.out.println(numbers.element());
}
}
What compile-time error occurs when attempting to compile this Java code?
java
interface A {
default void perform() {
System.out.println("A's perform");
}
}
interface B {
default void perform() {
System.out.println("B's perform");
}
}
class MyClass implements A, B {
// No implementation for perform()
}
Which of the following scenarios would typically result in an `IllegalStateException` when using `java.util.Iterator`?
What does this Java code print to the console?
java
public class Main {
public static void main(String[] args) {
String name = "Java";
System.out.println(name);
}
}
What will this Java 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> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
Iterator<Integer> it = numbers.iterator();
try {
it.remove();
} catch (IllegalStateException e) {
System.out.println("Caught: " + e.getClass().getSimpleName());
}
}
}