☕ Java MCQ Questions – Page 95

Questions 1881–1900 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1881 hard
An `ArrayList` is created using `new ArrayList<String>()`. If elements are added until its internal capacity needs to grow for the 3rd time, what is the minimum number of elements that must have been added?
Q1882 medium
What happens if `next()` is called on a `java.util.Iterator` when `hasNext()` would return `false`?
Q1883 easy code error
What compilation error will occur in the following Java code?
java
abstract class Processor {
    public abstract void process() {
        System.out.println("Processing data...");
    }
}
Q1884 easy code error
What is the result of running this Java code?
java
public class StringError {
    public static void main(String[] args) {
        String data = "UPPERCASE";
        System.out.println(data.toLowercase());
    }
}
Q1885 hard code output
What is the output of this code?
java
import java.util.Date;

final class DeepImmutableData {
    private final String name;
    private final Date birthDate;

    public DeepImmutableData(String name, Date birthDate) {
        this.name = name;
        this.birthDate = new Date(birthDate.getTime()); // Defensive copy
    }

    public String getName() { return name; }
    public Date getBirthDate() { return new Date(birthDate.getTime()); } // Defensive copy
}

public class Main {
    public static void main(String[] args) {
        Date d = new Date(100, 0, 1); // Jan 1, 2000
        DeepImmutableData data = new DeepImmutableData("Test", d);
        d.setYear(101); // Jan 1, 2001
        System.out.println(data.getBirthDate().getYear());
    }
}
Q1886 hard code error
What is the result of compiling and running the following Java code snippet?
java
public class StringError {
    public static void main(String[] args) {
        String s = "alpha.beta.gamma";
        String[] parts = s.split(".");
        System.out.println(parts.length);
    }
}
Q1887 medium
Which Stream API operation should be used when you have a stream of lists (e.g., `Stream<List<String>>`) and you want to obtain a single stream of all individual strings (`Stream<String>`)?
Q1888 hard code output
What is the output of this code?
java
public class DataTypeChallenge {
    public static void main(String[] args) {
        byte b1 = -1;
        byte b2 = 1;
        int result = b1 >>> b2;
        System.out.println(result);
    }
}
Q1889 hard code error
What is the compilation error in the `main` method?
java
class RestrictedException extends Exception {
    // Private constructor restricts direct external instantiation
    private RestrictedException(String message) {
        super(message);
    }

    // A static factory method might be provided for controlled instantiation
    public static RestrictedException create(String message) {
        return new RestrictedException(message);
    }
}

public class Main {
    public static void main(String[] args) {
        // Attempting to directly call a private constructor
        RestrictedException e = new RestrictedException("Access denied"); // This line causes the compilation error
    }
}
Q1890 easy
What does `synchronized(this)` mean when used in a code block within an instance method?
Q1891 easy
Which of the following describes the key requirement for objects used as keys in a `TreeMap` if no custom `Comparator` is provided?
Q1892 hard code output
What does this code print?
java
public class OperatorChallenge {
    public static void main(String[] args) {
        System.out.println(1 + 2 + "3" + 4 + 5);
    }
}
Q1893 easy code output
What is the output of this Java code snippet?
java
public class Main {
    public static void main(String[] args) {
        int x = 10;
        if (x > 5) {
            System.out.println("X is greater than 5");
        }
    }
}
Q1894 easy code output
What is the output of this code?
java
class BlockCounter {
    private int count = 0;
    public void increment() {
        synchronized(this) {
            count++;
            System.out.print(count);
        }
    }
}

public class MyProgram {
    public static void main(String[] args) throws InterruptedException {
        BlockCounter bc = new BlockCounter();
        Thread t1 = new Thread(() -> bc.increment());
        Thread t2 = new Thread(() -> bc.increment());
        t1.start();
        t2.start();
        t1.join();
        t2.join();
    }
}
Q1895 hard
What is a critical consideration when evaluating a boolean expression in an `if` statement that involves comparison between an `int` primitive and an `Integer` wrapper object using `==`?
Q1896 easy code output
What is the output of this code?
java
class Printer {
    public void print(String s) {
        System.out.print("String: " + s);
    }
    public void print(int i) {
        System.out.print("Int: " + i);
    }
}

public class Main {
    public static void main(String[] args) {
        Printer p = new Printer();
        p.print(123);
    }
}
Q1897 medium code error
What kind of error will occur when executing this Java code?
java
import java.util.Comparator;
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        Comparator<Number> customComparator = (n1, n2) -> {
            Integer i1 = (Integer) n1;
            Integer i2 = (Integer) n2;
            return i1.compareTo(i2);
        };
        TreeSet<Number> set = new TreeSet<>(customComparator);
        set.add(10);
        set.add(5);
        set.add(3.14);
        System.out.println(set.size());
    }
}
Q1898 hard code error
What is the compile-time error in this Java code?
java
abstract class Base {
    public abstract Base();
    private int value;
    Base(int val) {
        this.value = val;
    }
}
Q1899 hard code error
What kind of error will occur when executing the following Java code?
java
import java.util.LinkedList;
import java.util.Queue;

public class QError5 {
    public static void main(String[] args) {
        Queue rawQueue = new LinkedList();
        rawQueue.add("Hello");
        rawQueue.add(123);
        Integer i = (Integer) rawQueue.poll();
        System.out.println(i);
    }
}
Q1900 medium
What distinguishes a daemon thread from a user (non-daemon) thread in Java?
← Prev 9394959697 Next → Page 95 of 200 · 3994 questions