Quiz 5
1. What is the output of the following code:
public class Test {
public static void main(String[] args) {
int list[] = {1, 2, 3, 4, 5, 6};
for (int i = 1; i < list.length; i++)
list[i] = list[i - 1];
for (int i = 0; i < list.length; i++)
System.out.print(list[i] + " "); } }
A).C. 1 1 1 1 1 1
2. Which of the following are correct?
A). 1. String[] list = {"red", "yellow", "green"};
2. String[] list = new String[]{"red", "yellow", "green"};
3.Analyze the following code:
int[] list = new int[5]; list = new int[6];
A). The code can compile and run fine. The second line assigns a new array to list.
4. The reverse method is defined in this section. What is list1 after executing the following statements?
int[] list1 = {1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1);
A). list1 is 1 2 3 4 5 6
5. Analyze the following code:
public class Test {
public static void main(String[] args) {
boolean[][] x = new boolean[3][]; x[0] = new boolean[1];
x[1] = new boolean[2]; x[2] = new boolean[3];
System.out.println("x[2][2] is " + x[2][2]);
}
}
A). The program runs and displays x[2][2] is false.
6. Consider the array declaration and instantiation: int[ ] arr = new int[5]; Which of the following is true about arr?
A). It stores 5 elements with legal indices between 0 and 4
7. The following code accomplishes which of the tasks written below? Assume list is an int array that stores positive int values only.
int foo = 0;
for (int j =0 ; j < list.length; j++)
if (list[j] > foo) foo = list[j];
A). It stores the largest value in list (the maximum) in foo
8. The statement int[ ] list = {5, 10, 15, 20};
A). initializes list to have 4 int values
9). To initialize a String array names to store the three Strings "Huey", "Duey" and "Louie", you would do
A). String[ ] names = {"Huey", "Duey", "Louie"};
10. In a two-dimensional array, both dimensions must have the same number of elements, as in[10][10].
A). False
Comments
Post a Comment