Given the code fragment:
What is the result?
A. 1 3 5 7
1 3
B. 1 2
1 3
C. 1 3
1 3 0 0
D. 1 3 followed by an ArrayIndexOutOfBoundsException
E. Compilation fails.
Given the code fragment:
What is the result?
A. 1 3 5 7
1 3
B. 1 2
1 3
C. 1 3
1 3 0 0
D. 1 3 followed by an ArrayIndexOutOfBoundsException
E. Compilation fails.
b
not listed in the answers:
# javac Test.java
# java Test
1 3 1 3
Correct answer is:
1 3
1 3
This is because the outer for loops will iterate over the contents of the array “arr” which consists of two “sub” arrays, so to say. Since the arr.length is 2, the outer for loop will iterate twice. In the first iteration, the value of “a” is going to be the first sub array (i.e. {1, 3, 5, 7}), and in the second iteration the value of “a” is going to be {1, 3}. Now comes the second for loop which iterates over the elements of each array. Since the for loop condition is i<arr.length, it means that "i" will start in 0 and stops at 1. Meaning that it will only print a[0] and a[1] for each of the arrays. Therefore, the result is going to be 1 3 followed by an empty line (from the println at the end of each inner for loop) then 13.
Correct answer is
13
13
13 13
not println it’s a print function
code in the answer is not the same at the question