☕ Java MCQ Questions – Page 95
Questions 1881–1900 of 3994 total — Java interview practice
▶ Practice All Java QuestionsAn `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?
What happens if `next()` is called on a `java.util.Iterator` when `hasNext()` would return `false`?
What compilation error will occur in the following Java code?
java
abstract class Processor {
public abstract void process() {
System.out.println("Processing data...");
}
}
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());
}
}
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());
}
}
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);
}
}
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>`)?
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);
}
}
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
}
}
What does `synchronized(this)` mean when used in a code block within an instance method?
Which of the following describes the key requirement for objects used as keys in a `TreeMap` if no custom `Comparator` is provided?
What does this code print?
java
public class OperatorChallenge {
public static void main(String[] args) {
System.out.println(1 + 2 + "3" + 4 + 5);
}
}
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");
}
}
}
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();
}
}
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 `==`?
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);
}
}
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());
}
}
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;
}
}
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);
}
}
What distinguishes a daemon thread from a user (non-daemon) thread in Java?