☕ Java MCQ Questions

3994 multiple choice questions with answers and explanations — free practice for Java interviews

▶ Practice All Java Questions
Q1 hard code error
What is the runtime error when executing the provided Java code snippet?
java
public class NullSwitchError {
    public static void main(String[] args) {
        Integer value = null;
        String result = switch (value) { // Error expected at runtime
            case 1 -> "One";
            case 2 -> "Two";
            default -> "Other";
        };
        System.out.println(result);
    }
}
Q2 easy
Which of the following correctly declares and initializes a single-dimensional array named `colors` with values "Red", "Green", and "Blue"?
Q3 hard
When a class with mutable `private` fields is serialized and deserialized using default mechanisms, what is a key encapsulation concern?
Q4 easy code error
What will be the outcome when compiling and running this Java code?
java
public class StringModification {
    public static void main(String[] args) {
        String myString = "Java";
        myString.charAt(0) = 'j';
        System.out.println(myString);
    }
}
Q5 hard code output
What does this code print?
java
abstract class Shape {
    abstract void draw();
    protected void display() { System.out.println("Shape display"); }
}
class Circle extends Shape {
    @Override
    public void draw() { System.out.println("Drawing Circle"); }
    @Override
    public void display() { System.out.println("Circle display"); }
}
public class Test {
    public static void main(String[] args) {
        Shape s = new Circle();
        s.draw();
        s.display();
    }
}
Q6 hard code error
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 = "hello";
        System.out.println(text.codePointAt(10));
    }
}
Q7 easy code error
What is the output or error of the following Java code?
java
public class ExceptionTest {
    public static void main(String[] args) {
        try (Object obj = new Object()) {
            System.out.println("Inside try-with-resources");
        } catch (Exception e) {
            System.out.println("Caught exception: " + e.getMessage());
        }
    }
}
Q8 hard code output
What is the output of this code?
java
import java.util.Date;

final class MyImmutableDate {
    private final Date value;
    public MyImmutableDate(Date value) {
        this.value = new Date(value.getTime()); // Defensive copy
    }
    public Date getValue() {
        return new Date(value.getTime()); // Defensive copy
    }
}

public class Main {
    public static void main(String[] args) {
        Date originalDate = new Date(0);
        MyImmutableDate immutableDate = new MyImmutableDate(originalDate);
        originalDate.setYear(120); // Modify original
        immutableDate.getValue().setYear(121); // Modify obtained copy
        System.out.println(immutableDate.getValue().getYear());
    }
}
Q9 medium code error
What is the error in this Java code?
java
import java.util.ArrayList;
import java.util.List;

public class MyClass {
    public static void main(String[] args) {
        List<String> inventory = null;
        inventory.add("Sword"); // Attempt to add to a null list
        System.out.println(inventory.size());
    }
}
Q10 hard
Which statement accurately describes the time complexity characteristics of `java.util.LinkedList` for a list containing N elements?
Q11 medium code output
What is the output of the following Java code?
java
public class SpecificNullOverload {
    void execute(Object o) {
        System.out.println("Object arg");
    }
    void execute(Integer i) {
        System.out.println("Integer arg");
    }
    public static void main(String[] args) {
        SpecificNullOverload sno = new SpecificNullOverload();
        sno.execute(null);
    }
}
Q12 hard
Which specific method of its internal `HashMap` does `HashSet.add(E e)` primarily invoke to store an element, considering `HashSet` only stores keys and uses a dummy value?
Q13 hard
Given a `TreeMap<Integer, String> map = new TreeMap<>(); map.put(10, "Ten"); map.put(20, "Twenty"); map.put(30, "Thirty");`. What will `map.lowerEntry(10)` return?
Q14 medium code error
What is the output of this Java code?
java
public class ExceptionFlow1 {
    public static void main(String[] args) {
        System.out.println(testMethod());
    }

    public static String testMethod() {
        try {
            System.out.println("In try");
            return "Success";
        } catch (Exception e) {
            System.out.println("In catch");
            return "Failure";
        } finally {
            System.out.println("In finally");
        }
    }
}
Q15 medium
Which of the following statements about `java.util.LinkedList` memory usage compared to `java.util.ArrayList` is generally true?
Q16 easy code output
What is the output of this code?
java
class CharacterPrinter {
    public void printChar(char c) {
        System.out.print(c);
    }
}

public class MyProgram {
    public static void main(String[] args) throws InterruptedException {
        CharacterPrinter cp = new CharacterPrinter();
        Thread t1 = new Thread(() -> {
            cp.printChar('A');
            cp.printChar('B');
        });
        Thread t2 = new Thread(() -> {
            cp.printChar('C');
            cp.printChar('D');
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
    }
}
Q17 hard code error
What compilation error will occur when compiling the following Java code?
java
import java.util.function.Function;

public class LambdaVoidReturn {
    public static void main(String[] args) {
        Function<String, Integer> stringToInt = s -> System.out.println(s);
    }
}
Q18 easy code error
What kind of error will occur when this Java code is executed?
java
public class Test {
    public static void main(String[] args) {
        StringBuilder sb = null;
        sb.append("Hello");
        System.out.println(sb);
    }
}
Q19 easy
What is considered a best practice for managing system resources after using a `FileReader`?
Q20 hard code error
What kind of runtime error or unexpected behavior will occur when running this Java code, primarily related to thread lifecycle?
java
public class ReStartThread implements Runnable {
    public void run() {
        System.out.println(Thread.currentThread().getName() + " running.");
    }
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(new ReStartThread(), "TestThread");
        t.start();
        t.join(); // Wait for the thread to terminate
        System.out.println("TestThread state after join: " + t.getState());
        t.start(); // Problem line
    }
}
123 Next → Page 1 of 200 · 3994 questions