☕ Java MCQ Questions – Page 137

Questions 2721–2740 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2721 hard code output
What is the output of this code?
java
public class Test {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("abacaba");
        int idx1 = sb.indexOf("a", 1);
        int idx2 = sb.indexOf("b", 3);
        System.out.println(idx1 + " " + idx2);
    }
}
Q2722 easy code output
What is the output of this Java code?
java
import java.util.LinkedList;

public class MyClass {
    public static void main(String[] args) {
        LinkedList<Integer> nums = new LinkedList<>();
        nums.add(1);
        nums.add(2);
        nums.add(3);
        nums.remove(1);
        System.out.println(nums.size());
    }
}
Q2723 medium code error
What is the exception printed to the console when the following Java code is executed, assuming 'nonwritable_dir' is a path where the user lacks write permissions?
java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Test {
    public static void main(String[] args) {
        String filePath = "/path/to/nonwritable_dir/output.txt"; // Assume this path causes permission issues
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            writer.write("Attempt to write");
        } catch (IOException e) {
            System.out.println(e.getClass().getSimpleName() + ": " + e.getMessage());
        }
    }
}
Q2724 easy
How do you find the number of elements in a single-dimensional array named `myArray`?
Q2725 hard code output
What is the output of this code?
java
import java.io.IOException;

class MyResource implements AutoCloseable {
    public void close() throws IOException {
        System.out.print("Closing ");
        throw new IOException("Resource close failed");
    }
}

public class Test {
    public static void main(String[] args) {
        try (MyResource res = new MyResource()) {
            System.out.print("In try ");
            throw new RuntimeException("Try block failed");
        } catch (Exception e) {
            System.out.print("Caught: " + e.getMessage());
            if (e.getSuppressed().length > 0) {
                System.out.print(" Suppressed: " + e.getSuppressed()[0].getMessage());
            }
        }
    }
}
Q2726 hard code error
What error occurs when attempting to compile the following Java code?
java
public class InvalidIfConditionType {
    public static void main(String[] args) {
        boolean active = true;
        int count = 5;
        if (active && count) { // 'count' (int) cannot be implicitly converted to boolean
            System.out.println("Active and count present.");
        } else {
            System.out.println("Inactive or no count.");
        }
    }
}
Q2727 hard code output
What is the output of this Java program?
java
public class SleepInterruptStatus {
    public static void main(String[] args) throws InterruptedException {
        Thread sleeper = new Thread(() -> {
            try {
                System.out.println("Sleeper: Going to sleep.");
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                System.out.println("Sleeper: Interrupted while sleeping!");
                System.out.println("Sleeper: Is interrupted after catch: " + Thread.currentThread().isInterrupted());
            }
            System.out.println("Sleeper: Finishing.");
        });

        sleeper.start();
        Thread.sleep(100);
        sleeper.interrupt();
        sleeper.join();
    }
}
Q2728 hard
Which of the following `Spliterator` characteristics would a `java.util.LinkedList`'s spliterator generally possess?
Q2729 hard
Why does `TreeSet` rely on the `compareTo()` (or `compare()`) method for ordering and uniqueness rather than the `equals()` and `hashCode()` methods, unlike `HashSet`?
Q2730 easy
Which method of `BufferedWriter` is used to write a platform-independent new line character?
Q2731 medium code error
What is the error in the following Java code?
java
package com.example;

class Car {
    private String model;

    public Car(String model) {
        this.model = model;
    }

    private void displayModel() {
        System.out.println("Model: " + model);
    }
}

public class Showroom {
    public static void main(String[] args) {
        Car myCar = new Car("Tesla Model 3");
        myCar.displayModel();
    }
}
Q2732 hard
What happens if a `continue` statement is placed directly inside a `case` block of a `switch` statement that is not itself enclosed within any loop?
Q2733 easy code output
What does this Java code print?
java
public class LoopTest {
    public static void main(String[] args) {
        int count = 2;
        for (int i = 0; i < count; i++) {
            System.out.print("*");
        }
    }
}
Q2734 hard code error
What is the result of executing this Java code snippet?
java
interface Shape { }
class Circle implements Shape { }
class Square implements Shape { }

public class CastingPuzzle {
    public static void main(String[] args) {
        Shape s = new Circle();
        Square sq = (Square) s;
        System.out.println("No error");
    }
}
Q2735 hard
If a constructor in a superclass declares a checked exception, e.g., `public SuperClass() throws IOException`, what must be true for a subclass constructor?
Q2736 hard code error
What is the compilation error, if any, for the provided Java code snippet?
java
class Overloader {
    public void inspect(String s) {}
    public void inspect(String[] arr) {}

    public static void main(String[] args) {
        Overloader o = new Overloader();
        o.inspect(null); // Line X
    }
}
Q2737 easy code output
What does this Java code print?
java
public class TypeCast {
    public static void main(String[] args) {
        char letter = 'C';
        int asciiValue = letter;
        System.out.println(asciiValue);
    }
}
Q2738 hard
Under what circumstances might it be beneficial to explicitly create a `BufferedReader` with a custom, significantly larger buffer size (e.g., `new BufferedReader(reader, 65536)`), rather than relying on the default buffer size?
Q2739 hard
The Liskov Substitution Principle (LSP) is a core principle underpinning robust polymorphism. Which statement best captures the essence of LSP in object-oriented design?
Q2740 easy
How must a constructor be named in Java?
← Prev 135136137138139 Next → Page 137 of 200 · 3994 questions