Given the code fragment:
What is the result?
A.
1:2:3:4:5:
B.
1:2:3:
C.
Compilation fails.
D.
An ArrayoutofBoundsException is thrown at runtime.
Given the code fragment:
What is the result?
A.
1:2:3:4:5:
B.
1:2:3:
C.
Compilation fails.
D.
An ArrayoutofBoundsException is thrown at runtime.
public class MyClass{
public static void main(String[] args) {
int nums1 [] = new int [3];
int nums2 [] = {1, 2, 3, 4, 5};
nums1 = nums2;
for(int x : nums1){
System.out.print(x + ” : “);
}
}
}
output: 1 : 2 : 3 : 4 : 5 :
Indeed, the anwser is A.
Reference variable nums1 is (in the loop) pointing to array object with elements (1, 2, 3,4, 5).
A