☕ Java MCQ Questions – Page 85

Questions 1681–1700 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1681 hard code output
What is the output of this code?
java
class MyCustomException extends Exception { public MyCustomException(String msg) { super(msg); } }
class MyCustomError extends Error { public MyCustomError(String msg) { super(msg); } }
public class Main {
    public static void riskyOperation() throws MyCustomException { throw new MyCustomException("Operation failed"); }
    public static void callerMethod() {
        try { riskyOperation(); }
        catch (MyCustomException e) { System.out.println("Caught Exception in caller: " + e.getMessage()); throw new MyCustomError("Critical failure!"); }
        finally { System.out.println("Finally in caller."); }
    }
    public static void main(String[] args) {
        try { callerMethod(); }
        catch (MyCustomError e) { System.out.println("Caught Error in main: " + e.getMessage()); }
        catch (Throwable t) { System.out.println("Caught general Throwable: " + t.getClass().getSimpleName() + ": " + t.getMessage()); }
    }
}
Q1682 medium code error
Which exception will be thrown when executing this Java code?
java
public class StringError8 {
    public static void main(String[] args) {
        String line = "apple,banana";
        int commaIndex = line.indexOf(';'); // returns -1
        String fruit = line.substring(0, commaIndex);
        System.out.println(fruit);
    }
}
Q1683 easy code error
What compile-time error will occur in the `Child()` constructor?
java
class Parent {
    Parent() { System.out.println("Parent"); }
}
class Child extends Parent {
    Child() {
        System.out.println("Child");
        super(); // Not the first statement
    }
}
public class Main {
    public static void main(String[] args) {
        Child obj = new Child();
    }
}
Q1684 hard
From the `NavigableSet` interface implemented by `TreeSet`, which of the following methods return `null` if no element satisfies the condition, instead of throwing an exception?
Q1685 medium
What is the primary condition for successfully overloading a method in Java?
Q1686 hard code output
What is the output of this code?
java
public class OperatorChallenge {
    public static void main(String[] args) {
        byte b = 126;
        b += 1;
        b += 1;
        System.out.println(b);
    }
}
Q1687 easy
Which Java primitive data type is used to store whole numbers (integers) without decimal points?
Q1688 medium
If a `switch` statement in Java does not have a `default` case and none of the `case` labels match the `switch` expression, what will happen?
Q1689 hard code error
Which exception is thrown when the `main` method of this Java code is executed?
java
public class StringFormatError {
    public static void main(String[] args) {
        long val = 123L;
        try {
            String formatted = String.format("Value: %d, Text: %s", "abc", val);
            System.out.println(formatted);
        } catch (Exception e) {
            System.out.println(e.getClass().getSimpleName());
        }
    }
}
Q1690 hard code error
What is the error encountered when running this Java code?
java
public class ArrayStoreError {
    public static void main(String[] args) {
        Number[] numbers = new Integer[5];
        numbers[0] = 10; // Auto-boxes to Integer, valid
        numbers[1] = 3.14; // Auto-boxes to Double
        System.out.println(numbers[1]);
    }
}
Q1691 medium
When an `int` primitive variable is assigned to another `int` primitive variable, what fundamentally happens?
Q1692 easy code error
What will happen when this Java code is compiled and executed?
java
class MyClass {
    public void doSomething() {
        throw new java.io.IOException("Error occurred");
    }
}
Q1693 hard code error
Analyze the following Java code. What compilation error will it produce?
java
public class BlankFinalInstance {
    final int id; // Blank final instance variable

    public BlankFinalInstance(int id) {
        this.id = id;
    }

    public BlankFinalInstance() {
        // Constructor does not initialize 'id'
    }

    public static void main(String[] args) {
        BlankFinalInstance obj = new BlankFinalInstance();
        System.out.println(obj.id);
    }
}
Q1694 medium code error
What compile-time error will occur in the condition of this Java for loop?
java
public class LoopError {
    public static void main(String[] args) {
        for (int i = 0; 5; i++) {
            System.out.println(i);
        }
    }
}
Q1695 easy code error
What will happen when this Java code is compiled and executed?
java
class MyClass {
    public void readFile() throws java.io.FileNotFoundException {}
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        obj.readFile();
    }
}
Q1696 easy code output
What is the output of this code?
java
public class StringTest {
    public static void main(String[] args) {
        String s = "Programming";
        System.out.println(s.substring(3));
    }
}
Q1697 medium code error
What is the compilation error generated by the following Java code?
java
public class DivisionByZeroLiteral {
    public static void main(String[] args) {
        int x = 10;
        int y = x / 0; // Compile-time error due to constant expression
        System.out.println(y);
    }
}
Q1698 easy code output
What will be the output of the following Java program?
java
public class Main {
    public static void main(String[] args) {
        int count = 5;
        int initialCount = count;
        System.out.println(initialCount);
    }
}
Q1699 easy code error
What is the compilation or runtime error in the following Java code?
java
public class Main {
    public static void main(String[] args) {
        String text = "programming";
        int index = text.indexOf('g', -1);
        System.out.println(index);
    }
}
Q1700 medium
When is a traditional `for` loop generally preferred over an enhanced `for` (for-each) loop in Java?
← Prev 8384858687 Next → Page 85 of 200 · 3994 questions