☕ Java MCQ Questions – Page 4

Questions 61–80 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q61 easy code output
What does this code print to the console?
java
class Car {
    String model;
    int year;
}
public class Main {
    public static void main(String[] args) {
        Car myCar = new Car();
        myCar.model = "Sedan";
        myCar.year = 2023;
        System.out.print("Model: " + myCar.model + ", Year: " + myCar.year);
    }
}
Q62 easy code error
What error will occur when compiling the following Java code?
java
import java.util.HashSet;

public class MyClass {
    public static void main(String[] args) {
        HashSet<String> names = new HashSet<>();
        names.add("Alice");
        names.add(123); // This line will cause an error
        System.out.println(names.size());
    }
}
Q63 hard code error
What error will occur when `MyRecursiveTask` is executed with a very deep recursion level?
java
public class MyRecursiveTask {
    private static final Object lock = new Object();
    private int counter = 0;

    public synchronized void recursiveCall(int depth) {
        if (depth == 0) {
            return;
        }
        counter++;
        recursiveCall(depth - 1);
    }

    public static void main(String[] args) {
        MyRecursiveTask task = new MyRecursiveTask();
        task.recursiveCall(100000); // A very deep recursion
    }
}
Q64 easy code error
Consider the Java code. What will be the outcome of its compilation?
java
class DataProcessor {
    final void processData(byte[] data) {
        System.out.println("Final processing...");
    }

    void processData(byte[] data) {
        System.out.println("General processing...");
    }
}
Q65 easy code output
What is the output of this Java code snippet?
java
public class WhileLoopDemo {
    public static void main(String[] args) {
        int i = 0;
        while (i < 3) {
            System.out.print(i);
            i++;
        }
    }
}
Q66 medium
Given an object `myString` of type `String`, which method reference syntax correctly refers to its `length()` method?
Q67 hard
Which of the following is a primary advantage of implementing the `Externalizable` interface over `Serializable` for a complex object with many fields?
Q68 easy code error
What compile-time error will occur when compiling this Java code?
java
class MyCustomException {
    public MyCustomException(String message) {
        // This class does not extend Throwable
    }
}

public class Main {
    public static void main(String[] args) {
        try {
            throw new MyCustomException("Something went wrong");
        } catch (MyCustomException e) {
            System.out.println(e.getMessage());
        }
    }
}
Q69 hard
`String` objects are commonly used as keys in `HashMap`. Which statement accurately describes `String`'s `hashCode()` implementation and its implication for `HashMap` performance?
Q70 medium code error
What kind of error will occur when executing this Java code?
java
import java.util.Comparator;
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        Comparator<String> caseInsensitiveComparator = (s1, s2) -> s1.toLowerCase().compareTo(s2.toLowerCase());
        TreeSet<String> set = new TreeSet<>(caseInsensitiveComparator);
        set.add("Apple");
        set.add("Banana");
        set.add(null);
        System.out.println(set.size());
    }
}
Q71 hard
Given `int x = 0; int y = 5;`, what is the final value of `x` after `boolean condition = (x++ > 0 && y++ < 10);`?
Q72 hard
Which of the following statements correctly initializes a jagged multi-dimensional array in Java?
Q73 medium
Besides implementing the `List` interface, `java.util.LinkedList` also implements which other primary interface, making it suitable for queue and stack operations?
Q74 hard code error
What is the output of this code?
java
import java.util.LinkedList;

public class MyClass {
    public static void main(String[] args) {
        LinkedList<Integer> stack = new LinkedList<>();
        stack.push(10);
        stack.pop(); // Removes 10
        stack.pop(); // Attempt to pop from empty stack
        System.out.println(stack);
    }
}
Q75 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 MyTask implements Runnable {
    public void run() {
        try {
            this.wait(); // Problem line
        } catch (InterruptedException | IllegalMonitorStateException e) {
            System.out.println("Caught: " + e.getClass().getSimpleName());
        }
    }
    public static void main(String[] args) {
        Thread t = new Thread(new MyTask());
        t.start();
    }
}
Q76 easy code output
What does this code print?
java
public class Main {
    public static String testMethod() {
        try {
            int x = 1 / 0; // Throws ArithmeticException
            System.out.print("Try ");
        } catch (ArithmeticException e) {
            System.out.print("Catch ");
            return "Return from catch";
        } finally {
            System.out.print("Finally ");
        }
        return "End of method";
    }
    public static void main(String[] args) {
        System.out.println(testMethod());
    }
}
Q77 medium code error
What will this Java code snippet print to the console?
java
import java.util.HashMap;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
        try {
            Map<String, String> myMap = new HashMap<>(-5); // Negative initial capacity
            myMap.put("test", "value");
            System.out.println("Map created successfully.");
        } catch (Exception e) {
            System.out.println(e.getClass().getSimpleName());
        }
    }
}
Q78 medium code error
Examine the Java code. What error will occur when it runs?
java
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        TreeMap<Integer, String> map = new TreeMap<>();
        map.put(10, "Ten");
        map.put(20, "Twenty");
        map.remove(null);
        System.out.println(map.size());
    }
}
Q79 medium code output
What is the output of this code snippet?
java
interface MathOperation {
    int operate(int a, int b);
}

public class LambdaWithReturn {
    public static void main(String[] args) {
        MathOperation addition = (a, b) -> a + b;
        MathOperation subtraction = (a, b) -> a - b;

        System.out.println(addition.operate(5, 3));
        System.out.println(subtraction.operate(5, 3));
    }
}
Q80 hard code error
What is the most likely error when executing this Java code snippet?
java
import java.io.BufferedWriter;
import java.io.StringWriter;
import java.io.IOException;

public class Test {
    public static void main(String[] args) {
        StringWriter sw = new StringWriter();
        BufferedWriter bw = new BufferedWriter(sw);
        try (bw) { // Using try-with-resources on an existing variable
            bw.write("First line.");
        } catch (IOException e) {
            System.err.println(e.getMessage());
        }
        try {
            bw.write("Attempt to write after close."); // This will throw
        } catch (IOException e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
}
← Prev 23456 Next → Page 4 of 200 · 3994 questions