☕ Java MCQ Questions – Page 167
Questions 3321–3340 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat happens if a lambda expression's body consists of a single expression, rather than a block of statements?
In a strictly single-threaded application, what is the primary reason `StringBuffer` is generally less performant than `StringBuilder` for the same sequence of operations?
What will be printed to the console when the following Java code is executed?
java
import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;
public class TestClass {
public static void main(String[] args) throws IOException {
String data = "abcdefghij";
BufferedReader br = new BufferedReader(new StringReader(data));
br.mark(2); // Mark at 'a', read limit 2
br.read(); // Reads 'a'
br.read(); // Reads 'b'
br.read(); // Reads 'c' - mark invalidated
br.reset(); // Error here
br.close();
}
}
What is the output of this Java code?
java
public class OverloadDemo {
void display(int a) {
System.out.println("Integer: " + a);
}
void display(double a) {
System.out.println("Double: " + a);
}
public static void main(String[] args) {
OverloadDemo obj = new OverloadDemo();
obj.display(10);
}
}
What is the output of this Java code?
java
import java.util.LinkedList;
public class MyClass {
public static void main(String[] args) {
LinkedList<String> fruits = new LinkedList<>();
fruits.add("Mango");
fruits.add("Orange");
System.out.println(fruits.contains("Orange"));
}
}
What is the result of running this Java code?
java
public class StringError {
public static void main(String[] args) {
String message;
System.out.println(message.toUpperCase());
}
}
What is the output of this Java code?
java
import java.util.TreeMap;
import java.util.Map;
public class Test {
public static void main(String[] args) {
TreeMap<Integer, String> map = new TreeMap<>();
map.put(1, "A");
map.put(2, "B");
map.put(3, "C");
Map.Entry<Integer, String> first = map.pollFirstEntry(); // Removes 1=A
Map.Entry<Integer, String> last = map.pollLastEntry(); // Removes 3=C
Map.Entry<Integer, String> newFirst = map.pollFirstEntry(); // Map now only has {2=B}, removes 2=B
System.out.println(first.getValue() + ", " + last.getValue() + ", " + newFirst.getValue());
}
}
What is the compile-time error in this Java code snippet that uses a switch expression with a sealed interface?
java
sealed interface Shape permits Circle, Square {}
record Circle(double radius) implements Shape {}
record Square(double side) implements Shape {}
public class SealedSwitchError {
public static void main(String[] args) {
Shape shape = new Circle(10);
String description = switch (shape) { // Error expected here
case Circle c -> "Circle";
};
System.out.println(description);
}
}
What does this code print?
java
import java.io.IOException;
public class MyClass {
public static void performTask() throws IOException {
System.out.println("Task started.");
// No actual exception is thrown here
}
public static void main(String[] args) {
try {
performTask();
} catch (IOException e) {
System.out.println("Caught an IOException.");
}
System.out.println("Program finished.");
}
}
What is the output of this Java code?
java
import java.io.*;
public class DeserializationError3 {
public static void main(String[] args) {
byte[] data = null;
{
class MyDynamicObject implements Serializable {
String fieldA; // Original field
public MyDynamicObject(String a) { this.fieldA = a; }
}
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
oos.writeObject(new MyDynamicObject("Original"));
data = bos.toByteArray();
} catch (IOException e) { System.err.println(e); }
}
{
class MyDynamicObject implements Serializable {
String fieldB; // Modified field, 'fieldA' removed
public MyDynamicObject(String b) { this.fieldB = b; }
}
try (ByteArrayInputStream bis = new ByteArrayInputStream(data);
ObjectInputStream ois = new ObjectInputStream(bis)) {
MyDynamicObject obj = (MyDynamicObject) ois.readObject();
System.out.println("Deserialized: " + obj.fieldB);
} catch (Exception e) {
System.out.println("Error: " + e.getClass().getName());
}
}
}
}
What does this code print?
java
class Point {
int x;
int y;
public Point(int xCoord, int yCoord) {
x = xCoord;
y = yCoord;
}
}
public class Main {
public static void main(String[] args) {
Point p = new Point(5, 10);
System.out.print("X: " + p.x + ", Y: " + p.y);
}
}
A `Serializable` class `Foo` initially has fields `int x` and `String y`. A new `int z` field is added, but the `serialVersionUID` is *not* updated. An old serialized `Foo` object (without `z`) is then deserialized using the new class definition. What is the expected behavior for the `z` field?
What kind of error will occur when compiling the following Java code snippet?
java
public class FileError3 {
public static void main(String[] args) {
File myFile = new File("data.txt");
System.out.println(myFile.exists());
}
}
What does this code print?
java
import java.io.IOException;
import java.util.function.Consumer;
public class LambdaExceptionHandling {
@FunctionalInterface
interface ThrowingConsumer<T, E extends Exception> {
void accept(T t) throws E;
}
private static <T, E extends Exception> Consumer<T> wrapConsumer(ThrowingConsumer<T, E> throwingConsumer) {
return i -> {
try {
throwingConsumer.accept(i);
} catch (Exception e) {
throw new RuntimeException(e);
}
};
}
public static void main(String[] args) {
Consumer<Integer> safeConsumer = wrapConsumer(i -> {
if (i > 5) {
throw new IOException("Value " + i + " is too large!");
}
System.out.println("Processed: " + i);
});
try {
safeConsumer.accept(3);
safeConsumer.accept(7);
} catch (RuntimeException e) {
System.out.println("Caught RuntimeException: " + e.getCause().getMessage());
}
}
}
What is the output of this Java code?
java
public class ArrayMutability {
public static void main(String[] args) {
int[] numbers = {1, 2, 3};
numbers[0] = 99;
System.out.println(numbers[0]);
}
}
What is the output of this Java code?
java
String data = ",a,b,,c,";
String[] parts1 = data.split(",");
String[] parts2 = data.split(",", -1);
System.out.println(parts1.length);
System.out.println(parts2.length);
Consider the following method declarations within the same class:
1. `public void execute(int a)`
2. `private String execute(int a)`
Which statement about these two declarations is true regarding method overloading in Java?
What is the output of this code?
java
import java.util.function.Function;
public class FunctionComposition {
public static void main(String[] args) {
Function<String, String> f1 = s -> s + "A";
Function<String, String> f2 = s -> s + "B";
Function<String, String> f3 = s -> s + "C";
Function<String, String> combined = f1.andThen(f2).compose(f3);
System.out.println(combined.apply("start"));
}
}
What will this Java code print?
java
public class SimpleRunnable {
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
System.out.println("Anonymous Runnable task.");
}
};
new Thread(r).start();
}
}
What does this code print?
java
public class MySimpleThread extends Thread {
public void run() {
System.out.println("Running in " + Thread.currentThread().getName());
}
public static void main(String[] args) {
MySimpleThread thread1 = new MySimpleThread();
thread1.setName("WorkerThread");
thread1.start();
}
}