Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q13 of 15
Java - Arrays
What will be the output of the following code?
int[] arr = {5, 10, 15};
for(int i = 0; i < arr.length; i++) {
  System.out.print(arr[i] + " ");
}
A5 10 15
B15 10 5
C5 10 15 0
DError: ArrayIndexOutOfBoundsException
Step-by-Step Solution
Solution:
  1. Step 1: Understand the loop iteration

    The loop runs from i=0 to i=2 (arr.length=3), printing arr[i] each time.
  2. Step 2: Check printed values

    Values printed are arr[0]=5, arr[1]=10, arr[2]=15, each followed by a space.
  3. Final Answer:

    5 10 15 -> Option A
  4. Quick Check:

    Print elements in order = 5 10 15 [OK]
Quick Trick: Prints elements in order from index 0 to length-1 [OK]
Common Mistakes:
  • Assuming reverse order output
  • Expecting extra zero at end
  • Thinking loop causes index error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes