QuestionQ81

Creating and Using Arrays

Given this code fragment:

Question Image

What is the outcome?

  • A 1:2:3:4:5:
  • B 1:2:3:
  • C Compilation fails.
  • D An ArrayOutOfBoundsException is thrown at runtime.
Explanation

Assigning one array variable to another copies the array reference, not the array elements. After nums2 = nums1, nums2 refers to the three-element array {1, 2, 3}, so the enhanced for loop prints 1:2:3:.

Community Discussion

No comments yet. Be the first to start the discussion!