Array - Study Mode
[#41] Determine output: public class Test{
public static void main(String[] args){
int[] x = {1, 2, 3, 4}
int[] y = x
x = new int[2]
for(int i = 0
i < x.length
i++)
System.out.print(y[i] + " ")
}
}
Correct Answer
(C) 1 2
[#42] Analyze the following code and choose the correct answer. int[] arr = new int[5]
arr = new int[6]
Correct Answer
(C) The code can compile and run fine. The second line assigns a new array to arr .
[#43] What will be the output? public class Test{
public static void main(String[] args){
int[] a = new int[4]
a[1] = 1
a = new int[2]
System.out.println("a[1] is " + a[1])
}
}
Correct Answer
(C) a [ 1 ] is 0
Explanation
Solution: After executing the statement a = new int [ 2 ], a refers to int [ 2 ]. The default value for a [ 0 ] and a [ 1 ] is 0.
[#44] How do you find the index of an element in an ArrayList named list in Java?
Correct Answer
(B) list.indexOf(element)
[#45] How do you declare a multi-dimensional array in Java?
Correct Answer
(D) All of the above