Bird
0
0

What will be the output of this Java code snippet?

medium📝 Predict Output Q5 of 15
Java - Arrays

What will be the output of this Java code snippet?

int[] arr = new int[3];
arr[0] = 7;
arr[2] = 9;
System.out.println(arr[1]);
A0
B7
C9
DNullPointerException
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 printed index

    arr[0] = 7, arr[2] = 9, but arr[1] is not assigned, so it remains 0.
  3. Final Answer:

    0 -> Option A
  4. Quick Check:

    Unassigned int array elements default to 0 [OK]
Quick Trick: Unassigned int array elements are zero by default [OK]
Common Mistakes:
  • Assuming unassigned elements are null or garbage
  • Confusing assigned elements with unassigned
  • Expecting runtime error for unassigned index

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes