☕ Java MCQ Questions – Page 104

Questions 2061–2080 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2061 medium
If a superclass method declares `throws IOException`, which of the following is true for an overriding method in a subclass?
Q2062 hard
A `Serializable` class has a field marked `transient`. If this class also implements `private void writeObject(ObjectOutputStream oos)` and `private void readObject(ObjectInputStream ois)`, how will the `transient` field be handled?
Q2063 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) {
        String data = "Hello World";
        BufferedReader br = new BufferedReader(new StringReader(data));
        try {
            br.readLine();
            br.close();
            int charCode = br.read(); // Attempt to read after close
            System.out.println((char)charCode);
        } catch (IOException e) {
            System.err.println("Runtime Error: " + e.getMessage());
        }
    }
}
Q2064 medium
When using `LinkedList` as a `Queue` in Java, which method would you typically use to add an element to the tail of the queue according to the `Queue` interface contract?
Q2065 hard
A `TreeSet` of integers, `originalSet`, contains elements {10, 20, 30, 40, 50}. A `NavigableSet` view `subSet` is created using `originalSet.subSet(20, true, 40, false)`. What happens if an attempt is made to add the element `45` to `subSet`?
Q2066 easy code output
What is the output of this Java code?
java
import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        Function<Integer, Integer> square = x -> x * x;
        System.out.println(square.apply(4));
    }
}
Q2067 medium
When implementing its `get(int index)` method, how does `LinkedList` optimize traversal?
Q2068 medium
If a lambda expression's body contains multiple statements, how must it be enclosed?
Q2069 hard
Given the declaration: java int[][] jagged = { {1, 2}, {3}, {4, 5, 6} }; What is the value of `jagged[1].length`?
Q2070 easy code output
What does this code print?
java
class NoDataException extends Exception {
    public NoDataException() {
        super("No data found for the request.");
    }
}

public class Main {
    public static void processData() throws NoDataException {
        boolean dataExists = false;
        if (!dataExists) {
            throw new NoDataException();
        }
    }

    public static void main(String[] args) {
        try {
            processData();
        } catch (NoDataException e) {
            System.out.println(e.getMessage());
        } finally {
            System.out.println("Processing attempt complete.");
        }
    }
}
Q2071 medium code error
What is the error in the following Java code?
java
class SensorData {
    private int readings;

    public SensorData(int initialReadings) {
        this.readings = initialReadings;
    }

    public int getReadings() {
        return readings;
    }

    public void resetReadings() {
        if (true) {
            readings = 0; // Legal modification within the class
        }
    }
}

public class DataProcessor {
    public void processData(SensorData data) {
        data.readings = -1; // Attempt to modify private field directly
    }

    public static void main(String[] args) {
        SensorData sensor = new SensorData(5);
        new DataProcessor().processData(sensor);
    }
}
Q2072 hard
What is the primary advantage of calling `intern()` on a `String` literal that has already been implicitly interned by the JVM?
Q2073 medium code error
What is the compilation error in the following Java code?
java
public class MyClass {
    public static void main(String[] args) {
        int value;
        System.out.println("The value is: " + value);
    }
}
Q2074 easy
What is the primary characteristic that defines a 'functional interface' in Java?
Q2075 medium
Under what circumstances does a Java thread transition into the TERMINATED state?
Q2076 hard
In a traditional Java `switch` *statement*, if the `default` case is placed somewhere in the middle of other `case` labels and no `break` statement is present, what happens when a value matching the `default` case is encountered?
Q2077 easy code error
What compile-time error will occur in the `MyClass` definition?
java
abstract class MyClass {
    abstract MyClass();
}
public class Main {
    public static void main(String[] args) {
        // No direct instantiation here, error is in class definition
    }
}
Q2078 easy code error
What compilation error will occur in the following Java code?
java
abstract class Document {
    public abstract final void open();
}
Q2079 medium
What methods are primarily used by `HashMap` to determine key equality and bucket placement for entries?
Q2080 medium code error
What is the compilation error in the following Java code?
java
public class DuplicateVar {
    public static void main(String[] args) {
        int number = 5;
        String number = "five";
        System.out.println(number);
    }
}
← Prev 102103104105106 Next → Page 104 of 200 · 3994 questions