☕ Java MCQ Questions – Page 155
Questions 3081–3100 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhich Java primitive data type is used to store whole numbers with the largest possible range?
What is the error in the following Java code snippet?
java
public class ArrayError {
public static void main(String[] args) {
int[] numbers = null;
System.out.println(numbers.length);
}
}
What is the key difference between the `trim()` and `strip()` String methods regarding the types of whitespace characters they remove?
Which interface must a class implement to be eligible for Java's default serialization mechanism?
What does this Java code print?
java
public class TypeCast {
public static void main(String[] args) {
int count = 15;
float price = count;
System.out.println(price);
}
}
Multiple threads concurrently access and modify a `TreeMap` instance without any external synchronization. Which of the following is the most likely outcome?
What is the error in the following Java code?
java
class Vehicle {
public void start() {
System.out.println("Vehicle starting");
}
}
class Car extends Vehicle {
@Override
protected void start() { // Attempting to reduce access modifier
System.out.println("Car starting");
}
}
What is the compilation error in the provided Java code?
java
class Alpha {}
class Beta {}
public class CastingProblem {
public static void main(String[] args) {
Alpha a = new Alpha();
Beta b = (Beta) a; // ERROR: incompatible types
System.out.println("Casting successful!");
}
}
What is the output of this code?
java
import java.util.ArrayList;
class CustomObject {
int id;
public CustomObject(int id) { this.id = id; }
@Override
public String toString() { return "Custom:" + id; }
}
public class Test {
public static void main(String[] args) {
ArrayList<CustomObject> list = new ArrayList<>();
CustomObject obj1 = new CustomObject(1);
CustomObject obj2 = new CustomObject(2);
list.add(obj1);
list.add(new CustomObject(2));
list.add(obj2);
list.add(new CustomObject(1));
int index1 = list.indexOf(obj2);
int index2 = list.indexOf(new CustomObject(2));
int index3 = list.lastIndexOf(obj1);
System.out.println(index1 + "," + index2 + "," + index3);
}
}
What is the output of this code?
java
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
TreeSet<Integer> ts = new TreeSet<>();
ts.add(5);
ts.add(2);
ts.add(8);
ts.remove(5);
System.out.println(ts);
}
}
What is the length of the StringBuilder object after execution?
java
public class StringBuilderTest {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello");
sb.append("World");
sb.insert(5, " ");
System.out.println(sb.length());
}
}
What is the output of this code?
java
import java.util.ArrayDeque;
import java.util.Queue;
public class ArrayDequeMethods {
public static void main(String[] args) {
Queue<Integer> q = new ArrayDeque<>();
q.add(100);
q.offer(200);
q.remove();
q.add(300);
System.out.println(q.element());
}
}
Which of the following statements about accessing local variables from within a lambda expression is TRUE?
How do you initialize a 2D integer array named `matrix` with 3 rows and 4 columns?
What does this code print?
java
class CustomError extends Exception {
public CustomError(String message) {
super(message);
}
}
public class Main {
public static void checkValue(int value) throws CustomError {
if (value < 10) {
throw new CustomError("Value too low!");
}
System.out.println("Value is acceptable.");
}
public static void main(String[] args) {
try {
checkValue(5);
} catch (CustomError e) {
System.out.println("Error details: " + e.toString());
} finally {
System.out.println("Finally block executed.");
}
}
}
What is a primary difference between a Java array and a `java.util.ArrayList` in terms of size?
What does this Java code snippet print?
java
public class WhileLoopDemo {
public static void main(String[] args) {
int a = 0;
while (a++ < 3) {
System.out.print(a);
}
}
}
What does this code print, illustrating how serialization interacts with encapsulation of mutable fields?
java
import java.io.*; import java.util.HashMap; import java.util.Map;
class Config implements Serializable {
private Map<String, String> settings;
public Config(Map<String, String> initialSettings) { this.settings = initialSettings; }
public Map<String, String> getSettings() { return settings; }
}
public class Main {
public static void main(String[] args) throws Exception {
Config originalConfig = new Config(new HashMap<>(Map.of("theme", "dark")));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
new ObjectOutputStream(bos).writeObject(originalConfig);
Config deserializedConfig = (Config) new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray())).readObject();
deserializedConfig.getSettings().put("language", "en");
System.out.println("Original: " + originalConfig.getSettings());
System.out.println("Deserialized: " + deserializedConfig.getSettings());
}
}
What is the compilation error, if any, for the provided Java code snippet?
java
class Overloader {
public void setup(int a, long b) {}
public void setup(long a, int b) {}
public static void main(String[] args) {
Overloader o = new Overloader();
o.setup(10, 20); // Line X
}
}
What is the output of the following Java program?
java
public class StringTest {
public static void main(String[] args) {
String sentence = "The quick brown fox jumps over the lazy dog.";
boolean check1 = sentence.startsWith("The");
boolean check2 = sentence.endsWith("dog");
boolean check3 = sentence.contains("fox");
System.out.println(check1 + "," + check2 + "," + check3);
}
}