Bird
0
0

What will be the output of this Java code?

medium📝 Predict Output Q5 of 15
Java - Arrays
What will be the output of this Java code?
int[] arr = new int[3];
arr[0] = 10;
arr[1] = 20;
System.out.println(arr[2]);
A0
B10
C20
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand default values in int arrays

    When an int array is created, all elements are initialized to 0 by default.
  2. Step 2: Check assigned values and output

    arr[0] and arr[1] are set, but arr[2] is not assigned, so it remains 0.
  3. Final Answer:

    0 -> Option A
  4. Quick Check:

    Unassigned int array element = 0 = C [OK]
Quick Trick: Unassigned int array elements default to 0 [OK]
Common Mistakes:
  • Expecting error for unassigned index
  • Thinking unassigned elements have garbage values
  • Confusing assigned values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes