☕ Java MCQ Questions – Page 2

Questions 21–40 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q21 hard
What is a common characteristic of a Java deserialization vulnerability (e.g., using 'gadget chains')?
Q22 medium code output
What does this 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<String> letters = new ArrayList<>(List.of("X", "Y", "Z"));
        Iterator<String> it = letters.iterator();
        try {
            it.next(); // Advances to X
            it.remove(); // Removes X
            it.next(); // Advances to Y
            it.remove(); // Removes Y
            it.remove(); // This is the problematic call
        } catch (IllegalStateException e) {
            System.out.println("Error: " + e.getMessage());
        }
        System.out.println(letters);
    }
}
Q23 easy code output
What is the output of this code?
java
import java.util.HashSet;

public class Test {
    public static void main(String[] args) {
        HashSet<String> items = new HashSet<>();
        items.add("Pen");
        items.add("Book");
        items.remove("Pen");
        items.add("Pencil");
        items.add("Book"); // Duplicate
        System.out.println(items.size());
    }
}
Q24 hard code error
What compilation error will occur in the provided Java code?
java
public class FinalVarReassign {
    public static void main(String[] args) {
        final int counter = 0;
        // Attempt to reassign a final variable
        counter = 1;
        System.out.println(counter);
    }
}
Q25 medium code output
What does this code print?
java
import java.util.LinkedList;

public class Test {
    public static void main(String[] args) {
        LinkedList<Integer> stack = new LinkedList<>();
        stack.push(1);
        stack.push(2);
        stack.add(3); 
        System.out.print(stack.pop() + " ");
        System.out.println(stack.peek());
    }
}
Q26 hard code output
What is the output of this code?
java
public class StringMatchesTest {
    public static void main(String[] args) {
        String s1 = "a1b2c3";
        String s2 = "a1b23";
        System.out.println(s1.matches("([a-z]\\d)+") + ", " + s2.matches("([a-z]\\d)+"));
    }
}
Q27 easy code error
What is the compilation error in the given Java code?
java
import java.util.function.Function;

@FunctionalInterface
interface Greeter {
    String greet(String name);
}

public class Main {
    public static void main(String[] args) {
        Greeter greeter = new Greeter();
        System.out.println(greeter.greet("World"));
    }
}
Q28 medium code output
What will be printed when this Java code is executed?
java
class StaticParent {
    static void greet() {
        System.out.println("Parent's static greet");
    }
}

class StaticChild extends StaticParent {
    static void greet() { // Method hiding, not overriding
        System.out.println("Child's static greet");
    }
}

public class Main {
    public static void main(String[] args) {
        StaticParent p = new StaticChild();
        p.greet();
        StaticChild c = new StaticChild();
        c.greet();
    }
}
Q29 medium code error
What is the outcome when this Java code is executed?
java
import java.util.LinkedList;
import java.util.Iterator;

public class Test {
    public static void main(String[] args) {
        LinkedList<String> items = new LinkedList<>();
        items.add("Item1");
        items.add("Item2");
        for (String item : items) {
            items.remove(item);
        }
        System.out.println(items.size());
    }
}
Q30 easy
What happens if you call `remove()` on an empty `Queue` in Java?
Q31 medium code output
What is the output of the following Java code snippet?
java
import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;

public class BufferedReaderTest {
    public static void main(String[] args) {
        String data = "ABCD\nEFG";
        try (BufferedReader br = new BufferedReader(new StringReader(data))) {
            System.out.print((char) br.read());
            System.out.print(br.readLine());
            System.out.print((char) br.read());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Q32 easy
What is the primary role of a 'setter' method in an encapsulated class?
Q33 hard
You are using `Stream.forEach()` and the lambda you want to pass needs to perform an operation that can throw a checked exception (e.g., `IOException`). Which statement accurately describes the required handling?
Q34 easy
When might a do-while loop be particularly useful compared to a while loop?
Q35 medium code error
What error does this Java code produce when executed?
java
public class Main {
    public static void main(String[] args) {
        String phrase = "Hello";
        System.out.println(phrase.substring(2, 6));
    }
}
Q36 medium
When a subclass defines a static method with the same signature as a static method in its superclass, what is this called?
Q37 medium
What is the primary advantage of using a method reference over an equivalent lambda expression when applicable?
Q38 hard
Which access modifier is *not* permitted for an `abstract` method in Java, and why?
Q39 hard code output
What does this code print?
java
public class MultiArrayQ2 {
    public static void main(String[] args) {
        int[][] arr1 = new int[2][2];
        int[][] arr2 = new int[2][2];
        arr1[0] = arr2[1];
        arr2[1][0] = 99;
        System.out.println(arr1[0][0]);
    }
}
Q40 hard
What is the primary implication of declaring a method as `final` in a superclass with respect to inheritance?
← Prev 1234 Next → Page 2 of 200 · 3994 questions