☕ Java MCQ Questions – Page 145

Questions 2881–2900 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2881 hard code output
What is the output of this code?
java
import java.util.ArrayList;
import java.util.Arrays;

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

        ArrayList<String> retainElements = new ArrayList<>();
        retainElements.add("A");
        retainElements.add(null); 

        boolean changed = list.retainAll(retainElements);

        System.out.println(list + ":" + changed);
    }
}
Q2882 medium
Given `String text = "JavaProgramming";` what is the correct way to get the substring `"Programming"`?
Q2883 easy code error
What error will occur when this code is executed?
java
public class Main {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("HelloJava");
        sb.replace(5, 2, "World");
        System.out.println(sb);
    }
}
Q2884 easy
Which keyword is used to implement inheritance in Java?
Q2885 easy code error
What type of error will occur when running this Java code?
java
public class Syncher {
    private Object syncMonitor = null; // Deliberately null

    public void doSynchronizedWork() {
        synchronized (syncMonitor) {
            System.out.println("Work done.");
        }
    }

    public static void main(String[] args) {
        Syncher s = new Syncher();
        s.doSynchronizedWork();
    }
}
Q2886 easy code error
What is the compilation error in this code?
java
class Box {
    int value = 10;
}

public class Main {
    public static void main(String[] args) {
        Box myBox = new Box();
        int x = myBox;
        System.out.println(x);
    }
}
Q2887 hard code output
What is the output of this Java code?
java
import java.util.PriorityQueue;
import java.util.Comparator;

class Item {
    int id;
    String name;
    Item(int id, String name) { this.id = id; this.name = name; }
    @Override public String toString() { return name + ":" + id; }
}

public class QueueTest {
    public static void main(String[] args) {
        PriorityQueue<Item> pq = new PriorityQueue<>(Comparator.comparingInt(item -> item.id));

        pq.offer(new Item(2, "Banana"));
        pq.offer(new Item(1, "Apple"));
        pq.offer(new Item(3, "Cherry"));
        pq.offer(new Item(1, "Date"));

        StringBuilder sb = new StringBuilder();
        while (!pq.isEmpty()) {
            sb.append(pq.poll().toString()).append(" ");
        }
        System.out.println(sb.toString().trim());
    }
}
Q2888 medium
Given a 2D array `int[][] matrix = new int[5][8];`, what do `matrix.length` and `matrix[0].length` represent, respectively?
Q2889 easy code error
Which compilation error will be thrown by the following code?
java
interface ParentProcessor {
    void process();
}

@FunctionalInterface
interface ChildProcessor extends ParentProcessor {
    void childProcess();
}

public class Main {
    public static void main(String[] args) {
        // Attempt to use ChildProcessor
    }
}
Q2890 hard
What is the most significant difference in program flow control between using a `break` statement and a `return` statement inside a `while` loop when a specific condition is met?
Q2891 medium
What is the main purpose of the `newLine()` method in `BufferedWriter`?
Q2892 medium
What is the primary purpose of the `finally` block in Java's exception handling mechanism?
Q2893 medium code output
What is the output of this Java code snippet involving nested switch statements?
java
public class SwitchTest {
    public static void main(String[] args) {
        int x = 1;
        char y = 'B';
        String result = "";
        switch (x) {
            case 1:
                switch (y) {
                    case 'A': result = "1A"; break;
                    case 'B': result = "1B"; break;
                    default: result = "1X";
                }
                break;
            case 2:
                result = "2Y";
                break;
            default:
                result = "ZZ";
        }
        System.out.println(result);
    }
}
Q2894 medium code error
What error will this Java code produce when executed?
java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> items = new ArrayList<>();
        items.add("Item1");
        Iterator<String> it = items.iterator();
        it.remove(); // Calling remove() before next()
    }
}
Q2895 easy code output
What does this Java code print?
java
public class Main {
    public static void main(String[] args) {
        int a = 20;
        int b = 5;
        if (a > 10) {
            if (b > 10) {
                System.out.println("Inner true");
            } else {
                System.out.println("Inner false");
            }
        } else {
            System.out.println("Outer false");
        }
    }
}
Q2896 hard code output
What is the output of this code?
java
class Base {
    protected Base() { System.out.print("Base ctor "); }
    protected Base(int x) { System.out.print("Base ctor " + x + " "); }
}
class Derived extends Base {
    public Derived() {
        super();
        System.out.print("Derived ctor ");
    }
    public Derived(String s) {
        super(s.length());
        System.out.print("Derived ctor " + s + " ");
    }
}
public class Main {
    public static void main(String[] args) {
        new Derived("Hello");
        new Derived();
    }
}
Q2897 medium code error
What kind of error will occur when executing this Java code?
java
import java.util.TreeSet;
import java.util.SortedSet;

public class Main {
    public static void main(String[] args) {
        TreeSet<Integer> originalSet = new TreeSet<>();
        originalSet.add(10);
        originalSet.add(20);
        originalSet.add(30);
        originalSet.add(40);

        SortedSet<Integer> subSet = originalSet.subSet(15, true, 35, false);
        subSet.add(5);
        System.out.println(subSet.size());
    }
}
Q2898 hard
You insert two distinct objects, `obj1` and `obj2`, into a `TreeMap` as keys. `obj1.equals(obj2)` returns `true`, but `obj1.compareTo(obj2)` returns a non-zero value. What will be the state of the `TreeMap`?
Q2899 easy code error
What compile-time error will occur in the `MyClass()` constructor?
java
import java.io.IOException;

class MyClass {
    MyClass() {
        throw new IOException("Error!");
    }
}
public class Main {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
    }
}
Q2900 medium
Where can the `default` case be placed within a Java `switch` statement or expression?
← Prev 143144145146147 Next → Page 145 of 200 · 3994 questions