Bird
0
0

What will be the output of the following Java code?

medium📝 Predict Output Q13 of 15
Java - Arrays
What will be the output of the following Java code?
int[] nums = {1, 2, 3, 4};
System.out.println(nums[2]);
A1
B2
C3
D4
Step-by-Step Solution
Solution:
  1. Step 1: Understand array indexing in Java

    Arrays start at index 0, so nums[0]=1, nums[1]=2, nums[2]=3, nums[3]=4.
  2. Step 2: Identify the printed element

    The code prints nums[2], which is the third element, value 3.
  3. Final Answer:

    3 -> Option C
  4. Quick Check:

    Index 2 value = 3 [OK]
Quick Trick: Remember array index starts at 0 in Java [OK]
Common Mistakes:
  • Confusing index 2 with the second element
  • Off-by-one errors in indexing
  • Misreading array initialization

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes