Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q4 of 15
Java - Arrays
What will be the output of the following code?
int[] numbers = new int[3];
numbers[0] = 5;
numbers[1] = 10;
System.out.println(numbers[2]);
A10
B5
C0
DArrayIndexOutOfBoundsException
Step-by-Step Solution
Solution:
  1. Step 1: Analyze array initialization and assignments

    Array 'numbers' has size 3. Index 0 is set to 5, index 1 to 10. Index 2 is not assigned explicitly.
  2. Step 2: Determine default value at index 2

    Since index 2 is not assigned, it holds default int value 0. Printing numbers[2] outputs 0.
  3. Final Answer:

    0 -> Option C
  4. Quick Check:

    Unassigned int array element = 0 [OK]
Quick Trick: Unassigned int array elements default to 0 [OK]
Common Mistakes:
  • Expecting unassigned elements to be null or error
  • Confusing index values with assigned elements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes