☕ Java MCQ Questions – Page 189
Questions 3761–3780 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat type of error will occur when running this Java code?
java
public class MixedLocksNotify {
private final Object lockX = new Object();
private final Object lockY = new Object();
public void signalEvent() {
synchronized (lockX) {
System.out.println("Lock X acquired.");
lockY.notify(); // Calling notify on lockY, while holding lockX
System.out.println("Signal sent.");
}
}
public static void main(String[] args) {
MixedLocksNotify m = new MixedLocksNotify();
m.signalEvent();
}
}
What will be the output of the following Java program?
java
import java.util.HashSet;
import java.util.Set;
class MyObject {
int id;
MyObject(int id) { this.id = id; }
}
public class Main {
public static void main(String[] args) {
Set<MyObject> objects = new HashSet<>();
objects.add(new MyObject(1));
objects.add(new MyObject(1));
System.out.println(objects.size());
}
}
What is the output of this Java code snippet?
java
public class SwitchTest {
public static void main(String[] args) {
int dayOfWeek = 1;
String dayName = "";
switch (dayOfWeek) {
case 1:
dayName = "Monday";
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
default:
dayName = "Unknown";
}
System.out.println(dayName);
}
}
What is the output or error of the following Java code?
java
public class ExceptionTest {
public static void main(String[] args) {
try {
int x = 10;
} finally {
System.out.println("Value of x: " + x);
}
}
}
What compilation error will occur in the `DualThrower` class?
java
public class FirstException extends Exception {
public FirstException(String message) { super(message); }
}
public class SecondException extends Exception {
public SecondException(String message) { super(message); }
}
public class DualThrower {
public void process() throws FirstException {
try {
System.out.println("Attempting operation.");
throw new FirstException("Operation failed.");
} finally {
throw new SecondException("Cleanup failed!");
}
}
public static void main(String[] args) {
DualThrower dt = new DualThrower();
try {
dt.process();
} catch (FirstException e) {
System.out.println("Caught First: " + e.getMessage());
} catch (SecondException e) {
System.out.println("Caught Second: " + e.getMessage());
}
}
}
What is the outcome of compiling this Java snippet?
java
public class Main {
public static void main(String[] args) {
if (true) {
int x = 0;
while (x < 5) {
x++;
if (x == 3) {
continue outerLabel; // 'outerLabel' is not defined as a loop
}
}
}
}
}
While not strictly required, which annotation is often used to explicitly mark an interface as a functional interface and enables compile-time checks for this property?
What does this Java code print?
java
public class Main {
public static void main(String[] args) {
boolean isAdmin = true;
if (isAdmin) {
System.out.println("Welcome, Admin!");
}
}
}
What will be printed to the console?
java
public class StringBuilderTest {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Programming");
sb.setCharAt(0, 'P');
sb.setCharAt(5, 'M');
System.out.println(sb);
}
}
What kind of runtime error or unexpected behavior will occur when running this Java code, primarily related to thread lifecycle?
java
public class ProblematicWait implements Runnable {
private final Object sharedLock = new Object();
@Override
public void run() {
try {
sharedLock.wait(100); // Problematic line
} catch (InterruptedException | IllegalMonitorStateException e) {
System.out.println("Caught: " + e.getClass().getSimpleName());
} finally {
System.out.println("Thread exiting.");
}
}
public static void main(String[] args) {
ProblematicWait task = new ProblematicWait();
Thread thread = new Thread(task);
thread.start();
}
}
What is the output of this code?
java
public class Main {
public static void main(String[] args) throws InterruptedException {
Runnable task = () -> {
try {
Thread.sleep(100);
System.out.println("Task finished.");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
};
Thread t = new Thread(task);
t.start();
t.join(); // Wait for t to complete
System.out.println("Main thread completed after join.");
}
}
What error will occur when compiling this Java code?
java
public class ConditionCheck {
public static void main(String[] args) {
int num = 7;
if (num % 2 == 0)
System.out.println("Even");
System.out.println("Number is valid");
else
System.out.println("Odd");
}
}
What is the error in the following Java code?
java
class Base {
static void log() {
System.out.println("Base log");
}
}
class Derived extends Base {
@Override
static void log() { // Attempting to override static method with @Override
System.out.println("Derived log");
}
}
What does the following code print?
java
import java.util.Arrays;
import java.util.List;
import java.util.Comparator;
import java.util.stream.Collectors;
public class StreamTest {
public static void main(String[] args) {
List<String> colors = Arrays.asList("red", "green", "blue", "yellow");
String result = colors.stream()
.sorted(Comparator.comparing(String::length).reversed())
.collect(Collectors.joining(", "));
System.out.println(result);
}
}
What error occurs when running this Java code?
java
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.List;
import java.util.Arrays;
public class ListIteratorAddRemove {
public static void main(String[] args) {
List<String> list = new LinkedList<>(Arrays.asList("one", "two", "three"));
ListIterator<String> it = list.listIterator();
it.next(); // Passes "one"
it.add("zero"); // Adds "zero" after "one"
it.remove(); // Attempts to remove "zero"
}
}
When an object is deserialized using `ObjectInputStream.readObject()`, under which circumstances are the constructors of the *deserialized class* typically *not* invoked?
If a method has a `try` block containing a `return` statement, and its `finally` block also contains a `return` statement, which value will the method ultimately return?
What kind of error will occur when compiling this Java code?
java
class Car {
public Car(String type) {
System.out.println("Car type: " + type);
}
}
class Sedan extends Car {
public Sedan() {
// Missing super() call
System.out.println("Sedan created");
}
}
public class Main {
public static void main(String[] args) {
Sedan mySedan = new Sedan();
}
}
What is the output of this Java code, highlighting a subtle aspect of enum encapsulation?
java
public enum Status {
ACTIVE("A"), INACTIVE("I");
private String code; // Not declared final!
private Status(String code) {
this.code = code;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
public class Main {
public static void main(String[] args) {
System.out.println("Initial ACTIVE code: " + Status.ACTIVE.getCode());
Status.ACTIVE.setCode("X");
System.out.println("Modified ACTIVE code: " + Status.ACTIVE.getCode());
}
}
What is the output of this code?
java
class MyRunnable implements Runnable {
// This signature is incompatible with Runnable.run()
public void run() throws InterruptedException {
System.out.println("Runnable running.");
Thread.sleep(100);
}
}
public class Main {
public static void main(String[] args) {
MyRunnable task = new MyRunnable();
Thread t = new Thread(task);
t.start();
}
}