Oopl Test On Arrays
Oopl Test On Arrays
Multiple Choice
Identify the choice that best completes the statement or answers the question.
____ 3. Which of the following creates an array of 25 components of the type int?
(i) int[] alpha = new[25];
(ii) int[] alpha = new int[25];
num[5] = 10;
num[55] = 100;
Which of the following range is valid for the index of the array nameList.
Which of the following range is valid for the index of the array salesData.
____ 11. Which of the following statements creates alpha, an array of 5 components of the type int, and initializes
each component to 10?
____ 12. What is being passed into copy in the method call above?
a. A copy of the array hits c. A reference to the array object hits
b. The value of the elements of hits d. 10
(i) beta[0]
(ii) beta[50]
(i)
for (j = 1; j < 10; j++)
System.out.print(list[j] + " ");
System.out.println();
(ii)
for (j = 0; j <= 9; j++)
System.out.print(list[j] + " ");
System.out.println();
Which of the following correctly initializes all the components of the array sales to 10.
(i)
for (j = 0; j < 49; j++)
sales[j] = 10;
(ii)
for (j = 1; j <= 50; j++)
sales[j] = 10;
Which of the following correctly finds the sum of the elements of list?
(i)
sum = 0;
for (j = 0; j < 10; j++)
sum = sum + list[j];
(ii)
sum = list[0];
for (j = 1; j < 10; j++)
sum = sum + list[j];
a. 0 1 2 3 4 c. 0, 5, 10, 15, 20
b. 0 5 10 15 20 d. None of these
____ 18. What is the output of the following Java code?
a. 0 5 10 15 20 c. 5 10 15 20 20
b. 5 10 15 20 0 d. Code contains index out-of-bound
____ 19. What is the value of alpha[2] after the following code executes?
a. 1 c. 5
b. 4 d. 6
____ 20. What is the value of alpha[4] after the following code executes?
alpha[0] = 2;
for (j = 1; j < 5; j++)
alpha[j] = alpha[j – 1] + 3;
a. 5 c. 11
b. 8 d. 14
____ 21. What is the value of alpha[3] after the following code executes?
alpha[0] = 5;
for (j = 1; j < 5; j++)
{
if (j % 2 == 0)
alpha[j] = alpha[j – 1] + 2;
else
alpha[j] = alpha[j – 1] + 3;
}
a. 10 c. 15
b. 13 d. None of these
____ 22. What is stored in alpha after the following code executes?
if (j % 2 == 1)
alpha[j - 1] = alpha[j] + 2;
}
____ 23. What is stored in alpha after the following code executes?
if (j % 2 == 1)
alpha[j - 1] = alpha[j] + j;
}
if (j > 2)
alpha[j - 1] = alpha[j] + 2;
}
a. 2 4 6 8 10 c. 4 3 2 1 0
b. 10 8 6 4 2 d. Invalid code
____ 26. What is the value of alpha[3] after the following code executes?
if (j <= 2)
alpha[j + 1] = alpha[j] + 3;
}
a. 5 c. 9
b. 8 d. 10
hit[0] = 3;
hit[1] = 5;
hit[2] = 2;
hit[3] = 6;
hit[4] = 1;
System.out.println(hit[1 + 3]);
____ 29. Which indices are in bounds for the array array1, given the declaration above?
a. 0, 1, 2, 3 c. 1, 3, 5, 7
b. 1, 2, 3, 4 d. 0, 1, 3, 5
x[0] = 34;
x[1] = 88;
Which of the following for loops set the index of gamma out of bounds?
(i)
for (j = 0; j <= 49; j++)
System.out.print(gamma[j] + " ");
(ii)
for (j = 1; j < 50; j++)
System.out.print(gamma[j] + " ");
(iii)
for (j = 0; j <= 50; j++)
System.out.print(gamma[j] + " ");
return count;
}
Which of the following statements best describe the behavior of this method?
a. This method returns the number of values stored in list.
b. This method returns the sum of all the values of list.
c. This method returns the number of times item is stored in list.
d. None of these
____ 33. Given the following method heading
Which of the following input statements correctly input values into alpha? (Assume that console is a
Scanner object initialized to the standard input device.)
(i)
alpha = console.nextInt();
alpha = console.nextInt();
alpha = console.nextInt();
(ii)
alpha[0] = console.nextInt();
alpha[1] = console.nextInt();
alpha[2] = console.nextInt();
Which of the following input statements correctly input values into beta? (Assume that console is a
Scanner object initialized to the standard input device.)
(i)
beta[0] = console.nextInt();
beta[1] = console.nextInt();
beta[2] = console.nextInt();
(ii)
for (j = 0; j < 3; j++)
beta[j] = console.nextInt();
____ 36. Suppose alpha is an array of 50 components. Which of the following about alpha is true?
(i) The base address of alpha is the address of alpha[0].
(ii) The base address of alpha is the address of alpha[1].
bs = as;
____ 37. How many objects are present after the code fragment above is executed?
a. 1 c. 7
b. 2 d. 14
____ 38. Assume that two arrays are parallel. Assume the first array contains a person’s id number and the second
contains his or her age. In which index of the second array would you find the age of the person in the third
index of the first array?
a. 1
b. 2
c. 3
d. It cannot be determined form the information given.
____ 49. Which of the following statements creates alpha, a two-dimensional array of 10 rows and 5 columns, wherein
each component is of the type int?
(i)
int[][] alpha = new int[10][5];
(ii)
int[][] alpha;
alpha = new int[10][];
for (int i = 0; i < 10; i++)
alpha[i] = new int[5];
a. Only (i) c. Both (i) and (ii)
b. Only (ii) d. None of these
____ 50. Suppose that sales is a two-dimensional array of 10 rows and 7 columns wherein each component is of the
type int , and sum and j are int variables. Which of the following correctly finds the sum of the elements
of the fifth row of sales?
a. sum = 0;
for(j = 0; j < 7; j++)
sum = sum + sales[5][j];
b. sum = 0;
for(j = 0; j < 7; j++)
sum = sum + sales[4][j];
c. sum = 0;
for(j = 0; j < 10; j++)
sum = sum + sales[5][j];
d. sum = 0;
for(j = 0; j < 10; j++)
sum = sum + sales[4][j];