☕ Java MCQ Questions – Page 9

Questions 161–180 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q161 hard code error
What is the compile-time error in this code involving the ternary operator?
java
public class TernaryTypeTest {
    public static void main(String[] args) {
        int i = 10;
        double d = 20.0;
        long result = (true ? i : d); // Ternary result type is double, assigned to long
        System.out.println(result);
    }
}
Q162 medium
Under what circumstance is a method reference generally considered a more concise alternative to a lambda expression?
Q163 medium
What is a primary characteristic of a checked exception in Java?
Q164 easy code error
What kind of error will occur when compiling this Java code?
java
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        try {
            File file = new File("output.txt");
            // BufferedWriter requires a Writer object, not a File object directly
            BufferedWriter bw = new BufferedWriter(file);
            bw.write("Test data.");
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Q165 medium
What is the underlying data structure of `java.util.LinkedList`?
Q166 hard code error
What is the result of executing this Java code snippet?
java
public class NullUnboxing {
    public static void main(String[] args) {
        Integer x = (Integer) null;
        int y = x; 
        System.out.println(y);
    }
}
Q167 hard code error
What is the runtime error when executing this Java code snippet?
java
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        TreeSet<Number> set = new TreeSet<>();
        set.add(10);
        set.add(20.5);
        System.out.println(set.size());
    }
}
Q168 hard code output
What is the output of this code?
java
import java.util.HashSet;
import java.util.Objects;

class MutableItem {
    int value;
    public MutableItem(int value) { this.value = value; }
    @Override public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MutableItem that = (MutableItem) o;
        return value == that.value;
    }
    @Override public int hashCode() { return Objects.hash(value); }
}

public class Main {
    public static void main(String[] args) {
        HashSet<MutableItem> set = new HashSet<>();
        MutableItem item1 = new MutableItem(10);
        set.add(item1);
        item1.value = 20; // Modifying item in set
        System.out.println(set.contains(new MutableItem(10)));
        System.out.println(set.contains(new MutableItem(20)));
        System.out.println(set.size());
    }
}
Q169 easy
What is deserialization in Java?
Q170 medium
What is true about `abstract` methods in Java?
Q171 easy
Why might a developer choose to create an immutable class over a mutable one for certain data?
Q172 medium code error
What is the compilation error in the provided Java code?
java
class Device {
    public final void initialize() {
        System.out.println("Device initialized.");
    }
}

class Smartphone extends Device {
    @Override // ERROR: cannot override final method
    public void initialize() {
        System.out.println("Smartphone initialized.");
    }
}

public class Main {
    public static void main(String[] args) {
        Smartphone s = new Smartphone();
        s.initialize();
    }
}
Q173 medium
Is it possible to declare and initialize multiple variables in the initialization section of a Java `for` loop, and update multiple variables in the update section?
Q174 hard code output
What does this code print?
java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> original = new ArrayList<>();
        original.add("X");
        List<String> unmodifiable = Collections.unmodifiableList(original);
        original.add("Y");
        // unmodifiable.add("Z"); // This line would throw UnsupportedOperationException
        System.out.println(unmodifiable.size());
    }
}
Q175 hard
You're designing a custom exception `InvalidConfigurationException` which needs to carry detailed, immutable context data (e.g., a map of invalid properties and their error messages) about the configuration issue. What is the most robust and thread-safe way to store this data within the exception instance?
Q176 medium code error
What error will occur when compiling and running this Java code?
java
import java.util.HashSet;
import java.util.Set;

class CustomKey {
    private int value;

    public CustomKey(int value) {
        this.value = value;
    }

    @Override
    public int hashCode() {
        if (value == 0) {
            throw new IllegalArgumentException("Value cannot be zero for hashing"); // Deliberate error
        }
        return value;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        CustomKey customKey = (CustomKey) o;
        return value == customKey.value;
    }
}

public class HashSetError9 {
    public static void main(String[] args) {
        Set<CustomKey> keys = new HashSet<>();
        keys.add(new CustomKey(1));
        keys.add(new CustomKey(10));
        keys.add(new CustomKey(0)); // ERROR line
    }
}
Q177 hard code output
What does this code print?
java
public class StringJoinTest {
    public static void main(String[] args) {
        String[] elements = {"apple", null, "banana", ""};
        String result = String.join(" - ", elements);
        System.out.println(result);
    }
}
Q178 medium code error
What happens when this code is executed?
java
public class LoopTest {
    public static void main(String[] args) {
        int counter = 0;
        while (counter < 3) {
            System.out.println("Count: " + counter);
            // counter++; // Missing increment
        }
        System.out.println("Loop Finished");
    }
}
Q179 easy code error
Which compilation error will occur when compiling the following Java code?
java
import java.util.function.UnaryOperator;

@FunctionalInterface
interface Formatter {
    String format(String input);
}

public class Main {
    private String prefix = "Format: ";

    public static void main(String[] args) {
        Formatter formatter = s -> prefix + s;
        System.out.println(formatter.format("Data"));
    }
}
Q180 medium
What is the guaranteed iteration order of elements (keys or values) when iterating over a `java.util.HashMap`?
← Prev 7891011 Next → Page 9 of 200 · 3994 questions