☕ Java MCQ Questions – Page 6

Questions 101–120 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q101 hard code error
What error will this Java code produce when compiled and run?
java
public class NullIntegerArray {
    public static void main(String[] args) {
        Integer[] nums = new Integer[3]; // Default values are null
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            // Attempt to unbox a null Integer to an int
            sum += nums[i]; 
        }
        System.out.println(sum);
    }
}
Q102 easy code output
What is the output of this code?
java
public class Main {
    public static void main(String[] args) {
        try {
            System.out.print("Try block executed. ");
        } finally {
            System.out.print("Finally block executed.");
        }
    }
}
Q103 easy code error
What compile-time error will occur when compiling this Java code?
java
public class MyClass {
    public static void main(String[] args) {
        int x = 5;
        int y = 10; // Not a final variable
        switch (x) {
            case 5:
                System.out.println("Five");
                break;
            case y: 
                System.out.println("Ten");
                break;
            default:
                System.out.println("Other");
        }
    }
}
Q104 hard code error
What is the result of running this Java code?
java
import java.util.TreeMap;

class MyObject {
    int value;
    MyObject(int v) { this.value = v; }
}

public class TreeMapError2 {
    public static void main(String[] args) {
        TreeMap<MyObject, String> map = new TreeMap<>();
        map.put(new MyObject(1), "First");
        System.out.println(map.size());
    }
}
Q105 hard code error
What is the compile-time error in this Java code?
java
import java.io.IOException;
public class StaticProblem {
    static {
        System.out.println("Initializing...");
        throw new IOException("Static init error");
    }
    public static void main(String[] args) {
        System.out.println("Main method started.");
    }
}
Q106 medium
In what scenario is `StringBuffer` the most appropriate choice over `String` or `StringBuilder`?
Q107 easy code output
What does this code print?
java
public class StringTest {
    public static void main(String[] args) {
        String s = "developer";
        System.out.println(s.indexOf('e'));
    }
}
Q108 medium code error
What kind of error will occur when compiling this Java code?
java
public class ArrayError {
    public static void printArray(Integer[] arr) {
        for (Integer x : arr) {
            System.out.print(x + " ");
        }
    }

    public static void main(String[] args) {
        int[] intArray = {1, 2, 3};
        printArray(intArray);
    }
}
Q109 hard
Consider a scenario where a superclass `A` and a subclass `B` both declare a public instance variable `int value`. If you have `A obj = new B();`, accessing `obj.value` will resolve to which variable?
Q110 medium code error
What compile-time error will occur in this Java code snippet?
java
public class LoopError {
    public static void main(String[] args) {
        for (int k = 0; k < 2; k++) {
            System.out.println("Inside loop: " + k);
        }
        System.out.println("Outside loop: " + k);
    }
}
Q111 hard code output
What is the output of this code?
java
import java.util.HashSet;
import java.util.Objects;

class RelaxedEqualsItem {
    int id;
    String name;
    public RelaxedEqualsItem(int id, String name) { this.id = id; this.name = name; }
    @Override public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        RelaxedEqualsItem that = (RelaxedEqualsItem) o;
        return id == that.id || Objects.equals(name, that.name); // Broad equals
    }
    @Override public int hashCode() { return Objects.hash(id, name); }
}

public class Main {
    public static void main(String[] args) {
        HashSet<RelaxedEqualsItem> set = new HashSet<>();
        set.add(new RelaxedEqualsItem(1, "A"));
        set.add(new RelaxedEqualsItem(2, "A"));
        set.add(new RelaxedEqualsItem(1, "B"));
        System.out.println(set.size());
    }
}
Q112 hard
A `Serializable` class contains a field of a custom type `MyNonSerializableClass`. How can this field be correctly serialized and deserialized within the `Serializable` parent class without `MyNonSerializableClass` itself implementing `Serializable`?
Q113 medium code output
What does this code print?
java
class AmountException extends Exception {
    public AmountException(String message) {
        super(message);
    }
}

public class Main {
    public static void withdraw(double amount) throws AmountException {
        if (amount > 500) {
            throw new AmountException("Cannot withdraw more than 500.");
        } else if (amount < 0) {
            throw new AmountException("Amount cannot be negative.");
        }
        System.out.println("Withdrawal successful: " + amount);
    }

    public static void main(String[] args) {
        try {
            withdraw(600);
            withdraw(-100);
        } catch (AmountException e) {
            System.out.println("Transaction Error: " + e.getMessage());
        }
    }
}
Q114 hard
If a `for` loop iterates and modifies a shared mutable static `List` object in a multi-threaded application *without any explicit synchronization mechanisms*, what is the most likely outcome?
Q115 easy
Which method converts all characters in a string to uppercase?
Q116 easy code error
What error will occur when this code is compiled or executed?
java
public class Main {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer();
        sb.append('X', 3);
        System.out.println(sb);
    }
}
Q117 medium code error
What error does this Java code produce when executed?
java
public class Main {
    public static void main(String[] args) {
        String numStr = "123a";
        int number = Integer.parseInt(numStr);
        System.out.println(number);
    }
}
Q118 easy code error
What error will occur when attempting to deserialize an object from 'corrupt.txt' which contains plain text?
java
import java.io.*;

public class Main {
    public static void main(String[] args) {
        String filename = "corrupt.txt";
        try (FileWriter fw = new FileWriter(filename)) {
            fw.write("This is just some plain text, not a serialized object.");
        } catch (IOException e) {
            System.err.println("Error writing text file: " + e.getMessage());
        }

        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
            Object obj = ois.readObject(); // Attempt to read object from non-object data
            System.out.println("Object deserialized.");
        } catch (IOException | ClassNotFoundException e) {
            System.err.println(e.getClass().getSimpleName() + ": " + e.getMessage());
        }
    }
}
Q119 medium code error
What compilation error will this Java code produce?
java
public class SwitchError {
    public static void main(String[] args) {
        int option = 1;
        switch (option) {
            case 1
                System.out.println("Option 1 selected");
                break;
            case 2:
                System.out.println("Option 2 selected");
                break;
            default:
                System.out.println("Invalid option");
        }
    }
}
Q120 easy code error
What will happen when this Java code is compiled and executed?
java
class MyClass {
    public void causeNPE() throws java.io.IOException {
        java.io.IOException ex = null;
        throw ex;
    }
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        try {
            obj.causeNPE();
        } catch (java.io.IOException e) {
            System.out.println("Caught IOException");
        }
    }
}
← Prev 45678 Next → Page 6 of 200 · 3994 questions