☕ Java MCQ Questions – Page 199
Questions 3961–3980 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhich of the following statements about `ArrayList` and thread safety is true?
What error occurs when executing the following Java code?
java
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> stringList = new ArrayList<>();
stringList.add("One");
stringList.add("Two");
Integer[] intArray = new Integer[stringList.size()];
stringList.toArray(intArray); // Attempt to put String elements into an Integer array
for (Integer num : intArray) {
System.out.println(num);
}
}
}
What does this code print?
java
public class MyClass {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("First");
sb.append("Second").append("Third");
System.out.print(sb);
}
}
Which method is primarily used to begin the execution of a newly created `Thread` object?
What is the output of this code?
java
class Base {
Number getValue() {
return Integer.valueOf(10);
}
}
class Derived extends Base {
@Override
Integer getValue() {
return Integer.valueOf(20);
}
}
public class Main {
public static void main(String[] args) {
Base obj = new Derived();
System.out.println(obj.getValue());
}
}
What is the compile-time error in this Java code?
java
public class MissingRun implements Runnable {
public void execute() {
System.out.println("Executing a task.");
}
public static void main(String[] args) {
// new Thread(new MissingRun()).start(); // Line 8
}
}
Which statement best describes how immutability contributes to application security?
What is the runtime error when executing this Java code snippet?
java
import java.util.TreeSet;
import java.io.*;
class MyObject implements Serializable {
int value;
MyObject(int value) { this.value = value; }
}
public class Main {
public static void main(String[] args) throws IOException, ClassNotFoundException {
TreeSet<MyObject> set = new TreeSet<>((o1, o2) -> o1.value - o2.value);
set.add(new MyObject(10));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(set);
oos.close();
System.out.println("Serialization successful.");
}
}
What kind of error will occur when compiling or running the following Java code?
java
public class SBError {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Test"); // Length 4
char c = sb.charAt(sb.length()); // Access index 4
System.out.println(c);
}
}
Which of the following is the correct way to declare an integer variable named `score` in Java?
What is the compile-time error in this Java code?
java
import java.util.function.Supplier;
public class LambdaError {
public static void main(String[] args) {
Supplier<String> greeting = (name) -> "Hello, " + name;
System.out.println(greeting.get());
}
}
Which of the following Stream API terminal operations is designed to return an `Optional`?
What does this code print?
java
public class MyClass {
public static void main(String[] args) {
char grade = 'B';
switch (grade) {
case 'A': System.out.print("Excellent"); break;
case 'B': System.out.print("Good"); break;
case 'C': System.out.print("Fair"); break;
default: System.out.print("Invalid");
}
}
}
What is the output of this Java program?
java
public class Main {
public static void main(String[] args) {
int result = 5 + 2 * 3;
System.out.println(result);
}
}
What is the primary purpose of the `FileReader` class in Java?
What compilation error will occur in the `main` method's lambda expression?
java
import java.util.function.Consumer;
public class CustomServiceException extends Exception {
public CustomServiceException(String message) {
super(message);
}
}
public class ServiceRunner {
public void executeTask(Consumer<String> task) {
try {
task.accept("data");
} catch (Exception e) {
System.out.println("Caught task exception: " + e.getMessage());
}
}
public static void main(String[] args) {
ServiceRunner runner = new ServiceRunner();
runner.executeTask(s -> {
System.out.println("Processing " + s);
throw new CustomServiceException("Task failed for " + s);
});
}
}
What is the compilation error in the following Java code?
java
public class FinalVariable {
public static void main(String[] args) {
final int MAX_VALUE = 100;
MAX_VALUE = 200;
System.out.println(MAX_VALUE);
}
}
What is the output of this code?
java
public class DataTypeTest {
public static void main(String[] args) {
int myNumber = 123;
System.out.println(myNumber);
}
}
What is the compile-time error for the `Car` class?
java
abstract class Vehicle {
public abstract void start();
public void stop() {
System.out.println("Vehicle stopped.");
}
}
class Car extends Vehicle {
// Missing implementation for start()
}
public class Main {
public static void main(String[] args) {
// Code won't reach here due to compile error
}
}
What does this code print?
java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StreamTest {
public static void main(String[] args) {
List<List<String>> nestedLists = Arrays.asList(
Arrays.asList("a", "b"),
Arrays.asList("c", "d", "e"),
Arrays.asList("f")
);
String result = nestedLists.stream()
.flatMap(List::stream)
.map(String::toUpperCase)
.collect(Collectors.joining(" "));
System.out.println(result);
}
}