☕ Java MCQ Questions – Page 192

Questions 3821–3840 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3821 medium code error
What error occurs when compiling this Java code?
java
public class MyClass {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Hello");
        sb.modify("World");
        System.out.println(sb);
    }
}
Q3822 medium code error
What is the error in this Java code?
java
import java.util.ArrayList;
import java.util.List;

public class MyClass {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>();
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");

        for (String name : names) {
            if (name.equals("Bob")) {
                names.remove(name);
            }
        }
        System.out.println(names);
    }
}
Q3823 easy
What happens if a subclass method attempts to override a superclass method but declares a new checked exception that is broader than any declared by the superclass method (or declares a checked exception when the superclass method declared none)?
Q3824 easy
What is method overriding in the context of inheritance?
Q3825 medium code error
What is the compilation error in the provided Java code?
java
class Parent {
    public void display() {
        System.out.println("Parent display");
    }
}

class Child extends Parent {
    protected void display() { // ERROR: weaker access
        System.out.println("Child display");
    }
}

public class Main {
    public static void main(String[] args) {
        Child c = new Child();
        c.display();
    }
}
Q3826 hard code output
What is the output of this Java code snippet?
java
public class MultiArrayQ3 {
    public static void main(String[] args) {
        int[][] original = {{1, 2}, {3, 4}};
        int[][] cloned = original.clone();
        cloned[0][0] = 99;
        cloned[1] = new int[]{5, 6};
        System.out.println(original[0][0]);
        System.out.println(original[1][0]);
    }
}
Q3827 hard code output
What is the output of this code?
java
class InitOrder {
    static { System.out.print("Static A. "); }
    { System.out.print("Instance X. "); }
    InitOrder() {
        System.out.print("Constructor 1. ");
    }
    InitOrder(String s) {
        System.out.print("Constructor 2 (" + s + "). ");
    }
    static { System.out.print("Static B. "); }
    { System.out.print("Instance Y. "); }
}
public class Main {
    public static void main(String[] args) {
        new InitOrder();
        new InitOrder("arg");
    }
}
Q3828 hard code output
What is the output of this code snippet?
java
class Animal {}
class Dog extends Animal {}
class Labrador extends Dog {}
public class Main {
    public static void main(String[] args) {
        Animal a = new Labrador();
        Dog d = (Dog) a;
        Labrador l = (Labrador) d;
        System.out.println(l.getClass().getSimpleName());
    }
}
Q3829 medium
What is the main advantage of `ReadWriteLock` over a simple `ReentrantLock` for a data structure that is frequently read but rarely written?
Q3830 easy code output
What does this Java code print?
java
public class WrapperImmutability {
    public static void main(String[] args) {
        Integer val = 100;
        val = val + 50; 
        System.out.println(val);
    }
}
Q3831 hard
Given that `java.util.LinkedList` implements the `Deque` interface, what is the primary distinction between the `addFirst(E e)` and `push(E e)` methods?
Q3832 hard
When considering memory usage for an `ArrayList` initialized with a large capacity (e.g., `new ArrayList<>(1000)`), then populated with 50 elements, and subsequently having all 50 elements removed using `remove(0)` repeatedly, what is the memory footprint behavior?
Q3833 medium code error
What is the error in this Java code?
java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MyClass {
    public static void main(String[] args) {
        List<String> cities = Arrays.asList("London", "Paris");
        cities.add("Rome"); // Attempt to add to a fixed-size list
        System.out.println(cities);
    }
}
Q3834 hard code error
What error will this Java code most likely encounter given sufficient execution time?
java
public class ThreadBombLoop {
    public static void main(String[] args) {
        int i = 0;
        while (true) {
            new Thread(() -> {
                try {
                    Thread.sleep(1000000); // Sleep indefinitely
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }).start();
            i++;
            System.out.println("Started thread " + i);
        }
    }
}
Q3835 medium code output
What is the output of this code?
java
public class StringBuilderVsString {
    public static void main(String[] args) {
        String s = "Hello";
        StringBuilder sb = new StringBuilder("World");

        s.concat(" Java");
        sb.append(" Java");

        System.out.println(s);
        System.out.println(sb);
    }
}
Q3836 hard code error
What error occurs when running this Java code?
java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class RemoveIfDuringIteration {
    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
        Iterator<Integer> it = nums.iterator();
        
        while (it.hasNext()) {
            Integer num = it.next();
            if (num % 2 == 0) {
                // This modifies the list externally (not via iterator's remove)
                nums.removeIf(n -> n.equals(num)); 
            }
        }
    }
}
Q3837 medium
What is the result of calling `String.valueOf(null)`?
Q3838 hard
A thread calls `Object.wait()` without a timeout within a synchronized block. What two crucial things happen simultaneously regarding its state and monitor ownership?
Q3839 hard
Which of the following statements about throwing `null` is correct in Java?
Q3840 easy code error
What compilation error will occur in the following Java code?
java
abstract class Vehicle {
    public abstract void start();
}

public class Main {
    public static void main(String[] args) {
        Vehicle v = new Vehicle();
    }
}
← Prev 190191192193194 Next → Page 192 of 200 · 3994 questions