☕ Java MCQ Questions – Page 7

Questions 121–140 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q121 hard code error
What is the error in the execution of this Java code snippet?
java
public class BuilderError {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Data");
        sb.delete(-1, 2);
        System.out.println(sb);
    }
}
Q122 easy code output
What is the output of this Java code?
java
public class WhileLoopDemo {
    public static void main(String[] args) {
        int count = 0;
        while (count < 2) {
            System.out.print("Hello ");
            count++;
        }
    }
}
Q123 easy
What is the root class of all classes in Java?
Q124 hard code output
What is the output of this Java code involving an inner class and field shadowing?
java
class Outer {
    private String message = "Hello from Outer";
    String shadowVar = "Outer Shadow";
    class Inner {
        String shadowVar = "Inner Shadow";
        public void printMessages() {
            System.out.println("Inner's: " + this.shadowVar);
            System.out.println("Outer's msg: " + Outer.this.message);
            System.out.println("Outer's var: " + Outer.this.shadowVar);
        }
    }
}
public class Main {
    public static void main(String[] args) {
        new Outer().new Inner().printMessages();
    }
}
Q125 easy
What happens if you try to put a new value into a HashMap using an existing key?
Q126 hard
Which statement about the `instanceof` operator in relation to type casting is most accurate?
Q127 medium code error
What error will occur when compiling this Java code?
java
public class ConditionCheck {
    public static void main(String[] args) {
        int score = 75;
        System.out.println("Processing score");
        else {
            System.out.println("No score processed yet.");
        }
    }
}
Q128 easy
Which characteristic best describes the type of elements a Java single-dimensional array can store?
Q129 hard code output
What does this code print?
java
public class JaggedArrayNulls {
    public static void main(String[] args) {
        int[][] matrix = new int[3][];
        matrix[0] = new int[]{1, 2};
        matrix[2] = new int[]{3, 4, 5};
        try {
            System.out.println(matrix[1][0]);
        } catch (NullPointerException e) {
            System.out.println("NPE caught!");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("AIOOBE caught!");
        }
    }
}
Q130 hard code error
What is the compilation error, if any, for the provided Java code snippet?
java
import java.io.Serializable;

class Overloader {
    public void store(Object o) {}
    public void store(Serializable s) {}

    public static void main(String[] args) {
        Overloader o = new Overloader();
        o.store(null); // Line X
    }
}
Q131 easy
For method overloading, what must differ between methods with the same name?
Q132 medium code error
What error will occur when running the following Java code?
java
public class DoubleStartThread {
    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            System.out.println("Thread running");
        });
        t.start();
        t.start(); // Attempt to start the thread again
    }
}
Q133 hard
If a superclass declares a `static` method and a subclass declares its own `static` method with the exact same signature, what is the relationship between these two methods in Java?
Q134 easy
In an `if-else` statement, if the `if` condition evaluates to `false`, which block of code will be executed?
Q135 hard code output
What is the output of this code?
java
import java.io.IOException;

public class Test {
    public static void main(String[] args) {
        try {
            int result = callMethod();
            System.out.print("Result: " + result);
        } catch (Exception e) {
            System.out.print("Caught: " + e.getMessage());
            if (e.getSuppressed().length > 0) {
                System.out.print(" Suppressed: " + e.getSuppressed()[0].getMessage());
            }
        }
    }

    public static int callMethod() throws Exception {
        try {
            System.out.print("In callMethod try ");
            throw new RuntimeException("Try Exception");
        } finally {
            System.out.print("In callMethod finally ");
            throw new IOException("Finally Exception");
        }
    }
}
Q136 hard code output
What is the output of this code?
java
public class DataTypeChallenge {
    public static void main(String[] args) {
        Integer i = null;
        Integer j = 10;
        try {
            System.out.println(i == j);
        } catch (Exception e) {
            System.out.println("NPE Caught");
        }
    }
}
Q137 medium
When an unlabeled `break` statement is used within a `do-while` loop, what happens?
Q138 medium code error
What is the compilation error in the following Java code?
java
public class StaticAccess {
    int instanceVar = 10;

    public static void main(String[] args) {
        System.out.println(instanceVar);
    }
}
Q139 hard code error
What is the result of executing this Java code snippet?
java
class MyClass {}

public class InstanceofConfusion {
    public static void main(String[] args) {
        Object obj = "Hello";
        if (obj instanceof String) {
            MyClass mc = (MyClass) obj; 
            System.out.println("Cast successful");
        } else {
            System.out.println("Not a String");
        }
    }
}
Q140 medium
In the `read(char[] cbuf, int off, int len)` method of `FileReader`, what does the `off` parameter specify?
← Prev 56789 Next → Page 7 of 200 · 3994 questions