☕ Java MCQ Questions – Page 116

Questions 2301–2320 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2301 easy
Which of the following statements about the `throw` keyword is true?
Q2302 easy
What is a key advantage of using the `Runnable` interface over extending the `Thread` class for creating threads?
Q2303 medium
What is the default initial value for elements in an array of `String` objects when it is first created, e.g., `String[] names = new String[5];`?
Q2304 hard
In a `try-with-resources` statement, if an exception occurs within the `try` block, and another exception occurs during the implicit closing of a resource, how are these exceptions typically handled?
Q2305 hard
Given the limitation of creating generic arrays directly, what is the primary role of `java.lang.reflect.Array.newInstance(Class<?> componentType, int length)`?
Q2306 hard code error
What is the runtime error when executing this Java code snippet?
java
import java.util.TreeSet;
import java.util.Comparator;

class CustomKey {
    int id;
    String name;
    CustomKey(int id, String name) { this.id = id; this.name = name; }
}

public class Main {
    public static void main(String[] args) {
        TreeSet rawSet = new TreeSet(); // Using raw type
        rawSet.add(new CustomKey(1, "One"));
        rawSet.add("Two"); // Adding a String, which will be ignored by the error
        rawSet.add(new CustomKey(3, "Three"));
        System.out.println(rawSet.size());
    }
}
Q2307 hard
Consider `int[] original = {10, 20, 30};`. What happens when `Arrays.copyOfRange(original, 1, 5)` is executed?
Q2308 easy code output
What is the output of this code?
java
abstract class Shape {
    abstract void draw();
}

public class Test {
    public static void main(String[] args) {
        Shape s = new Shape(); // This line will cause a compile-time error
        System.out.println("Program continues.");
    }
}
Q2309 medium
Consider the `createNewFile()` method of the `File` class. If the file specified by the `File` object already exists, what is the behavior of this method?
Q2310 hard code output
What is the output of this code?
java
abstract class Shape {
    abstract void draw();
}

class Circle extends Shape {
    @Override
    void draw() {
        System.out.println("Drawing Circle");
    }
}

class Square extends Shape {
    // No draw() method implemented
}

public class Main {
    public static void main(String[] args) {
        // Shape s = new Circle(); // This would work
        // s.draw();
        Shape s2 = new Square(); // This line
    }
}
Q2311 medium
Can `java.util.LinkedList` store `null` elements?
Q2312 medium code error
What compilation error will this Java code produce? (Assume Java 8 environment)
java
public class SwitchError {
    public static void main(String[] args) {
        String data = null;
        switch (data) {
            case "test":
                System.out.println("Test");
                break;
            case null:
                System.out.println("Null data");
                break;
            default:
                System.out.println("Other");
        }
    }
}
Q2313 medium code error
What error does this Java code produce when executed?
java
public class Main {
    public static void main(String[] args) {
        String s1 = null;
        String s2 = "test";
        System.out.println(s1.equals(s2));
    }
}
Q2314 hard code error
What compile-time error will occur in this Java record definition with a compact constructor?
java
record Product(String name, double price) {
    Product {
        // Compact constructor implicitly assigns name and price.
        // Explicit assignment here is redundant and causes an error.
        this.name = name.trim();
        if (price < 0) {
            throw new IllegalArgumentException("Price cannot be negative");
        }
    }
}
Q2315 medium
Is it possible to overload constructors in Java?
Q2316 medium code output
What does this code print?
java
interface MyFunction {
    void apply();
}

public class LambdaVariableCapture {
    public static void main(String[] args) {
        int x = 10;
        MyFunction func = () -> System.out.println(x * 2);
        // x = 20; // Uncommenting this line would cause a compile error
        func.apply();
    }
}
Q2317 hard code output
What does this code print?
java
import java.util.LinkedList;

public class Test {
    static class MyObject {
        int id;
        MyObject(int id) { this.id = id; }
    }
    public static void main(String[] args) {
        LinkedList<Object> list = new LinkedList<>();
        list.add(null);
        list.add(new MyObject(1));
        list.add(new MyObject(2));
        System.out.print(list.contains(null) + " ");
        System.out.print(list.contains(new MyObject(1)) + " ");
        System.out.print(list.contains(new MyObject(3)));
    }
}
Q2318 medium code error
What will be the compilation error in this code?
java
class Super {
    public void display() {
        System.out.println("Super display");
    }
}

class Sub extends Super {
    @Override
    protected void display() {
        System.out.println("Sub display");
    }
}
Q2319 easy
Which of the following statements about `case` labels in a Java `switch` statement is true?
Q2320 easy code error
What is the compilation or runtime error in the following Java code?
java
public class Main {
    public static void main(String[] args) {
        String greeting = "Hello";
        greeting[0] = 'J';
        System.out.println(greeting);
    }
}
← Prev 114115116117118 Next → Page 116 of 200 · 3994 questions