☕ Java MCQ Questions – Page 135

Questions 2681–2700 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2681 easy code output
What does this code print to the console?
java
import java.io.*;

class Initializable implements Serializable {
    private String data;

    public Initializable() {
        System.out.println("Constructor called!");
        this.data = "Default";
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getData() {
        return data;
    }
}

public class Main {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Initializable obj = new Initializable();
        obj.setData("Hello");

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(obj);
        oos.close();

        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        Initializable deserializedObj = (Initializable) ois.readObject();
        ois.close();

        System.out.println("Deserialized Data: " + deserializedObj.getData());
    }
}
Q2682 easy code output
What does this code print?
java
import java.util.HashSet;

public class Test {
    public static void main(String[] args) {
        HashSet<String> fruits = new HashSet<>();
        boolean addedFirst = fruits.add("Mango");
        boolean addedSecond = fruits.add("Mango"); // Duplicate
        System.out.println(addedFirst);
        System.out.println(addedSecond);
    }
}
Q2683 medium code output
What is the output of this code?
java
class MyCheckedException extends Exception {
    public MyCheckedException(String msg) {
        super(msg);
    }
}

class MyUncheckedException extends RuntimeException {
    public MyUncheckedException(String msg) {
        super(msg);
    }
}

public class Main {
    public static void main(String[] args) {
        try {
            throw new MyUncheckedException("Unchecked!");
        } catch (MyCheckedException e) {
            System.out.println("Caught Checked: " + e.getMessage());
        } catch (MyUncheckedException e) {
            System.out.println("Caught Unchecked: " + e.getMessage());
        }
    }
}
Q2684 hard
What happens if a thread T1 calls `T2.join()` and T1 itself is interrupted while waiting for T2 to finish?
Q2685 medium
In the context of method overriding, what is the primary use of the `super` keyword inside an overriding method?
Q2686 hard
What are the most critical consequences if `FileWriter.close()` is consistently omitted after all write operations are complete and before the program exits?
Q2687 easy code error
What is the primary compile-time error in this code?
java
class DataObject {
    private String info;
    public DataObject(String info) { this.info = info; }
}

public class Main {
    public static void main(String[] args) {
        DataObject obj = new DataObject("Invalid data");
        throw obj; // Attempt to throw an object that is not a Throwable
    }
}
Q2688 hard
A thread `T` uses `java.util.concurrent.locks.ReentrantLock` and calls `Condition.await()` within a `try-finally` block. What state does `T` enter, and what monitor operation occurs?
Q2689 medium
If you have a 2D array `String[][] names = new String[3][5];`, which index combination correctly refers to an element in the second row and third column?
Q2690 easy
Which of the following can be used as the monitor object for a `synchronized` block in Java?
Q2691 medium code error
What is the compilation error in the provided Java code?
java
class ParentService {
    private void internalLogic() {
        System.out.println("Parent internal logic");
    }
}

class ChildService extends ParentService {
    @Override // ERROR: @Override on private method
    public void internalLogic() {
        System.out.println("Child internal logic");
    }
}

public class Main {
    public static void main(String[] args) {
        // ChildService cs = new ChildService();
    }
}
Q2692 easy code output
What is the output of this Java code?
java
public class Product {
    private double price;

    public void setPrice(double newPrice) {
        if (newPrice > 0) {
            this.price = newPrice;
        } else {
            this.price = 0.0;
        }
    }

    public double getPrice() {
        return price;
    }

    public static void main(String[] args) {
        Product p = new Product();
        p.setPrice(-10.0);
        p.setPrice(25.5);
        System.out.println(p.getPrice());
    }
}
Q2693 medium code error
What type of error will occur when compiling the following Java code?
java
public class Main {
    public static void main(String[] args) {
        int x = 10.5;
        System.out.println(x);
    }
}
Q2694 easy
Where is the loop condition evaluated in a do-while loop?
Q2695 hard code error
What error will this Java code produce when compiled and run?
java
public class ArrayDeclarationSyntax {
    public static void main(String[] args) {
        // This declares 'x' as an int array, but 'y' as a single int variable.
        int x[], y;
        x = new int[5];
        // Attempt to access 'y' as if it were an array
        y[0] = 10;
        System.out.println(x.length);
    }
}
Q2696 medium
A class `Child` implements `Serializable`, but its immediate superclass `Parent` does not. During the serialization of a `Child` object, what happens regarding the fields inherited from `Parent`?
Q2697 hard code output
What is the output of this code?
java
import java.util.ArrayList;

class MyObject {
    int value;
    public MyObject(int value) { this.value = value; }
    public String toString() { return "O:" + value; }
}

public class Test {
    public static void main(String[] args) {
        ArrayList<MyObject> originalList = new ArrayList<>();
        originalList.add(new MyObject(1));
        originalList.add(new MyObject(2));

        ArrayList<MyObject> clonedList = (ArrayList<MyObject>) originalList.clone();

        originalList.get(0).value = 10; 
        originalList.add(new MyObject(3)); 

        System.out.println("Original: " + originalList);
        System.out.println("Cloned: " + clonedList);
    }
}
Q2698 medium
What is the primary characteristic of the `delete()` method when attempting to delete a directory using a `File` object?
Q2699 medium
`java.util.TreeMap` primarily implements which set of interfaces, reflecting its sorted and navigable nature?
Q2700 easy code error
What is the error in this Java code?
java
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterError7 {
    public static void main(String[] args) throws IOException {
        FileWriter writer = FileWriter("another.txt"); // Missing 'new' keyword
        writer.write("Sample text.");
        writer.close();
    }
}
← Prev 133134135136137 Next → Page 135 of 200 · 3994 questions