☕ Java MCQ Questions – Page 33

Questions 641–660 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q641 easy
Which interface does `java.util.HashSet` primarily implement?
Q642 easy
Which class is primarily used to read a serialized object from an `InputStream`?
Q643 hard code output
What is the output of this code?
java
class A {
    public static void print() { System.out.println("A Static"); }
    public void display() { System.out.println("A Instance"); }
}
class B extends A {
    public static void print() { System.out.println("B Static"); }
    public void display() { System.out.println("B Instance"); }
}
public class Test {
    public static void main(String[] args) {
        A obj = new B();
        obj.print();
        obj.display();
    }
}
Q644 easy code output
What does this code print?
java
class CustomError extends Exception {
    public CustomError(String msg) {
        super(msg);
    }
}

public class Main {
    public static void main(String[] args) {
        try {
            throw new CustomError("Detail specific error");
        } catch (CustomError e) {
            System.out.println("Caught specific: " + e.getMessage());
        } catch (Exception e) {
            System.out.println("Caught generic: " + e.getMessage());
        }
    }
}
Q645 easy code output
What is the output of this code?
java
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<String, String> userPreferences = new HashMap<>();
        userPreferences.put("Background", "Blue");
        userPreferences.put("Font", null);
        System.out.println(userPreferences.get("Font"));
    }
}
Q646 medium code error
What compilation error will occur in the `DataLoader` class?
java
public class CustomDataException extends Exception {
    public CustomDataException(String message) {
        super(message);
    }
}

public class DataLoader {
    public void load(String path) {
        if (path == null || path.isEmpty()) {
            throw new CustomDataException("Path cannot be empty.");
        }
        System.out.println("Loading data from: " + path);
    }

    public static void main(String[] args) {
        DataLoader loader = new DataLoader();
        loader.load(null);
    }
}
Q647 easy code error
What is the error in the following Java code?
java
public class Main {
    public static void main(String[] args) {
        double d = 10.5;
        int i = d;
        System.out.println(i);
    }
}
Q648 hard
Consider a class `ConcreteClass` that implements `InterfaceA` but does not implement `InterfaceB`. If you have `Object obj = new ConcreteClass();`, which of the following cast attempts will compile but throw a `ClassCastException` at runtime?
Q649 medium code output
What does this Java code snippet print to the console?
java
class OverrideThrows {
    static class Super {
        void action() throws java.io.IOException {
            System.out.println("Super action");
        }
    }

    static class Sub extends Super {
        @Override
        void action() throws java.io.FileNotFoundException { // Can throw narrower exception
            System.out.println("Sub action");
        }
    }

    public static void main(String[] args) {
        try {
            Super obj = new Sub();
            obj.action();
        } catch (java.io.IOException e) {
            System.out.println("Caught IO Exception");
        }
    }
}
Q650 easy code error
What compile-time error will occur when compiling this Java code?
java
public class MyClass {
    public static void main(String[] args) {
        boolean flag = true;
        switch (flag) {
            case true:
                System.out.println("It's true");
                break;
            default:
                System.out.println("It's false");
        }
    }
}
Q651 easy
How can you use an enhanced `for` loop (for-each loop) to print each element of a `String` array named `words`?
Q652 easy code error
What type of error will occur when compiling or running this Java code snippet?
java
public class ArrayTest {
    public static void main(String[] args) {
        int[] data = null;
        System.out.println(data[0]);
    }
}
Q653 easy
Which keyword is used within a subclass to explicitly call the superclass's version of an overridden method?
Q654 easy code error
What error will this code produce?
java
public class LoopError {
    public static void main(String[] args) {
        int limit = 5;
        for (int i = 0; j < limit; i++) {
            System.out.println(i);
        }
    }
}
Q655 medium
In which of the following scenarios is a `while` loop generally considered more appropriate than a `for` loop?
Q656 easy code output
What is the output of this Java code?
java
import java.util.function.Consumer;

public class Main {
    public static void printStatic(String text) {
        System.out.print("Static: " + text);
    }

    public static void main(String[] args) {
        Consumer<String> consumer = Main::printStatic;
        consumer.accept("Message");
    }
}
Q657 medium
When an `ArrayBlockingQueue` (a bounded queue) is full, what is the behavior of its `add()` method?
Q658 medium code output
What is the output of this code snippet?
java
public class StringTest {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = new String("Hello");
        String str3 = "hello";
        System.out.println(str1.equals(str2) + "," + str1.equalsIgnoreCase(str3));
    }
}
Q659 medium code output
What is the output of the given Java program?
java
import java.util.Arrays;
import java.util.List;

public class StreamMapReduceLambda {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("a", "bb", "ccc");
        int totalLength = words.stream()
                                  .mapToInt(String::length) // Method reference
                                  .sum();
        System.out.println(totalLength);
    }
}
Q660 medium code error
What is the expected outcome when compiling and running the following Java code?
java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Test {
    public static void main(String[] args) {
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"), -1);
            writer.write("Content");
            writer.close();
        } catch (IOException e) {
            System.out.println(e.getClass().getSimpleName() + ": " + e.getMessage());
        } catch (IllegalArgumentException e) {
            System.out.println(e.getClass().getSimpleName() + ": " + e.getMessage());
        }
    }
}
← Prev 3132333435 Next → Page 33 of 200 · 3994 questions