☕ Java MCQ Questions – Page 41

Questions 801–820 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q801 easy code error
What is the error in this code?
java
import java.util.TreeMap;

public class Test {
    public static void main(String[] args) {
        TreeMap<Integer, String> map = new TreeMap<>();
        map.put(1, "One");
        map.put(null, "Zero"); // Autoboxing null key
        System.out.println(map.containsKey(null));
    }
}
Q802 easy
Which of the following represents the correct order of components in a traditional Java `for` loop header?
Q803 medium
Which character encoding does `FileWriter` use by default if not explicitly specified?
Q804 hard code output
What does this code print?
java
import java.io.*;

public class Test {
    public static void main(String[] args) throws IOException {
        String data = "Line1\nLine2\nLine3";
        BufferedReader br = new BufferedReader(new StringReader(data));
        br.readLine();
        br.skip(1);
        System.out.println(br.readLine());
        br.close();
    }
}
Q805 medium code output
What does this code print?
java
import java.util.TreeMap;

public class TreeMapNullKey {
    public static void main(String[] args) {
        TreeMap<String, String> map = new TreeMap<>();
        map.put("key1", "value1");
        map.put("key2", "value2");
        map.put(null, "value3");
        System.out.println("Map size: " + map.size());
    }
}
Q806 hard code output
What does this code print?
java
public class Test {
    public static void main(String[] args) {
        try {
            try {
                System.out.print("Try-inner ");
                throw new RuntimeException("Exception from try");
            } finally {
                System.out.print("Finally-inner ");
                throw new Error("Error from finally");
            }
        } catch (Exception e) {
            System.out.print("Catch-outer " + e.getMessage());
        } catch (Error e) {
            System.out.print("Catch-error " + e.getMessage());
        } finally {
            System.out.print(" Finally-outer");
        }
    }
}
Q807 easy code error
What error will occur when this code is executed?
java
public class Main {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Testing");
        sb.delete(5, 2);
        System.out.println(sb);
    }
}
Q808 medium
You have an `Optional<User>` and want to transform it into an `Optional<String>` containing the user's email if the `User` is present. Which `Optional` method is most appropriate for this transformation?
Q809 hard code error
What error will this code produce at compile time?
java
public class ContinueLabeledBlock {
    public static void main(String[] args) {
        int counter = 0;
        myBlock: {
            for (int i = 0; i < 3; i++) {
                if (i == 1) {
                    continue myBlock;
                }
                counter++;
            }
        }
        System.out.println(counter);
    }
}
Q810 hard code error
What compilation error will occur when compiling the following Java code?
java
public class LambdaCaptureError {
    public static void main(String[] args) {
        int counter = 0;
        // counter++; // Uncommenting this line would make it non-effectively final
        Runnable r = () -> {
            // counter++; // This line causes the error
            System.out.println(counter);
        };
        r.run();
    }
}
Q811 hard code error
What is the runtime error when `main` method is executed?
java
class MaliciousException extends RuntimeException {
    public MaliciousException(String message) {
        super(message);
    }

    @Override
    public synchronized Throwable fillInStackTrace() {
        // Intentionally throwing an exception during stack trace filling
        throw new IllegalStateException("Filling stack trace failed intentionally!"); // This line causes the runtime error
    }
}

public class Main {
    public static void main(String[] args) {
        // Instantiation of MaliciousException triggers its fillInStackTrace() method
        MaliciousException me = new MaliciousException("Original problem");
    }
}
Q812 hard code error
What runtime error will this Java code snippet throw?
java
public class Test {
    public static void main(String[] args) {
        int[][] matrix = new int[3][];
        matrix[0] = new int[2];
        matrix[1][0] = 5;
        System.out.println(matrix[1][0]);
    }
}
Q813 medium
What is the effect of the `continue` statement when encountered inside a `while` loop?
Q814 easy
To convert all characters in a String to uppercase, which method would you use?
Q815 easy
In Java, what type of entity is an array?
Q816 hard
In Java, what is the most precise rule regarding checked exceptions when overriding an instance method in a subclass, particularly concerning polymorphism?
Q817 medium code output
What does this code print?
java
interface MessagePrinter {
    void print();
}

public class LambdaThisScope {
    private String message = "Outer class message";

    public void runPrinter() {
        MessagePrinter printer = () -> {
            System.out.println(this.message);
        };
        printer.print();
    }

    public static void main(String[] args) {
        new LambdaThisScope().runPrinter();
    }
}
Q818 medium code error
What error will occur when running this Java code snippet?
java
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public class Test {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("existing", "present");

        // Attempt to use computeIfAbsent with a null mapping function
        String result = map.computeIfAbsent("newKey", null); 
        System.out.println(result);
    }
}
Q819 medium code error
What is the compilation error in the following Java code?
java
public class InvalidName {
    public static void main(String[] args) {
        int 1number = 100;
        System.out.println(1number);
    }
}
Q820 hard
Which statement correctly differentiates the effect of the `final` keyword when applied to a primitive data type versus a reference data type in Java?
← Prev 3940414243 Next → Page 41 of 200 · 3994 questions