☕ Java MCQ Questions – Page 173

Questions 3441–3460 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3441 medium
Which of the following is a valid way to create a `Runnable` instance using a lambda expression in Java 8 or later?
Q3442 easy code error
What type of error will occur when compiling this Java code?
java
public class PrimitiveBooleanSync {
    private boolean active = false;

    public void toggleActive() {
        synchronized (active) { // Attempting to synchronize on a primitive boolean
            active = !active;
            System.out.println("Active: " + active);
        }
    }

    public static void main(String[] args) {
        new PrimitiveBooleanSync().toggleActive();
    }
}
Q3443 easy code output
What is the output of this code?
java
import java.util.function.Supplier;

public class LambdaTest {
    public static void main(String[] args) {
        Supplier<String> greeter = () -> "Welcome!";
        System.out.println(greeter.get());
    }
}
Q3444 hard
While `HashSet` makes no guarantees about the iteration order of its elements, under what specific scenario might the elements *appear* to be iterated in insertion order, though it's still not a contractual guarantee?
Q3445 medium code error
What is the output of this Java code?
java
import java.io.*;

public class DeserializationError10 {
    static class MyExternalizableObject implements Externalizable {
        String name;

        public MyExternalizableObject() {}
        public MyExternalizableObject(String name) { this.name = name; }

        @Override
        public void writeExternal(ObjectOutput out) throws IOException {
            out.writeObject(name);
        }
        // The readExternal method is intentionally missing
        // @Override
        // public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        //     name = (String) in.readObject();
        // }
    }

    public static void main(String[] args) {
        byte[] data = null;
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
             ObjectOutputStream oos = new ObjectOutputStream(bos)) {
            oos.writeObject(new MyExternalizableObject("Test"));
            data = bos.toByteArray();
        } catch (IOException e) { e.printStackTrace(); }

        try (ByteArrayInputStream bis = new ByteArrayInputStream(data);
             ObjectInputStream ois = new ObjectInputStream(bis)) {
            MyExternalizableObject obj = (MyExternalizableObject) ois.readObject();
            System.out.println("Deserialized: " + obj.name);
        } catch (Exception e) {
            System.out.println("Error: " + e.getClass().getName() + ": " + e.getMessage().split(";")[0]);
        }
    }
}
Q3446 medium code output
What is the output of this code?
java
import java.util.HashMap;

public class Test {
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1, "one");
        map.put(2, "two");
        map.put(1, "uno"); // Overwrites value for key 1
        map.remove(2);
        System.out.println(map.size());
    }
}
Q3447 medium
In the context of encapsulation, what is a common reason to make a class's constructor `private`?
Q3448 medium code output
What is the output of the following Java program?
java
import java.util.function.Supplier;

class DataHolder {
    private String value = "Initial";
    public String getValue() { return value; }
}

public class InstanceMethodReference {
    public static void main(String[] args) {
        DataHolder holder = new DataHolder();
        Supplier<String> supplier = holder::getValue;
        System.out.println(supplier.get());
    }
}
Q3449 hard code error
What is the compile-time error when calling `calculate(5, 10)`?
java
class Calculator {
    public void calculate(int a, long b) {
        System.out.println("int, long");
    }
    public void calculate(long a, int b) {
        System.out.println("long, int");
    }
}
public class Tester {
    public static void main(String[] args) {
        Calculator calc = new Calculator();
        calc.calculate(5, 10); // Line 12
    }
}
Q3450 hard code error
What is the compilation error, if any, for the provided Java code snippet?
java
class Overloader {
    public void execute(Object o) {}
    public void execute(Runnable r) {}

    public static void main(String[] args) {
        Overloader o = new Overloader();
        o.execute(null); // Line X
    }
}
Q3451 medium
Which of the following variable names is considered invalid in Java?
Q3452 easy code error
What error will this Java code produce during compilation?
java
public class ArrayError {
    public static void main(String[] args) {
        int[][][] cube = new int[2][3];
        System.out.println(cube[0][0][0]);
    }
}
Q3453 hard code error
What runtime error will this Java code snippet throw?
java
public class Test {
    public static void main(String[] args) {
        int[][] grid = new int[2][2];
        grid[0][0] = 1;
        grid[1] = null;
        System.out.println(grid[1][0]);
    }
}
Q3454 medium code output
What is the output of this Java code?
java
import java.util.function.IntBinaryOperator;

class Counter {
    private int value;
    public Counter(int initial) { this.value = initial; }
    public int add(int x, int y) {
        value += (x + y);
        return value;
    }
}

public class MethodRefTest {
    public static void main(String[] args) {
        Counter counter = new Counter(10);
        IntBinaryOperator adder = counter::add;
        int res1 = adder.applyAsInt(2, 3);
        int res2 = adder.applyAsInt(1, 1);
        System.out.println(res1 + " " + res2);
    }
}
Q3455 medium
Consider a scenario where an `Object` reference `obj` holds an instance of `String`. What happens if you try to cast `obj` to a `StringBuilder` without checking its actual type?
Q3456 medium
Is `HashSet` inherently thread-safe?
Q3457 easy
Which statement is true regarding an interface that *can* be used as a functional interface even without the `@FunctionalInterface` annotation?
Q3458 medium code output
What is the output of this Java program?
java
import java.util.HashSet;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Set<String> fruits = new HashSet<>();
        fruits.add("apple");
        fruits.add("mango");
        fruits.add("orange");
        System.out.println(fruits.contains("grape"));
    }
}
Q3459 hard code error
What is the error in the execution of this Java code snippet?
java
public class BuilderError {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        sb.appendCodePoint(-1);
        System.out.println(sb);
    }
}
Q3460 hard code output
What is the output of this Java code?
java
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.nio.file.Files;
import java.nio.file.Path;

public class FileWriterNullCharWrite {
    public static void main(String[] args) {
        String filename = "null_char_test.txt";
        String s = "Hello\u0000World"; // Null character
        try (FileWriter fw = new FileWriter(filename)) {
            fw.write(s);
        } catch (IOException e) { System.out.println("Error: " + e.getMessage()); }

        try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
            String line = br.readLine();
            System.out.println("\'" + (line != null ? line.replace("\u0000", "[NULL]") : "[Empty]") + "\'");
        } catch (IOException e) { System.out.println("Error reading: " + e.getMessage());
        } finally { try { Files.deleteIfExists(Path.of(filename)); } catch (IOException ignored) {} }
    }
}
← Prev 171172173174175 Next → Page 173 of 200 · 3994 questions