Bird
0
0

What will be the output of this Java program?

medium📝 Predict Output Q5 of 15
Java - Exception Handling
What will be the output of this Java program?
public class ArrayTest {
  public static void main(String[] args) {
    try {
      int[] numbers = new int[3];
      numbers[4] = 50;
    } catch (ArrayIndexOutOfBoundsException e) {
      System.out.println("Array index is out of bounds");
    }
  }
}
A0
B50
CArray index is out of bounds
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Understand array indexing

    The array 'numbers' has size 3, valid indices are 0 to 2.
  2. Step 2: Accessing invalid index

    Accessing index 4 causes ArrayIndexOutOfBoundsException.
  3. Step 3: Catch block

    The exception is caught and prints "Array index is out of bounds".
  4. Final Answer:

    Array index is out of bounds -> Option C
  5. Quick Check:

    Invalid array index triggers exception catch [OK]
Quick Trick: Invalid array index triggers ArrayIndexOutOfBoundsException [OK]
Common Mistakes:
  • Expecting the program to print 50
  • Assuming index 4 is valid for size 3 array
  • Thinking this causes a compile-time error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes