☕ Java MCQ Questions – Page 146

Questions 2901–2920 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2901 hard
When using instances of a mutable class as elements in a `HashSet`, what is the most robust design principle regarding the `equals()` and `hashCode()` methods to prevent unexpected behavior and maintain the integrity of the set?
Q2902 easy code output
What is the output of this code?
java
public class MyClass {
    public static void main(String[] args) {
        int day = 3;
        switch (day) {
            case 1: System.out.print("Monday"); break;
            case 2: System.out.print("Tuesday"); break;
            case 3: System.out.print("Wednesday"); break;
            default: System.out.print("Unknown");
        }
    }
}
Q2903 easy code output
What does this code print?
java
interface Playable {
    void play();
}

class Song implements Playable {
    @Override
    public void play() {
        System.out.println("Playing music.");
    }
}

public class Main {
    public static void main(String[] args) {
        Song mySong = new Song();
        mySong.play();
    }
}
Q2904 easy
Which method is used to retrieve and remove the head of a Queue, returning null if the queue is empty?
Q2905 easy
Which of the following describes the type of inheritance Java supports directly?
Q2906 easy
If the specified file does not exist when a `FileWriter` is instantiated, what happens by default?
Q2907 easy code error
What is the compilation error in the `Main` class?
java
class Book {
    String title;
    public Book(String t) {
        this.title = t;
    }
}

public class Main {
    public static void main(String[] args) {
        Book myBook = new Book();
        System.out.println(myBook.title);
    }
}
Q2908 hard
A `Serializable` singleton class implements `private Object readResolve()` method. What is the primary purpose of this method in this context?
Q2909 medium
What does the `ceilingKey(K key)` method of `TreeMap` return?
Q2910 easy code error
What kind of error will occur when compiling this Java code?
java
public class Main {
    public static void main(String[] args) {
        int value = 5;
        if (value > 0) {
            System.out.println("Positive");
        else {
            System.out.println("Non-positive");
        }
    }
}
Q2911 medium
What is the primary purpose of using a labeled `continue` statement in Java?
Q2912 hard code output
What does this code print?
java
import java.util.TreeSet;
import java.util.Comparator;

public class App {
    public static void main(String[] args) {
        TreeSet<String> set = new TreeSet<>(new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                if (s1 == null && s2 == null) return 0;
                if (s1 == null) return -1;
                if (s2 == null) return 1;
                return s1.compareTo(s2.toLowerCase()); // Case-insensitive comparison
            }
        });
        set.add("Apple");
        set.add(null);
        set.add("banana");
        set.add("apple"); // Case-insensitive duplicate
        System.out.println(set);
    }
}
Q2913 easy code error
What kind of error will occur when compiling the following Java code snippet?
java
import java.io.File;
import java.io.FileReader;

public class FileError4 {
    public static void main(String[] args) {
        File file = new File("input.txt");
        FileReader reader = new FileReader(file);
        System.out.println("Reader created.");
    }
}
Q2914 medium code error
What is the compilation error in the following Java code?
java
public class DoubleToInt {
    public static void main(String[] args) {
        double decimalValue = 123.45;
        int wholeNumber = decimalValue;
        System.out.println(wholeNumber);
    }
}
Q2915 easy code error
What compile-time error will this code produce?
java
class MyProcessor implements Runnable {
    protected void run() {
        System.out.println("Processing data...");
    }
}

public class Main {
    public static void main(String[] args) {
        MyProcessor processor = new MyProcessor();
        new Thread(processor).start();
    }
}
Q2916 medium code output
What does this Java code print?
java
import java.util.TreeSet;

public class Test {
    public static void main(String[] args) {
        TreeSet set = new TreeSet<>(); 
        set.add(10);
        try {
            set.add("hello"); 
            System.out.println(set);
        } catch (ClassCastException e) {
            System.out.println("ClassCastException caught!");
        }
    }
}
Q2917 easy
Which part of a method signature MUST differ for method overloading to be valid in Java?
Q2918 hard
Consider a `TreeSet` populated with custom objects where the `Comparator` used to order them returns 0 for two objects, `A` and `B`, even though `A.equals(B)` evaluates to `false`. What is the consequence of this `Comparator`'s behavior within the `TreeSet`?
Q2919 easy code error
What is wrong with this Java code?
java
import java.util.function.Runnable;

public class Test {
    public static void main(String[] args) {
        Runnable r = () -> 
            System.out.println("Line 1");
            System.out.println("Line 2");
    }
}
Q2920 hard code error
What error occurs when running this Java code?
java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class ForEachRemainingCME {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>(Arrays.asList("A", "B", "C"));
        Iterator<String> iterator = list.iterator();
        list.add("D"); // External modification
        iterator.forEachRemaining(System.out::print);
    }
}
← Prev 144145146147148 Next → Page 146 of 200 · 3994 questions