☕ Java MCQ Questions – Page 167

Questions 3321–3340 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3321 easy
What happens if a lambda expression's body consists of a single expression, rather than a block of statements?
Q3322 hard
In a strictly single-threaded application, what is the primary reason `StringBuffer` is generally less performant than `StringBuilder` for the same sequence of operations?
Q3323 medium code error
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();
    }
}
Q3324 easy code output
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);
    }
}
Q3325 easy code output
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"));
    }
}
Q3326 easy code error
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());
    }
}
Q3327 hard code output
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());
    }
}
Q3328 hard code error
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);
    }
}
Q3329 easy code output
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.");
    }
}
Q3330 medium code error
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());
            }
        }
    }
}
Q3331 easy code output
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);
    }
}
Q3332 hard
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?
Q3333 easy code error
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());
    }
}
Q3334 hard code output
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());
        }
    }
}
Q3335 easy code output
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]);
    }
}
Q3336 hard code output
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);
Q3337 hard
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?
Q3338 hard code output
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"));
    }
}
Q3339 easy code output
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();
    }
}
Q3340 easy code output
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();
    }
}
← Prev 165166167168169 Next → Page 167 of 200 · 3994 questions