☕ Java MCQ Questions – Page 69

Questions 1361–1380 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1361 easy code error
What is the expected error when compiling and running the following Java code?
java
public class MyClass {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Hello");
    }
}
Q1362 hard code output
What is the output of this code? (Assuming Java 21+)
java
public class SwitchTest {
    public static void main(String[] args) {
        Object obj = "Programming"; // Length 11
        String result = switch (obj) {
            case String s when s.length() > 10 -> "Long String";
            case String s when s.length() > 5 -> "Medium String";
            case String s -> "Short String";
            default -> "Not a String";
        };
        System.out.println(result);
    }
}
Q1363 easy code error
What compile-time error will occur in the `Child` class?
java
class Parent {
    public void sendNotification(String message) {
        System.out.println("Sending: " + message);
    }
}

class Child extends Parent {
    @Override
    public void sendNotification(int id) {
        System.out.println("Sending notification to ID: " + id);
    }
}
Q1364 hard
In Java, regarding definite assignment, what is the compiler's behavior when a `final` local variable is initialized within an `if-else` construct?
Q1365 medium code output
What value will the `process` method return?
java
public class ReturnInFinally {
    public static int process() {
        try {
            System.out.println("Try block");
            return 1;
        } finally {
            System.out.println("Finally block");
            return 2;
        }
    }
    public static void main(String[] args) {
        System.out.println(process());
    }
}
Q1366 hard code error
What runtime error will this Java code snippet throw?
java
public class Test {
    public static void main(String[] args) {
        int[][] original = {{1, 2}, {3, 4}};
        Object clonedObj = original.clone();
        char[][] clonedChar = (char[][]) clonedObj;
        System.out.println(clonedChar[0][0]);
    }
}
Q1367 medium code output
What is the output of this Java code?
java
import java.util.TreeMap;

public class TreeMapHigherLower {
    public static void main(String[] args) {
        TreeMap<Integer, String> map = new TreeMap<>();
        map.put(10, "ten");
        map.put(20, "twenty");
        map.put(30, "thirty");
        System.out.println(map.higherKey(20) + "," + map.lowerKey(20));
    }
}
Q1368 medium code error
What compilation error will this Java code produce?
java
public class SwitchError {
    public static void main(String[] args) {
        int day = 1;
        switch (day) {
            case 1:
                System.out.println("Monday");
                break;
            case 1:
                System.out.println("Another Monday?");
                break;
            default:
                System.out.println("Invalid day");
        }
    }
}
Q1369 easy code output
What is the output of this Java code?
java
public class Main {
    public static void main(String[] args) {
        char grade = 'A';
        System.out.println(grade);
    }
}
Q1370 hard
When iterating over a `java.util.ArrayList<Integer>` with millions of elements, what is the typical performance comparison between a traditional `for` loop using `get(index)` and an enhanced `for` (for-each) loop?
Q1371 medium
Which statement accurately describes how to "resize" a single-dimensional array in Java?
Q1372 medium code error
What is the compilation error in the provided Java code?
java
abstract class Base {
    private abstract void setup();
    public void init() {
        System.out.println("Initializing");
    }
}

class Concrete extends Base {
    // Implementation expected
}
Q1373 medium code error
What is the outcome when this Java code is executed?
java
import java.util.LinkedList;

public class Test {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Alpha");
        list.add("Beta");
        list.remove(new String[]{"Alpha"});
        System.out.println(list);
    }
}
Q1374 hard
Consider a `TreeMap<String, String>` initialized with `new TreeMap<>((s1, s2) -> s1.compareTo(s2))`. If you attempt to `put(null, "value")`, what is the outcome?
Q1375 hard code error
What error occurs when executing the following Java code?
java
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Boolean> flags = new ArrayList<>();
        flags.set(0, true); // Attempt to set an element at index 0 in an empty list
        System.out.println(flags);
    }
}
Q1376 medium code error
What is the compilation error in the following code?
java
abstract class Animal {
    abstract void makeSound();
    abstract void move() {
        System.out.println("Animal moves");
    }
}
Q1377 hard code output
What does this code print?
java
import java.io.*;

class MySerializableClass implements Serializable {
    private static final long serialVersionUID = 1L;
    private int data;
    
    static {
        System.out.println("MySerializableClass static initializer called.");
    }

    {
        System.out.println("MySerializableClass instance initializer called.");
    }

    public MySerializableClass() {
        System.out.println("MySerializableClass constructor called.");
        this.data = 100;
    }

    public MySerializableClass(int data) {
        System.out.println("MySerializableClass parameterized constructor called.");
        this.data = data;
    }

    public int getData() { return data; }
}

public class SerializationTest {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        System.out.println("--- Creating original object ---");
        MySerializableClass original = new MySerializableClass(50);
        System.out.println("Original Data: " + original.getData());

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

        System.out.println("--- Deserializing object ---");
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        MySerializableClass deserialized = (MySerializableClass) ois.readObject();
        ois.close();
        System.out.println("Deserialized Data: " + deserialized.getData());
    }
}
Q1378 easy
Which static method is typically used to pause the execution of the current thread for a specified duration?
Q1379 hard code error
What is the most likely error when executing this Java code snippet?
java
import java.io.BufferedWriter;
import java.io.StringWriter;
import java.io.IOException;

public class Test {
    public static void main(String[] args) {
        StringWriter sw = new StringWriter();
        BufferedWriter innerWriter = new BufferedWriter(sw);
        BufferedWriter outerWriter = new BufferedWriter(innerWriter);

        try {
            outerWriter.write("Hello");
            innerWriter.close(); // This closes the underlying StringWriter too
            outerWriter.write(" World"); // This should now fail
            outerWriter.flush();
        } catch (IOException e) {
            System.err.println("Chaining Error: " + e.getMessage());
        }
        System.out.println("Accumulated content: " + sw.toString());
    }
}
Q1380 hard code error
What is the result of executing the given Java code snippet?
java
import java.io.*;

public class FileWriterError8 {
    public static void main(String[] args) {
        File file = new File("flush_test.txt");
        FileWriter fw = null;
        try {
            fw = new FileWriter(file);
            fw.write("Initial content.");
            fw.close(); // Stream is closed
            fw.flush(); // Attempt to flush a closed stream
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        } finally {
            if (fw != null) {
                // No explicit close needed here as it's already closed.
            }
            file.delete();
        }
    }
}
← Prev 6768697071 Next → Page 69 of 200 · 3994 questions