☕ Java MCQ Questions – Page 26

Questions 501–520 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q501 easy
After calling the `start()` method on a `Thread` object, what is the immediate state it enters, waiting to be scheduled by the OS?
Q502 easy
Which method of the `java.io.File` class would you use to confirm if the `File` object represents a regular file?
Q503 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<Integer> numbers = new ArrayList<>(List.of(10, 20, 30, 40));
        Iterator<Integer> it = numbers.iterator();
        while (it.hasNext()) {
            Integer num = it.next();
            if (num == 20 || num == 40) {
                it.remove();
            }
        }
        System.out.println(numbers);
    }
}
Q504 easy code error
What compilation error will occur in the following Java code?
java
class Account {
    private double balance = 100.0;
    // No public setter method for 'balance'
}

public class Bank {
    public static void main(String[] args) {
        Account userAccount = new Account();
        userAccount.setBalance(500.0);
        System.out.println(userAccount.balance);
    }
}
Q505 medium code output
What will be the output when this Java program runs?
java
public class ExceptionInFinally {
    public static void main(String[] args) {
        try {
            System.out.println("In try");
            throw new RuntimeException("From try");
        } catch (RuntimeException e) {
            System.out.println("In catch: " + e.getMessage());
            // No re-throw, but it's caught
        } finally {
            System.out.println("In finally");
            throw new Error("From finally"); // New unchecked error
        }
    }
}
Q506 hard code output
What does this code print?
java
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.BinaryOperator;

public class ReduceOperation {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4);
        BinaryOperator<Integer> sum = (a, b) -> {
            System.out.println("Accumulating: " + a + " + " + b);
            return a + b;
        };

        int result1 = numbers.stream().reduce(0, sum);
        System.out.println("Result with identity 0: " + result1);

        Optional<Integer> result2 = numbers.stream().reduce(sum);
        System.out.println("Result without identity: " + result2.orElse(-1));

        List<Integer> emptyList = Arrays.asList();
        int result3 = emptyList.stream().reduce(100, sum);
        System.out.println("Result with empty list and identity 100: " + result3);

        Optional<Integer> result4 = emptyList.stream().reduce(sum);
        System.out.println("Result with empty list without identity: " + result4.orElse(-1));
    }
}
Q507 easy code output
What does this Java code print?
java
import java.util.ArrayList;
import java.util.List;

public class FinalMutableTest {
    public static void main(String[] args) {
        final List<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        System.out.println(fruits.size());
    }
}
Q508 hard code output
What is the output of this code? (Assuming Java 17+)
java
public class SwitchTest {
    sealed interface Shape permits Circle, Square {}
    record Circle(double radius) implements Shape {}
    record Square(double side) implements Shape {}

    public static void main(String[] args) {
        Shape shape = new Circle(10.0);
        String description = switch (shape) {
            case Circle c -> "Circle with radius " + c.radius();
            // Missing Square case, and no default.
        };
        System.out.println(description);
    }
}
Q509 hard
Given a `HashMap<String, Integer> map = new HashMap<>();` and an `Iterator<Map.Entry<String, Integer>> entryIt = map.entrySet().iterator();`. If `map.put("A", 1); map.put("B", 2);`, and then `entryIt` is obtained and `entryIt.next();` is called, followed by `map.remove("B");` externally, what is the most likely outcome upon a subsequent call to `entryIt.hasNext()` or `entryIt.next()`?
Q510 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 = new StringBuilder("Java");
        sb.concat("World");
        System.out.println(sb);
    }
}
Q511 easy code error
What is the result of running this Java code snippet?
java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

public class IteratorError {
    public static void main(String[] args) {
        ArrayList<String> data = new ArrayList<>(Arrays.asList("One", "Two"));
        Iterator<String> it = data.iterator();
        data.clear(); // Modifying the list after iterator creation
        it.next(); // Attempting to use the invalidated iterator
    }
}
Q512 easy code output
What is the output of this code?
java
public class StringTest {
    public static void main(String[] args) {
        String s1 = "java";
        String s2 = "java";
        System.out.println(s1.equals(s2));
    }
}
Q513 hard
Consider a `try-catch-finally` block. If an exception is thrown in the `try` block, and another unrelated exception is thrown in the `finally` block, which exception will be propagated out of the method?
Q514 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 name = null;
        int len = name.length();
        System.out.println(len);
    }
}
Q515 medium
What is the purpose of the `yield` keyword when used within a `switch` expression in Java?
Q516 hard code error
What kind of error will occur when compiling or running the following Java code?
java
public class SBError {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Programming"); // Length 11
        char[] destArray = new char[5]; // Can hold 5 chars
        // Attempt to copy 7 chars (from 0 to 7) into destArray starting at index 0
        sb.getChars(0, 7, destArray, 0);
        System.out.println(new String(destArray));
    }
}
Q517 easy code output
What will be printed by this Java program?
java
public class MyClass {
    public static void main(String[] args) {
        int month = 2;
        String season;
        switch (month) {
            case 1:
            case 2:
            case 12: season = "Winter"; break;
            case 3:
            case 4:
            case 5: season = "Spring"; break;
            default: season = "Unknown";
        }
        System.out.print(season);
    }
}
Q518 hard code output
What is the output of this Java code?
java
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.nio.file.Files;
import java.nio.file.Path;

public class PrintWriterAutoFlush {
    public static void main(String[] args) {
        String filename = "autoflush_test.txt";
        try {
            try (PrintWriter pw = new PrintWriter(new FileWriter(filename))) {
                pw.print("First line.");
            }
            try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
                System.out.println("After first PrintWriter: " + br.readLine());
            }

            try (PrintWriter pw = new PrintWriter(new FileWriter(filename, true), true)) {
                pw.println("Second line.");
                pw.print("Third line.");
            }
            try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
                String line; while((line = br.readLine()) != null) { System.out.println("After second PrintWriter: " + line); }
            }
        } catch (IOException e) { System.out.println("Error: " + e.getMessage());
        } finally { try { Files.deleteIfExists(Path.of(filename)); } catch (IOException ignored) {} }
    }
}
Q519 hard code output
What is the output of this code?
java
public class StringIndexOfEmptyTest {
    public static void main(String[] args) {
        String text = "abcde";
        System.out.println(text.indexOf("") + "," + text.indexOf("", 3) + "," + text.lastIndexOf("", 2));
    }
}
Q520 easy
What is the primary characteristic of a `java.util.HashSet` regarding its elements?
← Prev 2425262728 Next → Page 26 of 200 · 3994 questions