☕ Java MCQ Questions – Page 134

Questions 2661–2680 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2661 hard
Which statement accurately describes the guarantees and limitations of `java.io.File.deleteOnExit()`?
Q2662 hard
Evaluate the Java expression: `5 + 2 * 3 ^ 4 | 1`. Assume `^` is bitwise XOR and `|` is bitwise OR.
Q2663 easy
When a thread enters a `synchronized` block or method, what type of lock does it acquire?
Q2664 medium
Does changing only the access modifier (e.g., from `public` to `private`) allow you to overload a method in Java?
Q2665 medium code output
What is the output of this Java code?
java
import java.util.TreeSet;
import java.util.Comparator;

public class Test {
    public static void main(String[] args) {
        TreeSet<Integer> numbers = new TreeSet<>(Comparator.reverseOrder());
        numbers.add(5);
        numbers.add(2);
        numbers.add(8);
        numbers.add(1);
        System.out.println(numbers);
    }
}
Q2666 hard
What is the primary use case for calling `ObjectOutputStream.enableReplaceObject()` and overriding `replaceObject()` in a subclass of `ObjectOutputStream`?
Q2667 easy code error
What kind of error will occur when compiling the following Java code snippet?
java
import java.io.File;

public class FileError7 {
    public static void main(String[] args) {
        File file = new File(123);
        System.out.println(file.getPath());
    }
}
Q2668 easy
Consider a `for` loop structured as `for (int i = 0; i < N; i++)`. If `N` has a positive integer value, how many times will the loop's body be executed?
Q2669 easy
What happens if an exception is thrown in a `try` block, but there is no `catch` block that can handle that specific exception type?
Q2670 easy code error
What is the compile-time error in the following Java code?
java
public class DataTypeBigIntError {
    public static void main(String[] args) {
        int hugeNumber = 3000000000;
        System.out.println(hugeNumber);
    }
}
Q2671 medium
What is a significant characteristic of the `LocalDate` and `LocalDateTime` classes introduced in Java 8's Date and Time API?
Q2672 hard code output
What is the output of this Java code?
java
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.io.FileReader;
import java.io.BufferedReader;

public class FileWriterEmptyWrite {
    public static void main(String[] args) {
        String filename = "empty_write.txt";
        try {
            try (FileWriter fw = new FileWriter(filename)) {
                fw.write("");
            }
            long fileSize = Files.size(Path.of(filename));
            System.out.println("File exists: " + Files.exists(Path.of(filename)));
            System.out.println("File size: " + fileSize);

            try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
                String line = br.readLine();
                System.out.println("Content: '" + (line != null ? line : "") + "'");
            }
        } catch (IOException e) { System.out.println("Error: " + e.getMessage());
        } finally { try { Files.deleteIfExists(Path.of(filename)); } catch (IOException ignored) {} }
    }
}
Q2673 easy
Which operator is used to calculate the remainder of a division in Java?
Q2674 easy code output
What is the output of this code?
java
public class EmptyTask implements Runnable {
    @Override
    public void run() {
        // This run method is empty
    }

    public static void main(String[] args) {
        System.out.println("Starting task...");
        Thread thread = new Thread(new EmptyTask());
        thread.start();
        System.out.println("Task started.");
    }
}
Q2675 medium code error
What is the error in the following Java code?
java
package com.example;

class BankAccount {
    private double balance;

    public BankAccount(double initialBalance) {
        this.balance = initialBalance;
    }
}

public class Main {
    public static void main(String[] args) {
        BankAccount account = new BankAccount(1000.0);
        System.out.println(account.balance);
    }
}
Q2676 hard code error
What is the result of running this Java code?
java
import java.util.Comparator;
import java.util.TreeMap;

public class TreeMapError5 {
    public static void main(String[] args) {
        Comparator<String> lenComparator = new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                return Integer.compare(s1.length(), s2.length());
            }
        };

        TreeMap<String, Integer> map = new TreeMap<>(lenComparator);
        map.put("hello", 5);
        map.put("world", 5);
        map.put(null, 0); 
        System.out.println(map.size());
    }
}
Q2677 hard code error
What compilation error will occur when compiling this Java code?
java
public class NonExistentLabelBreak {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            if (i == 2) {
                break myMissingLabel;
            }
            System.out.println(i);
        }
    }
}
Q2678 easy
Which keyword is used in Java to implement synchronization?
Q2679 hard code output
What is the output of this code?
java
import java.io.*;

public class Test {
    public static void main(String[] args) throws IOException {
        String data = "A\nB\nC";
        BufferedReader br = new BufferedReader(new StringReader(data));
        System.out.print((char)br.read()); // A
        br.readLine(); // Consumes \n and 'B\n'
        System.out.print((char)br.read()); // C
        System.out.println(br.readLine()); // null (after C and EOF)
        br.close();
    }
}
Q2680 hard
In Java versions prior to Java 7, without `try-with-resources`, what was considered the most robust pattern for ensuring a `BufferedWriter` was closed in a `finally` block, especially if a `write` operation also threw an `IOException`?
← Prev 132133134135136 Next → Page 134 of 200 · 3994 questions