Bird
0
0

What will be the output of the following Java code?

medium📝 Predict Output Q4 of 15
Java - Arrays

What will be the output of the following Java code?

int[] nums = {5, 10, 15};
System.out.println(nums[1]);
AArrayIndexOutOfBoundsException
B5
C15
D10
Step-by-Step Solution
Solution:
  1. Step 1: Identify the array elements and indices

    The array nums has elements: nums[0]=5, nums[1]=10, nums[2]=15.
  2. Step 2: Check the printed index

    The code prints nums[1], which is 10.
  3. Final Answer:

    10 -> Option D
  4. Quick Check:

    Array index 1 = second element [OK]
Quick Trick: Array indices start at 0; nums[1] is second element [OK]
Common Mistakes:
  • Confusing index 1 with index 0
  • Expecting output 5 or 15 incorrectly
  • Thinking index 1 is out of bounds

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes