☕ Java MCQ Questions – Page 77

Questions 1521–1540 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1521 medium code error
What kind of error will occur when compiling and running the following Java code?
java
public class Test {
    public static void main(String[] args) {
        outer: for (int i = 0; i < 2; i++) {
            try {
                System.out.println("Try block i: " + i);
            } finally {
                if (i == 0) {
                    continue outer;
                }
                System.out.println("Finally block i: " + i);
            }
        }
    }
}
Q1522 medium code error
What is the compilation error in the following code?
java
class Parent {
    protected String name;
    Parent(String name) { this.name = name; }
}

class Child extends Parent {
    Child(String name) {
        this.name = name; // Attempt to access name field directly
    }
}
Q1523 easy
What is the default initial capacity of a `StringBuilder` when created using the no-argument constructor `new StringBuilder()`?
Q1524 medium code error
What error does this Java code produce when executed?
java
public class Main {
    public static void main(String[] args) {
        String message = null;
        System.out.println(message.trim());
    }
}
Q1525 hard
What is the primary purpose of a 'compact constructor' in a Java Record class?
Q1526 hard code error
What is the result of compiling and running the following Java code snippet?
java
import java.io.UnsupportedEncodingException;

public class StringError {
    public static void main(String[] args) {
        String data = "sample text";
        try {
            byte[] bytes = data.getBytes("INVALID-CHARSET");
            System.out.println(bytes.length);
        } catch (UnsupportedEncodingException e) {
            System.out.println("Caught: " + e.getMessage());
        }
    }
}
Q1527 hard code error
What is the output of this code?
java
import java.io.File;
import java.io.IOException;

public class DeleteOnExitParentDeletion {
    public static void main(String[] args) {
        File parentDir = new File("dir_for_cleanup");
        File nestedFile = new File(parentDir, "nested_file.txt");

        try {
            parentDir.mkdir();
            nestedFile.createNewFile();
            
            nestedFile.deleteOnExit(); // Register only the nested file for deletion
            
            boolean parentDeleted = parentDir.delete(); // Attempt to delete the parent directory immediately
            System.out.println("Parent directory deleted immediately: " + parentDeleted);

        } catch (IOException e) {
            System.out.println("Error: " + e.getClass().getSimpleName() + ": " + e.getMessage());
        } finally {
            // The nested file will be deleted on JVM exit. The parent won't if it was not empty.
            // Manual cleanup if the program didn't exit or parent deletion failed.
            nestedFile.delete();
            parentDir.delete();
        }
    }
}
Q1528 medium
How does `HashSet` handle an attempt to add a duplicate element?
Q1529 easy code error
What is the outcome when compiling and running this Java code?
java
public class IntegerModification {
    public static void main(String[] args) {
        Integer value = 10;
        value.intValue() = 20;
        System.out.println(value);
    }
}
Q1530 easy code error
What happens when you run this Java code?
java
import java.util.Queue;
import java.util.PriorityQueue;

public class QueueError {
    public static void main(String[] args) {
        Queue<String> pq = new PriorityQueue<>();
        pq.add("apple");
        pq.add(null);
        System.out.println(pq.peek());
    }
}
Q1531 medium code output
What is the output of this Java program?
java
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class StreamTest {
    public static void main(String[] args) {
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
        Optional<String> result = names.stream()
                                      .filter(s -> s.length() > 5)
                                      .findFirst();
        System.out.println(result.orElse("No match"));
    }
}
Q1532 easy code error
What is the runtime error in the following Java code snippet?
java
import java.util.ArrayList;

public class Question5 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add(1, "Java");
        System.out.println(list);
    }
}
Q1533 easy code error
What kind of error will occur when compiling or running the following Java code snippet?
java
import java.io.File;
import java.io.IOException;

public class FileError9 {
    public static void main(String[] args) throws IOException {
        File dir = new File("myDir");
        dir.mkdir(); // Assume this succeeds
        File file = new File("myDir"); // Reusing the same path string
        file.createNewFile();
        System.out.println("File creation attempted.");
    }
}
Q1534 easy
Which keyword, when applied to a method, prevents it from being overridden by any subclass?
Q1535 medium code output
What does this Java code print to the console?
java
import java.io.BufferedWriter;
import java.io.StringWriter;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        StringWriter sw = new StringWriter();
        BufferedWriter bw = new BufferedWriter(sw);
        bw.write("Line 1");
        bw.newLine();
        bw.write("Line 2");
        bw.close();
        System.out.print(sw.toString());
    }
}
Q1536 hard
When a `break` statement is encountered within a `try` block nested inside a loop, what is the sequence of execution concerning an associated `finally` block before the loop is exited?
Q1537 medium code error
What exception will be thrown when running this Java code snippet?
java
import java.util.PriorityQueue;
import java.util.Queue;

public class QueueError {
    public static void main(String[] args) {
        Queue<Integer> pq = new PriorityQueue<>();
        pq.element(); // Attempt to retrieve but not remove from an empty queue
    }
}
Q1538 medium
Consider a `do-while` loop where the loop condition is initially `false`. How many times will the loop body execute?
Q1539 hard
Consider a statement placed immediately after an unconditional `break` statement within a loop (e.g., `while(true) { break; System.out.println("Unreachable"); }`). What is the Java compiler's assessment of this subsequent statement's reachability?
Q1540 easy
What is the primary purpose of serialization in Java?
← Prev 7576777879 Next → Page 77 of 200 · 3994 questions