☕ Java MCQ Questions – Page 15

Questions 281–300 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q281 hard
When resizing a `HashMap`, how are existing entries redistributed to new buckets in the larger internal array?
Q282 easy
Which of the following is a primitive data type in Java?
Q283 medium code error
What compilation error will occur when compiling the `TestClass`?
java
public class MyCustomException extends Exception {
    public MyCustomException(String message) {
        super(message);
    }
}

public class TestClass {
    public static void processData(int value) {
        if (value < 0) {
            throw new MyCustomException("Value cannot be negative.");
        }
        System.out.println("Processing: " + value);
    }

    public static void main(String[] args) {
        processData(-5);
    }
}
Q284 medium
What is the key characteristic that distinguishes a mutable object from an immutable object in Java?
Q285 easy
In which Java package is the `ArrayList` class located?
Q286 easy
Which of the following statements about the `finally` block is true?
Q287 hard
In a Java class, which of the following statements accurately describes the order of variable initialization when an instance of the class is created?
Q288 easy
If no exception occurs within a `try` block, which blocks will execute?
Q289 medium code error
What error occurs when compiling this Java code?
java
import java.io.IOException;

public class BroadExceptionCatch {
    public static void riskyOperation() throws Exception {
        System.out.println("Executing risky operation...");
        throw new Exception("Something went wrong!");
    }

    public static void main(String[] args) {
        try {
            riskyOperation();
        } catch (IOException e) { // Catching a specific exception
            System.err.println("Caught IOException: " + e.getMessage());
        }
    }
}
Q290 easy code output
What is the output of this code?
java
class StaticCounter {
    private static int count = 0;
    public static synchronized void increment() {
        count++;
        System.out.print(count);
    }
}

public class MyProgram {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> StaticCounter.increment());
        Thread t2 = new Thread(() -> StaticCounter.increment());
        t1.start();
        t2.start();
        t1.join();
        t2.join();
    }
}
Q291 medium code output
What is the output of this Java code?
java
public class TryCatchExample {
    public static void main(String[] args) {
        try {
            System.out.println("Inside try block");
            int result = 10 / 0;
            System.out.println("This won't be printed");
        } catch (ArithmeticException e) {
            System.out.println("Inside catch block: " + e.getMessage());
        }
    }
}
Q292 medium
What is the fundamental difference in the return type between the `list()` method and the `listFiles()` method of the `File` class when used on a directory?
Q293 easy code error
Identify the exception that will be thrown by the following Java code.
java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

public class IteratorError {
    public static void main(String[] args) {
        ArrayList<String> items = new ArrayList<>(Arrays.asList("First", "Second", "Third"));
        Iterator<String> it = items.iterator();
        while (it.hasNext()) {
            String item = it.next();
            if (item.equals("Second")) {
                items.remove(item); // Directly removing from the original list
            }
        }
    }
}
Q294 hard code error
What is the compile-time error in this Java code snippet?
java
public class DuplicateCaseLabelError {
    public static void main(String[] args) {
        final int STATUS_ACTIVE = 1;
        final int STATUS_OPEN = 1;
        int status = 1;
        String message = "";
        switch (status) { // Error expected here
            case STATUS_ACTIVE:
                message = "Active";
                break;
            case STATUS_OPEN:
                message = "Open";
                break;
            default:
                message = "Unknown";
        }
        System.out.println(message);
    }
}
Q295 easy code output
What is the output of the following Java code?
java
public class Task {
    private String description;

    public void setDescription(String description) {
        this.description = description.trim(); // Trim whitespace
    }

    public String getDescription() {
        return description;
    }

    public static void main(String[] args) {
        Task task = new Task();
        task.setDescription("  Buy Groceries  ");
        System.out.println("[" + task.getDescription() + "]");
    }
}
Q296 easy code error
What compilation error will occur when compiling this Java code?
java
import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;

public class MyClass {
    public static void main(String[] args) {
        BufferedReader br;
        try (br = new BufferedReader(new StringReader("test"))) {
            String line = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Q297 medium
If an exception occurs within the `try` block and is successfully caught by a matching `catch` block, what is the execution order?
Q298 hard code error
What compile-time error occurs when attempting to compile this Java code?
java
class SuperClass {
    public static void greet() {
        System.out.println("Hello from SuperClass!");
    }
}

class SubClass extends SuperClass {
    @Override
    public static void greet() {
        System.out.println("Hello from SubClass!");
    }
}
Q299 medium code error
What is the reason for the compilation failure in the following Java code?
java
public class Test {
    public static void main(String[] args) {
        myOuterLabel:;
        for (int i = 0; i < 3; i++) {
            if (i == 1) {
                continue myOuterLabel;
            }
            System.out.print(i);
        }
    }
}
Q300 easy
How are multi-dimensional arrays in Java fundamentally structured?
← Prev 1314151617 Next → Page 15 of 200 · 3994 questions