☕ Java MCQ Questions – Page 83

Questions 1641–1660 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1641 hard code output
What is the output of this code?
java
public class Test {
    public static void main(String[] args) {
        String result = "";
        outer: for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                if (i == 0) {
                    if (j == 1) { result += "X"; break outer; }
                    result += "A" + j;
                } else {
                    result += "B" + j;
                }
            }
        }
        System.out.print(result);
    }
}
Q1642 easy code error
What is the result of compiling the following Java code?
java
class Greeter {
    void sayHello(String message) {
        System.out.println(message);
    }

    void sayHello(String text) {
        System.out.println(text);
    }
}
Q1643 medium code error
Examine the following Java code. What compilation error will it produce?
java
import java.util.ArrayList;
import java.util.List;

public class FinalListExample {
    public static void main(String[] args) {
        final List<String> names = new ArrayList<>();
        names.add("Alice");
        names = new ArrayList<>(); // Attempt to reassign the final list reference
        names.add("Bob");
    }
}
Q1644 medium
What is the primary difference between calling the `start()` method and directly calling the `run()` method on a `Thread` object in Java?
Q1645 hard
Beyond the memory consumed by storing actual `key-value` pairs, what significant memory overhead does a `HashMap` typically incur?
Q1646 medium code error
What is the compilation error in the following code?
java
class Base {
    public final void methodA() {
        System.out.println("Base methodA");
    }
}

class Derived extends Base {
    @Override
    public void methodA() {
        System.out.println("Derived methodA");
    }
}
Q1647 hard code error
What is the compilation error in the provided Java code?
java
class Test {
    public static void main(String[] args) {
        final int result;
        do {
            if (Math.random() > 0.5) {
                result = 100;
            }
        } while (false); // Loop executes exactly once
        System.out.println(result);
    }
}
Q1648 easy
Which of the following describes a key characteristic of a custom *unchecked* exception?
Q1649 hard code error
What is the runtime error when executing this Java code snippet?
java
import java.util.Comparator;
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        Comparator<String> customComparator = (s1, s2) -> {
            if (s1.length() == s2.length()) {
                return s1.compareTo(s2);
            }
            return s1.length() - s2.length();
        };

        TreeSet<String> set = new TreeSet<>(customComparator);
        set.add("A");
        set.add(null);
        System.out.println(set.size());
    }
}
Q1650 hard
An `ArrayList` is initialized with `new ArrayList<Integer>(5)`. Then 8 elements are added, causing it to resize. After this, `trimToSize()` is called. What is the exact internal capacity of the `ArrayList` immediately after `trimToSize()`?
Q1651 hard code error
What is the runtime error when executing this Java code snippet?
java
import java.util.Comparator;
import java.util.TreeSet;

class RecursiveCompare {
    int value;
    RecursiveCompare(int value) { this.value = value; }
}

public class Main {
    public static void main(String[] args) {
        Comparator<RecursiveCompare> buggyComparator = (a, b) -> {
            return buggyComparator.compare(b, a); // Infinite recursion
        };
        TreeSet<RecursiveCompare> set = new TreeSet<>(buggyComparator);
        set.add(new RecursiveCompare(1));
        set.add(new RecursiveCompare(2));
        System.out.println(set.size());
    }
}
Q1652 hard
Regarding the `final` and `abstract` modifiers in Java, which statement is true?
Q1653 medium
What does `Optional.empty()` represent?
Q1654 easy code output
What will be printed when this Java code is executed?
java
public class Main {
    public static void main(String[] args) {
        int x = 10;
        x = 20;
        System.out.println(x);
    }
}
Q1655 hard code output
What does this Java code snippet print?
java
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class CreateFileError {
    public static void main(String[] args) {
        Path tempDir = null;
        try {
            tempDir = Files.createTempDirectory("parent_test");
            File subDirFile = new File(tempDir.toFile(), "nonExistentSubDir/newFile.txt");
            
            System.out.println("Parent directory exists: " + subDirFile.getParentFile().exists());
            boolean created = subDirFile.createNewFile();
            System.out.println("File created: " + created);
        } catch (IOException e) {
            System.out.println("Caught Exception: " + e.getClass().getSimpleName());
            System.out.println("Message contains 'directory': " + e.getMessage().contains("directory"));
        } finally {
            if (tempDir != null) {
                try { Files.deleteIfExists(tempDir); } catch (IOException e) { /* ignore */ }
            }
        }
    }
}
Q1656 medium
What is the fundamental difference in control flow between using `throw` and `return` in a method?
Q1657 easy
Which method checks if a string contains a specified sequence of characters?
Q1658 easy code output
What is the output of this Java code snippet?
java
import java.io.*;

class Inner implements Serializable {
    private String innerValue;
    public Inner(String v) { this.innerValue = v; }
    public String getInnerValue() { return innerValue; }
}

class Outer implements Serializable {
    private String outerValue;
    private Inner innerObject;

    public Outer(String o, String i) {
        this.outerValue = o;
        this.innerObject = new Inner(i);
    }
    public String getCombinedValue() {
        return outerValue + "-" + innerObject.getInnerValue();
    }
}

public class Main {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Outer outer = new Outer("OuterData", "InnerData");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(outer);
        oos.close();

        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        Outer deserializedOuter = (Outer) ois.readObject();
        ois.close();

        System.out.println(deserializedOuter.getCombinedValue());
    }
}
Q1659 medium
If a variable is declared within the body of a `while` loop (e.g., `int x = 0;`), what is its scope?
Q1660 medium
Which built-in functional interface is best suited for scenarios where you need to generate or supply a value without taking any input?
← Prev 8182838485 Next → Page 83 of 200 · 3994 questions