☕ Java MCQ Questions – Page 87

Questions 1721–1740 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1721 medium
Which of the following correctly demonstrates the creation and use of an anonymous array in Java?
Q1722 hard code output
What is the output of this code?
java
import java.io.*;

public class Test {
    public static void main(String[] args) throws IOException {
        String data = "1\n\n3\n";
        BufferedReader br = new BufferedReader(new StringReader(data));
        System.out.println("[" + br.readLine() + "]");
        System.out.println("[" + br.readLine() + "]");
        System.out.println("[" + br.readLine() + "]");
        System.out.println("[" + br.readLine() + "]");
        br.close();
    }
}
Q1723 hard code output
What is the output of this code?
java
class MyBrokenException extends Exception {
    public MyBrokenException(String message) {
        super(message);
        if (message.contains("fatal")) {
            throw new IllegalStateException("Internal error!");
        }
    }
}
public class Main {
    public static void main(String[] args) {
        try {
            throw new MyBrokenException("fatal error");
        } catch (MyBrokenException e) {
            System.out.println("Caught MyBrokenException: " + e.getMessage());
        } catch (IllegalStateException e) {
            System.out.println("Caught IllegalStateException: " + e.getMessage());
        } finally {
            System.out.println("Finally block.");
        }
    }
}
Q1724 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 = "A\nB\nC";
        BufferedReader br = new BufferedReader(new StringReader(data), 2);
        br.mark(1);
        System.out.print((char)br.read()); // A
        System.out.print((char)br.read()); // \n
        try {
            br.reset();
        } catch (IOException e) {
            System.out.print("RESET_ERROR");
        }
        System.out.print((char)br.read()); // B
        br.close();
    }
}
Q1725 medium code error
Identify the compilation error in the provided Java code, which attempts to overload methods.
java
public class OverloadChecker {
    public void processData(String s) {
        System.out.println("Public: " + s);
    }

    private void processData(String s) { // This line causes the error
        System.out.println("Private: " + s);
    }

    public static void main(String[] args) {
        OverloadChecker oc = new OverloadChecker();
        oc.processData("test");
    }
}
Q1726 medium
What will be the output of the following Java code snippet? `String str = "Hello World";` `System.out.println(str.substring(6, 11));`
Q1727 hard code output
What is the output of this code?
java
class Animal {
    public static void sound() {
        System.out.println("Animal makes a sound");
    }
    public void eat() {
        System.out.println("Animal eats");
    }
}

class Dog extends Animal {
    public static void sound() {
        System.out.println("Dog barks");
    }
    @Override
    public void eat() {
        System.out.println("Dog eats kibble");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myDog = new Dog();
        myDog.sound();
        myDog.eat();
    }
}
Q1728 hard code error
What is the primary flaw or error in this Java code related to the while loop's termination condition?
java
public class LogicalInfiniteLoop {
    public static void main(String[] args) {
        double x = 0.1;
        while (x != 1.0) {
            x += 0.1;
            System.out.println(x);
        }
        System.out.println("Loop finished.");
    }
}
Q1729 hard
Given `byte b = -1;`, what is the value of `b >>> 4` in Java?
Q1730 medium code output
What does this code print?
java
public class ResourceCleanup {
    static class MyResource implements AutoCloseable {
        public void open() { System.out.println("Resource opened"); }
        @Override
        public void close() { System.out.println("Resource closed"); }
    }

    public static void main(String[] args) {
        MyResource res = null;
        try {
            res = new MyResource();
            res.open();
            throw new RuntimeException("Error during processing");
        } catch (Exception e) {
            System.out.println("Caught exception: " + e.getMessage());
        } finally {
            if (res != null) {
                res.close();
            }
        }
    }
}
Q1731 hard
What is the primary purpose of the `trimToSize()` method in `StringBuffer`?
Q1732 medium
What is the primary benefit of constructor overloading in Java?
Q1733 medium
What is the most efficient and recommended method to copy elements from one array to another (existing) array in Java?
Q1734 hard
If a class `PoorHashObject` defines its `hashCode()` method to always return a constant value (e.g., `return 42;`) while still correctly implementing `equals()`, what is the asymptotic time complexity for `add()` and `contains()` operations in a `HashSet` containing `N` instances of `PoorHashObject`?
Q1735 easy code output
What is the output of this code?
java
class Base {
    Base() {
        System.out.print("Base constructor");
    }
}

class Derived extends Base {
    Derived() {
        // super(); is implicitly called here
        System.out.print("Derived constructor");
    }
}

public class Main {
    public static void main(String[] args) {
        Derived d = new Derived();
    }
}
Q1736 easy
If a thread attempts to acquire an object's monitor lock that is already held by another thread, what state does the attempting thread enter?
Q1737 easy
What is an object in Java?
Q1738 hard code output
What does this code print?
java
public class OperatorChallenge {
    public static void main(String[] args) {
        Object o = true ? new Integer(1) : new Double(2.0);
        System.out.println(o.getClass().getName());
    }
}
Q1739 easy
Is it possible to cast a `char` to an `int` implicitly?
Q1740 hard code output
What is the output of this code?
java
import java.util.function.Consumer;

public class MethodRefAmbiguity {
    public static void print(String s) { System.out.println("String: " + s); }
    public static void print(Object o) { System.out.println("Object: " + o); }

    public static void main(String[] args) {
        Consumer<Object> c = MethodRefAmbiguity::print;
        c.accept("Hello");
    }
}
← Prev 8586878889 Next → Page 87 of 200 · 3994 questions