☕ Java MCQ Questions – Page 133

Questions 2641–2660 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2641 medium
Is `java.util.TreeMap` inherently thread-safe?
Q2642 hard code output
What is the output of the following Java program, focusing on thread interruption?
java
public class InterruptionRunnable {
    public static void main(String[] args) throws InterruptedException {
        Runnable interruptibleTask = () -> {
            try {
                System.out.println("Task started.");
                Thread.sleep(1000); // Will be interrupted
                System.out.println("Task finished normally.");
            } catch (InterruptedException e) {
                System.out.println("Task interrupted. Flag state: " + Thread.currentThread().isInterrupted());
            }
        };

        Thread worker = new Thread(interruptibleTask, "Worker");
        worker.start();

        Thread.sleep(100); // Give task a chance to start
        worker.interrupt();
        worker.join();

        System.out.println("Main thread done.");
    }
}
Q2643 easy
Which method is used by a `Collection` to return an `Iterator` instance?
Q2644 easy code error
What is the error in the following Java code snippet?
java
public class ArrayError {
    public static void main(String[] args) {
        int size = 5.0;
        int[] arr = new int[size];
    }
}
Q2645 hard code output
What is the output of this code?
java
import java.util.LinkedList;
import java.util.ListIterator;

public class Test {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("X");
        list.add("Y");
        list.add("Z");
        ListIterator<String> it = list.listIterator();
        it.next(); // Passes 'X'
        it.add("A"); // Adds 'A' before 'Y'
        it.next(); // Passes 'Y'
        it.remove(); // Removes 'Y'
        it.next(); // Passes 'Z'
        it.set("W"); // Replaces 'Z' with 'W'
        System.out.println(list);
    }
}
Q2646 medium code output
What will be printed to the console when this code runs?
java
import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        ArrayList<Double> prices = new ArrayList<>();
        prices.add(10.5);
        prices.add(20.0);
        prices.set(0, 15.0);
        System.out.println(prices);
    }
}
Q2647 hard code output
What is the output of this code?
java
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;

public class Test {
    public static void main(String[] args) throws IOException {
        StringWriter sw = new StringWriter();
        BufferedWriter bw = new BufferedWriter(sw, 2); // Buffer size of 2 characters
        bw.write('A');
        bw.write('B');
        bw.write('C');
        bw.close();
        System.out.println(sw.toString());
    }
}
Q2648 easy code error
What compilation error will occur in the following Java code?
java
class Product {
    public abstract void getDescription();
}
Q2649 medium code error
What is the result of attempting to run the following Java code?
java
public class Main {
    public static void main(String[] args) {
        Thread t = new Thread(() -> System.out.println("Thread running"));
        try {
            t.setPriority(100); // This line
            t.start();
        } catch (IllegalArgumentException e) {
            System.err.println("Caught error: " + e.getMessage());
        }
    }
}
Q2650 medium
Which statement correctly describes the allowance of null keys and null values in a `java.util.HashMap`?
Q2651 medium
What is the primary purpose of encapsulation in Java?
Q2652 hard code output
What does this code print?
java
public class IntegerArrayNullPointerException {
    public static void main(String[] args) {
        Integer[] nums = new Integer[3];
        nums[0] = 10;
        nums[2] = 20;
        int sum = 0;
        try {
            for (Integer num : nums) {
                sum += num;
            }
        } catch (Exception e) {
            System.out.println(e.getClass().getSimpleName());
            return;
        }
        System.out.println(sum);
    }
}
Q2653 hard
When considering the implementation of `StringBuffer`, which statement accurately describes how its thread safety is generally maintained for operations like `append` or `insert`?
Q2654 medium code output
What is the output of this code?
java
import java.util.LinkedList;
import java.util.Queue;

public class QueueIsNull {
    public static void main(String[] args) {
        Queue<String> queue = new LinkedList<>();
        queue.offer("One");
        queue.poll();
        System.out.println(queue.peek());
        System.out.println(queue.poll());
    }
}
Q2655 medium code output
What is the output of the following Java code?
java
public class StringBufferTest {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("racecar");
        sb.reverse();
        System.out.print(sb.equals("racecar") + " " + (sb.toString().equals("racecar")));
    }
}
Q2656 medium code error
What is the result of attempting to compile and run this Java code with the given `execute` method calls?
java
public class OverloadChecker {
    public void execute(int a, double b) {
        System.out.println("int, double: " + a + ", " + b);
    }

    public void execute(double a, int b) {
        System.out.println("double, int: " + a + ", " + b);
    }

    public static void main(String[] args) {
        OverloadChecker oc = new OverloadChecker();
        oc.execute(10, 20); // This line causes the error
    }
}
Q2657 medium
What is a main advantage of implementing `Runnable` over extending the `Thread` class when creating a multithreaded application?
Q2658 medium code error
What kind of error will occur when running this Java code?
java
public class ArrayError {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30};
        for (int i = 0; i <= numbers.length; i++) {
            System.out.println(numbers[i]);
        }
    }
}
Q2659 easy
Once a single-dimensional array is created in Java, what can be said about its size?
Q2660 easy
Which of the following is NOT a valid scenario for method overloading?
← Prev 131132133134135 Next → Page 133 of 200 · 3994 questions