☕ Java MCQ Questions – Page 173
Questions 3441–3460 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhich of the following is a valid way to create a `Runnable` instance using a lambda expression in Java 8 or later?
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();
}
}
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());
}
}
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?
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]);
}
}
}
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());
}
}
In the context of encapsulation, what is a common reason to make a class's constructor `private`?
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());
}
}
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
}
}
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
}
}
Which of the following variable names is considered invalid in Java?
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]);
}
}
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]);
}
}
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);
}
}
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?
Is `HashSet` inherently thread-safe?
Which statement is true regarding an interface that *can* be used as a functional interface even without the `@FunctionalInterface` annotation?
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"));
}
}
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);
}
}
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) {} }
}
}