Given the code fragment:
What is the result?
Given the code fragment:
What is the result?
In the given code fragment, two integer arrays, nums1 and nums2, are initialized with different values. After the statement nums2 = nums1, the reference nums2 is updated to point to the same array as nums1. Hence, the for loop will iterate over the elements of the array referenced by nums2, which is now the same as nums1. Therefore, the output sequence will be the elements of nums1: 1, 2, 3. The correct result is 1:2:3:
Assignment statement `nums2 = nums1`, where there's a space and both arrays will now point to the same memory location.
Tested code provided by rachuk Even though both arrays have different sizes, there's no problem in reassigning their variables. Variables are still nothing else than references to objects.
Answer C public static void main(String[] args) { int nums1[] = {1, 2 ,3}; int nums2[] = {1, 2, 3 ,4 ,5}; nums 2 = nums 1; // not defined. The variable has a space between nums and 2 for (int i : nums2) { System.out.print(i + ":"); } }
There's space between the numbers 1 and 2 and the nums
Compilation fails: no variables "num 1" and "num 2" defined
That maybe correct.
Correct answer is B, code for testing: public static void main(String[] args) { int nums1[] = {1, 2 ,3}; int nums2[] = {1, 2, 3 ,4 ,5}; nums2 = nums1; for (int i : nums2) { System.out.print(i + " : "); } }
Compilation fails. It should be nums2 instead of nums 2.
B is corrent, in line 3 "nums2" becomes a reference of "nums1".
An online complier gave me B.
Answer is B , because array is a object
Answer : B and tested locally
or C if there is a typo error
Answer: B
correct one is B
B is correct
Ran this program I was surprised with the result but A is correct