☕ Java MCQ Questions – Page 40

Questions 781–800 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q781 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<String> masterList = new ArrayList<>();
        masterList.add("A");
        masterList.add("B");
        masterList.add("C");
        masterList.add("D");

        List<String> sub = masterList.subList(1, 3); // View of [B, C]
        masterList.add("E"); // Modifies masterList
        for (String s : sub) { // Iterate over subList after masterList modification
            System.out.print(s + " ");
        }
    }
}
Q782 easy
Which operator is used to check if two values are NOT equal in Java?
Q783 easy code output
What is the output of this Java code snippet?
java
public class ArrayTest {
    public static void main(String[] args) {
        double[] temperatures = {25.5, 28.0, 22.3};
        System.out.println(temperatures[0] + temperatures[1]);
    }
}
Q784 medium
Which interface does `ArrayDeque` implement, allowing it to function efficiently as both a `Queue` and a `Stack`?
Q785 hard
Can a `private` method in a superclass be overridden by a subclass?
Q786 easy code error
What is the outcome when compiling and running this Java code?
java
import java.util.List;
public class ImmutableListModification {
    public static void main(String[] args) {
        List<String> names = List.of("Alice", "Bob");
        names.add("Charlie");
        System.out.println(names);
    }
}
Q787 hard code output
What is printed when the `main` method is executed?
java
class A {
    static { System.out.println("Static A"); }
    { System.out.println("Instance A"); }
    public A() { System.out.println("Constructor A"); }
}
class B extends A {
    static { System.out.println("Static B"); }
    { System.out.println("Instance B"); }
    public B() { System.out.println("Constructor B"); }
}
public class Main {
    public static void main(String[] args) {
        new B();
    }
}
Q788 hard code output
What is the output of this code?
java
public class Test {
    public static void main(String[] args) {
        int count = 0;
        for (int i = 0; i < 5; i++) {
            if (i % 2 == 0) { count++; continue; }
            if (count > 2) { break; }
            count += i;
        }
        System.out.print(count);
    }
}
Q789 medium
What is the typical average time complexity for the `add()`, `remove()`, and `contains()` operations in a `HashSet`?
Q790 easy
Are static fields of a class included in the default serialization process?
Q791 easy code output
What is the output of this code?
java
public class MyClass {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Hello");
        sb.reverse();
        System.out.print(sb);
    }
}
Q792 medium
Consider a `while` loop that iterates based on a counter variable. Which action is most critical to prevent an infinite loop?
Q793 easy code output
What does this Java code print?
java
public class LoopTest {
    public static void main(String[] args) {
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 1; j++) {
                System.out.print(i + "" + j);
            }
        }
    }
}
Q794 medium
After calling `thread.start()` on a newly created thread, what state does the thread immediately transition to?
Q795 medium code error
What error does this Java code produce when executed?
java
public class Main {
    public static void main(String[] args) {
        String data = null;
        System.out.println(data.toUpperCase());
    }
}
Q796 hard
Consider a scenario where *two different threads* independently iterate over the *same instance* of a standard `ArrayList`'s `Iterator` (e.g., `Iterator<T> it = list.iterator();`). Which statement accurately describes the potential issues or behavior?
Q797 hard code error
What kind of error will occur when compiling or running the following Java code?
java
public class SBError {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("abcdefgh"); // Length 8
        // Attempt to delete from index 5 to 3 (start > end)
        sb.delete(5, 3);
        System.out.println(sb);
    }
}
Q798 hard code output
What is the output of this code?
java
class Parent {
    String name = "Parent Name";
    static String type = "Parent Type";
}
class Child extends Parent {
    String name = "Child Name";
    static String type = "Child Type";
}
public class Main {
    public static void main(String[] args) {
        Parent p = new Child();
        Child c = new Child();
        System.out.println(p.name + " " + p.type);
        System.out.println(c.name + " " + c.type);
    }
}
Q799 easy code error
What is the compilation error in the following Java code snippet?
java
public class LoopError {
    public static void main(String[] args) {
        do {
            int x = 0;
            System.out.println(x++);
        } while (x < 5);
    }
}
Q800 medium code error
What is the compile-time or runtime error in the following Java code snippet when `main` method is executed?
java
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;

class Item implements Serializable {
    String name = "Laptop";
}

public class SerializationQuestion4 {
    public static void main(String[] args) throws IOException {
        Item myItem = new Item();
        // Trying to serialize to a non-existent directory without creating it
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("nonexistentdir/item.ser"))) {
            oos.writeObject(myItem);
        }
    }
}
← Prev 3839404142 Next → Page 40 of 200 · 3994 questions