☕ Java MCQ Questions – Page 132

Questions 2621–2640 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2621 hard
Under what conditions can a Java lambda expression be serialized?
Q2622 hard
When overriding a method, what is the rule regarding checked exceptions thrown by the overridden method in the subclass?
Q2623 hard code output
What is the output of this code?
java
import java.util.ArrayList;
import java.util.Iterator;

public class Test {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("A");
        list.add("B");
        list.add("C");

        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            String s = it.next();
            if (s.equals("B")) {
                list.remove("C"); 
            }
        }
        System.out.println(list);
    }
}
Q2624 hard code output
Given the following Java code, what will be the output?
java
public class DaemonRunnable {
    public static void main(String[] args) throws InterruptedException {
        Runnable daemonTask = () -> {
            while (true) {
                try {
                    System.out.println("Daemon thread is running...");
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    System.out.println("Daemon thread interrupted.");
                    break;
                }
            }
        };

        Thread daemonThread = new Thread(daemonTask, "MyDaemonThread");
        daemonThread.setDaemon(true);
        daemonThread.start();

        Thread.sleep(500); // Main thread sleeps for a bit
        System.out.println("Main thread finishing.");
    }
}
Q2625 medium code output
What is the output of this Java code?
java
import java.util.function.Function;

class Item {
    String id;
    Item(String id) {
        this.id = id;
    }
    public String getId() { return id; }
}

public class MethodRefTest {
    public static void main(String[] args) {
        Function<String, Item> itemFactory = Item::new;
        Item item1 = itemFactory.apply("SKU123");
        Item item2 = itemFactory.apply("SKU456");
        System.out.println(item1.getId() + " - " + item2.getId());
    }
}
Q2626 hard code output
What is the output of this code, assuming System.getProperty("line.separator") returns "\n"?
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);
        bw.write("Line 1");
        bw.newLine();
        bw.write("Line 2\n");
        bw.write("Line 3");
        bw.close();
        System.out.println(sw.toString().replace("\n", "\\n").replace("\r", "\\r"));
    }
}
Q2627 easy code output
What does the following Java code print to the console?
java
public class WhileLoopDemo {
    public static void main(String[] args) {
        boolean running = true;
        int counter = 0;
        while (running) {
            System.out.print(counter);
            counter++;
            if (counter == 2) {
                running = false;
            }
        }
    }
}
Q2628 hard code error
What compile-time error will this Java code produce?
java
public class OperatorError1 {
    public static void main(String[] args) {
        short s = 10;
        s = s + 5; // Error line
        System.out.println(s);
    }
}
Q2629 hard
What is the most accurate distinction between data hiding and encapsulation in object-oriented programming?
Q2630 easy code error
What is the output or error of the following Java code?
java
public class ExceptionTest {
    public static void main(String[] args) {
        try {
            System.out.println("In try block.");
        } finally {
            throw new IllegalArgumentException("Error in finally");
        }
        System.out.println("After finally");
    }
}
Q2631 easy code output
What does this Java code print?
java
public class ArrayTest {
    public static void main(String[] args) {
        int[] values = new int[4];
        System.out.println(values[0]);
    }
}
Q2632 medium
Which type of variable in Java has no default initial value and must be explicitly assigned a value before use?
Q2633 easy
Given `int[][] table = new int[3][5];`, what will `table[0].length` return?
Q2634 hard
Why is it generally not possible to directly create an array of a generic type `T` using `new T[size]` in Java (e.g., `T[] array = new T[10];`)?
Q2635 hard code output
What is the output of this code?
java
public class BitwiseWhile {
    public static void main(String[] args) {
        int n = 10; 
        int count = 0;
        while (n > 0) {
            if ((n & 1) == 1) {
                count++;
            }
            n >>= 1; 
        }
        System.out.println(count);
    }
}
Q2636 easy code output
What is printed by this Java code snippet?
java
public class Main {
    public static void main(String[] args) {
        int counter = 0;
        do {
            System.out.print(counter);
            counter++;
            if (counter == 3) {
                break;
            }
        } while (counter < 5);
    }
}
Q2637 medium
Why would you choose to declare a class as `abstract` even if it doesn't contain any abstract methods?
Q2638 medium code error
What type of error will occur when compiling the following Java code?
java
public class Main {
    public static void main(String[] args) {
        char c = "A";
        System.out.println(c);
    }
}
Q2639 easy code error
What is the error in this Java code?
java
import java.util.HashMap;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
        Map<String, String> myMap = new HashMap<>();
        myMap.put("greeting", "Hello");
        Integer value = myMap.get("greeting");
        System.out.println(value);
    }
}
Q2640 easy code output
What is the output of this code snippet?
java
public class MyClass {
    public static void main(String[] args) {
        byte b = 1;
        String text = "";
        switch (b) {
            case 0: text = "Zero"; break;
            case 1: text = "One"; break;
            case 2: text = "Two"; break;
            default: text = "Other";
        }
        System.out.print(text);
    }
}
← Prev 130131132133134 Next → Page 132 of 200 · 3994 questions