☕ Java MCQ Questions – Page 78
Questions 1541–1560 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat kind of error will occur when compiling this Java code?
java
public class ArrayError {
public static void main(String[] args) {
int[] numbers = new int[3] {1, 2, 3};
System.out.println(numbers[0]);
}
}
What does this code print?
java
public class MissingStart implements Runnable {
@Override
public void run() {
System.out.println("This should not print.");
}
public static void main(String[] args) {
Runnable r = new MissingStart();
// No Thread created or started
System.out.println("Program finished.");
}
}
What does this code print to the console?
java
public class Counter {
private int count;
public Counter() {
this.count = 0;
}
public void increment() {
count++;
}
public int getCount() {
return count;
}
public static void main(String[] args) {
Counter c = new Counter();
c.increment();
c.increment();
System.out.println(c.getCount());
}
}
What is the output of this code?
java
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class LockConditionTest {
private final ReentrantLock lock = new ReentrantLock();
private final Condition condition = lock.newCondition();
private boolean flag = false;
public void worker1() throws InterruptedException {
lock.lock();
try {
while (!flag) { condition.await(); }
System.out.println("Worker1 done.");
} finally { lock.unlock(); }
}
public void worker2() {
lock.lock();
try { flag = true; condition.signalAll(); }
finally { lock.unlock(); }
}
public static void main(String[] args) throws InterruptedException {
LockConditionTest test = new LockConditionTest();
new Thread(() -> { try { test.worker1(); } catch (InterruptedException e) {} }).start();
Thread.sleep(100); // Give worker1 time to await
new Thread(test::worker2).start();
Thread.sleep(500); // Allow threads to finish
}
}
What is the runtime error encountered when executing this Java code snippet?
java
import java.util.HashSet;
import java.util.Objects;
class KeyWithoutInstanceof {
int value;
public KeyWithoutInstanceof(int value) { this.value = value; }
@Override
public boolean equals(Object o) { // Missing instanceof check
KeyWithoutInstanceof other = (KeyWithoutInstanceof) o; // Dangerous cast
return this.value == other.value;
}
@Override
public int hashCode() { return Objects.hashCode(value); }
}
public class Main {
public static void main(String[] args) {
HashSet<KeyWithoutInstanceof> set = new HashSet<>();
set.add(new KeyWithoutInstanceof(10));
System.out.println(set.contains("some_string")); // Triggering equals with incompatible type
}
}
What is the output of this code?
java
public class OperatorChallenge {
public static void main(String[] args) {
int maxInt = Integer.MAX_VALUE;
long l = (long) maxInt + 1;
int i = (int) l;
System.out.println(i);
}
}
What is the output of this Java program?
java
class Box {
Box() {
System.out.println("Box created.");
}
}
public class Main {
public static void main(String[] args) {
Box b1 = new Box();
Box b2 = new Box();
}
}
A thread transitions from the `RUNNABLE` state to `TIMED_WAITING` when which of the following `Thread` class methods is invoked with a timeout?
What does this Java code print?
java
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
TreeMap<Integer, String> treeMap = new TreeMap<>();
treeMap.put(10, "Ten");
treeMap.put(20, "Twenty");
System.out.println(treeMap.containsKey(10) + " " + treeMap.containsKey(30));
}
}
What is the result of compiling and running the following Java code snippet?
java
public class StringError {
public static void main(String[] args) {
String text = "abcdef";
boolean matches = text.matches("[a-z+");
System.out.println(matches);
}
}
What does this Java code print?
java
public class LoopTest {
public static void main(String[] args) {
for (int i = 1; i <= 2; i++) {
System.out.println("Hello");
}
}
}
What is the output of this code?
java
public class Test {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("ABC");
sb.insert(1, "X").insert(3, "Y").insert(0, "P");
System.out.println(sb);
}
}
What is the primary purpose of the `break` keyword within a `case` block of a Java `switch` statement?
What is the error encountered when running this Java code?
java
import java.io.FileReader;
import java.io.IOException;
public class FileReaderError6 {
public static void main(String[] args) {
String filePath = null; // Null file path
try (FileReader fr = new FileReader(filePath)) { // Constructor with null String
int data = fr.read();
System.out.println("Read: " + (char)data);
} catch (IOException e) {
System.err.println(e.getClass().getSimpleName() + ": " + e.getMessage());
} catch (NullPointerException e) {
System.err.println(e.getClass().getSimpleName() + ": Path must not be null");
}
}
}
What error will occur when compiling this Java code?
java
public class ConditionCheck {
public static void main(String[] args) {
int num = 10;
if (num > 5) {
String message = "Number is large";
if (num > 15) {
message = "Number is very large";
}
} else {
System.out.println(message);
}
}
}
What compile-time error will this code produce?
java
class Task implements Runnable {
public void run() throws Exception {
System.out.println("Task is running...");
if (Math.random() > 0.5) {
throw new Exception("Something went wrong");
}
}
}
public class Main {
public static void main(String[] args) {
Task myTask = new Task();
new Thread(myTask).start();
}
}
What is the output of this code?
java
public class Test {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Programming");
sb.delete(3, 6);
sb.deleteCharAt(4);
System.out.println(sb);
}
}
What is the output of this code?
java
public class ArrayDefaultValues {
public static void main(String[] args) {
int[] intArray = new int[3];
String[] stringArray = new String[2];
Object[] objectArray = new Object[1];
Boolean[] booleanArray = new Boolean[1];
System.out.print(intArray[0]);
System.out.print(stringArray[0] == null);
System.out.print(objectArray[0] == null);
System.out.println(booleanArray[0]);
}
}
What compile-time error will occur in the `MyClass` definition?
java
class MyClass {
final MyClass() {
System.out.println("Final constructor");
}
}
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
}
}
What error does this Java code produce when executed?
java
public class Main {
public static void main(String[] args) {
String word = "Java";
System.out.println(word.charAt(4));
}
}