COP2210 Quiz 6 Loops
What will be the output of the following code? int[] arr = new int[9]; System.out.println(arr[0]); A) error because array is not initialized B) 0 C) some garbage value D) error because index must be greater than 0
0
What is the output of this program? class array_output { public static void main(String args[]) { int array_variable [] = new int[10]; for (int i = 0; i < 10; ++i) { array_variable[i] = i; System.out.print(array_variable[i] + " "); i++; } } } A) 0 1 2 3 4 5 6 7 8 9 B) 1 3 5 7 9 C) 0 2 4 6 8 D) 1 2 3 4 5 6 7 8 9 10
0 2 4 6 8
If final int SIZE = 15 and int[] x = new int[SIZE], what would be the range of subscript values that could be used with x[]? A) 0-14 B) 0-15 C) 1-15 D) 1-14
0-14
Given the following for loop: int x = 2; int y = 0; for ( ; y < 10; ++y) { if (y % x == 0) continue; else if (y == 8) break; else System.out.print(y + " "); } What is the output of this code? A) 1 3 5 7 B) 2 4 6 8 C) 1 3 5 7 9 D) 1 2 3 4 5 6 7 8 9
1 3 5 7 9
Given the following for loop: for (int ii = 1; ii < 10; ii++) { System.out.println(ii); } How many lines will this loop print? A) 11 B) 9 C) 10 D) 0 E) 8
9
When an individual element of a primitive array is passed to a method A) All of these B) A reference to the array is passed C) The element is passed just as an object would be passed D) The method does not have access to the original array
The method does not have access to the original array
Which of the following for loops is valid, given the following declaration? String[] names = {"abc", "def", "ghi", "jkl"}; A) for (int i = 0; i < names.length(); i++ System.out.println(names[i].length()); B) for (int i = 0; i < names.length; i++ System.out.println(names[i].length()); C) for (int i = 0; i < names.length; i++ System.out.println(names[i].length); D) for (int i = 0; i < names.length(); i++ System.out.println(names[i].length);
for (int i = 0; i < names.length; i++ System.out.println(names[i].length());
Given the following for loop: var i = 0; while (i < 3) { println("hi"); i++; } What does the code output? A) hi B) hi hi C) hi hi hi D) hi hi hi
hi hi hi
Which of these is an incorrect array declaration? A) int arr[] = int [5] new; B) int arr[] = new int[5]; C) int arr[]; arr = new int[5]; D) int [] arr = new int[5];
int arr[] = int [5] new;
A partially-filled array is normally used A) with an accompanying integer value that holds the number of items stored in the array B) to display array elements C) with an accompanying parallel array D) when you know how many elements will be in the array
with an accompanying integer value that holds the number of items stored in the array
What would be the results after the following code was executed? char[] x = {'a', 'b', 'a'}; char[] y = {'x', 'y', 'w'}; char[] temp; temp = x; y = x; x = temp; A) x[] = { 'x', 'y', 'w' } and y[] = { 'x', 'y', 'w' } B) x[] = { 'a', 'b', 'a' } and y[] = { 'a', 'b', 'a' } C) This will produce a compilation error. D) x[] = { 'x', 'y', 'w' } and y[] = { 'a', 'b', 'a' }
x[] = { 'a', 'b', 'a' } and y[] = { 'a', 'b', 'a' }
What would be the results after the following code was executed? int[] x = {23, 55, 83, 19}; int[] y = {36, 78, 12, 24}; for(int a = 0; a < x.length; a++) { x[a] = y[a]; y[a] = x[a]; } A) x[] = {36, 78, 12, 24} and y[] = { 23, 55, 83, 19 } B) x[] = {23, 55, 83, 19} and y[] = { 23, 55, 83, 19 } C) It will not execute because there is a compilation error D) x[] = {36, 78, 12, 24} and y[] = { 36, 78, 12, 24 }
x[] = {36, 78, 12, 24} and y[] = { 36, 78, 12, 24 }
What would be the results after the following code was executed? int[] x = {23, 55, 83, 19}; int [] y = {42, 65, 19, 2}; x = y; y = x; y[2] = 4; A) x[] = {42, 65, 19, 2} and y[] = {42, 65, 4, 2} B) You cannot assign one array to another like this. C) x[] = {42, 65, 4, 2} and y[] = {42, 65, 4, 2} D) x[] = {42, 65, 19, 2} and y[] = {23, 55, 4, 19} E) x[] = {42, 65, 19, 2} and y[] = {23, 4, 19, 19}
x[] = {42, 65, 4, 2} and y[] = {42, 65, 4, 2}