0% found this document useful (0 votes)
80 views12 pages

Oopl Test On Arrays

The document contains a multiple choice test on Java arrays. It asks 26 questions that cover topics such as: the valid index ranges for arrays, initializing and accessing array elements, iterating through arrays using for loops, and modifying array elements within loops. The questions test understanding of basic array concepts in Java like declaration, initialization, accessing elements, and iterating.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views12 pages

Oopl Test On Arrays

The document contains a multiple choice test on Java arrays. It asks 26 questions that cover topics such as: the valid index ranges for arrays, initializing and accessing array elements, iterating through arrays using for loops, and modifying array elements within loops. The questions test understanding of basic array concepts in Java like declaration, initialization, accessing elements, and iterating.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

OOPL TEST

Multiple Choice
Identify the choice that best completes the statement or answers the question.

____ 1. Which of the following about Java arrays is true?


(i) All components must be of the same type.
(ii) The array index and array element must be of the same type.
a. Only (i) c. Both (i) and (ii)
b. Only (ii) d. None of these
____ 2. Which of the following about Java arrays is true?
(i) Array components must be of the type double.
(ii) The array index must evaluate to an integer.
a. Only (i) c. Both (i) and (ii)
b. Only (ii) d. None of these

____ 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];

a. Only (i) c. Both (i) and (ii)


b. Only (ii) d. None of these

____ 4. Which of the following declares an array of int named hits?


a. int hits; c. new int hits[];
b. int[] hits; d. int hits = int[];

int[] num = new int[100];

for (int i = 0; i < 50; i++)


num[i] = i;

num[5] = 10;
num[55] = 100;

____ 5. What is the data type of the array above?


a. int c. list
b. char d. num
____ 6. What is the index number of the last component in the array above?
a. 0 c. 100
b. 99 d. 101

____ 7. What is the value of num.length in the array above?


a. 0 c. 100
b. 99 d. 101
____ 8. What is the value at index 10 of the array above?
a. 0 c. 10
b. 5 d. 50
____ 9. Suppose you have the following declaration.

char[] nameList = new char[100];

Which of the following range is valid for the index of the array nameList.

(i) 1 through 100


(ii) 0 through 100
a. Only (i) c. Both are invalid
b. Only (ii) d. None of these
____ 10. Suppose you have the following declaration.

double[] salesData = new double[1000];

Which of the following range is valid for the index of the array salesData.

(i) 0 through 999


(ii) 1 through 1000
a. Only (i) c. Both are invalid
b. Only (ii) d. None of these

____ 11. Which of the following statements creates alpha, an array of 5 components of the type int, and initializes
each component to 10?

(i) int[] alpha = {10, 10, 10, 10, 10};


(ii) int[5] alpha = {10, 10, 10, 10, 10}

a. Only (i) c. Both (i) and (ii)


b. Only (ii) d. None of these

int[] hits = {10, 15, 20, 25, 30};


copy(hits);

____ 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

____ 13. Suppose you have the following declaration.

int[] beta = new int[50];

Which of the following is a valid element of beta.

(i) beta[0]
(ii) beta[50]

a. Only (i) c. Both (i) and (ii)


b. Only (ii) d. None of these
____ 14. Consider the following declaration.

int[] list = new int[10];


int j;

Which of the following correctly outputs all the elements of list.

(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();

a. Only (i) c. Both (i) and (ii)


b. Only (ii) d. None of these
____ 15. Consider the following declaration.

double[] sales = new double[50];


int j;

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;

a. Only (i) c. Both (i) and (ii)


b. Only (ii) d. None of these
____ 16. Consider the following declaration.

int[] list = new int[10];


int j;
int sum;

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. Only (i) c. Both (i) and (ii)


b. Only (ii) d. None of these
____ 17. What is the output of the following Java code?

int[] list = {0, 5, 10, 15, 20};


int j;

for (j = 0; j < 5; j++)


System.out.print(list[j] + " ");
System.out.println();

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?

int[] list = {0, 5, 10, 15, 20};


int j;

for (j = 1; j <= 5; j++)


System.out.print(list[j] + " ");
System.out.println();

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?

int[] alpha = new int[5];


int j;

for (j = 0; j < 5; j++)


alpha[j] = 2 * j + 1;

a. 1 c. 5
b. 4 d. 6

____ 20. What is the value of alpha[4] after the following code executes?

int[] alpha = new int[5];


int j;

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?

int[] alpha = new int[5];


int j;

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?

int[] alpha = new int[5];


int j;

for (j = 0; j < 5; j++)


{
alpha[j] = j + 5;

if (j % 2 == 1)
alpha[j - 1] = alpha[j] + 2;
}

a. alpha = {5, 6, 7, 8, 9} c. alpha = {8, 6, 7, 8, 9}


b. alpha = {5, 6, 10, 8, 9} d. alpha = {8, 6, 10, 8, 9}

____ 23. What is stored in alpha after the following code executes?

int[] alpha = new int[5];


int j;

for (j = 0; j < 5; j++)


{
alpha[j] = 2 * j;

if (j % 2 == 1)
alpha[j - 1] = alpha[j] + j;
}

a. alpha = {0, 2, 4, 6, 8} c. alpha = {0, 3, 4, 7, 8}


b. alpha = {3, 2, 9, 6, 8} d. alpha = {0, 2, 9, 6, 8}
____ 24. What is stored in alpha after the following code executes?

int[] alpha = new int[5];


int j;

for (j = 0; j < 5; j++)


{
alpha[j] = j + 1;

if (j > 2)
alpha[j - 1] = alpha[j] + 2;
}

a. alpha = {1, 5, 6, 7, 5} c. alpha = {4, 5, 6, 7, 9}


b. alpha = {1, 2, 3, 4, 5} d. None of these
____ 25. What is the output of the following Java code?

int[] alpha = {2, 4, 6, 8, 10};


int j;

for (j = 4; j >= 0; j--)


System.out.print(alpha[j] + " ");
System.out.println();

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?

int[] alpha = new int[5];


int j;

for (j = 4; j >= 0; j--)


{
alpha[j] = j + 5;

if (j <= 2)
alpha[j + 1] = alpha[j] + 3;
}

a. 5 c. 9
b. 8 d. 10

int[] hit = new hit[5];

hit[0] = 3;
hit[1] = 5;
hit[2] = 2;
hit[3] = 6;
hit[4] = 1;
System.out.println(hit[1 + 3]);

____ 27. What is the output of the code fragment above?


a. 1 c. 5
b. 3 d. 6

int[] array1 = {1, 3, 5, 7}

for (int i = 0; i <array.length; i++)


if (array1[i] > 5)
System.out.println(i + " " + array1[i]);

____ 28. What is the output of the code fragment above?


a. 0 3 c. 7 3
b. 3 7 d. 5 1

____ 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

int[] x = new int[10];

x[0] = 34;
x[1] = 88;

System.out.println(x[0] + " " + x[1] + " " + x[10]);

____ 30. What is the output of the code fragment above?


a. 34 88 0
b. 34 88 88
c. 0 34 88
d. This program throws an exception.
____ 31. Given the declaration

int[] gamma = new int[50];


int j;

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] + " ");

a. Only (i) c. Only (iii)


b. Only (ii) d. All (i), (ii), and (iii)
____ 32. Consider the following method definition.

public static int strange(int[] list, int listSize, int item)


{
int count;

for (int j = 0; j < listSize; j++)


if (list[j] == item)
count++;

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

public static void mystery(int list[], int size)

and the declaration

int[] alpha = new int[50];

Which of the following is a valid call to the method mystery?

a. mystery(alpha[50]); c. mystery(alpha, 50);


b. mystery(alpha[], 50); d. None of these
____ 34. Consider the following declaration.

int[] alpha = new int[3];

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();

a. Only (i) c. Both (i) and (ii)


b. Only (ii) d. None of these
____ 35. Consider the following declaration.

int[] beta = new int[3];


int j;

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();

a. Only (i) c. Both (i) and (ii)


b. Only (ii) d. None of these

____ 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].

a. Only (i) c. Both (i) and (ii)


b. Only (ii) d. None of these

double[] as = new double[7];


double[] bs;

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.

char[][] array1 = new char[15][10];

____ 39. How many dimensions are in the array above?


a. 0 c. 2
b. 1 d. 3
____ 40. How many rows are in the array above?
a. 0 c. 10
b. 2 d. 15
____ 41. How many columns are in the array above?
a. 0 c. 10
b. 2 d. 15

____ 42. What is the value of array1[3].length?


a. 0 c. 10
b. 2 d. 15

____ 43. What is the value of array1.length?


a. 0 c. 10
b. 2 d. 15

double[][] vals = {{1.1, 1.3, 1.5},


{3.1, 3.3, 3.5},
{5.1, 5.3, 5.5},
{7.1, 7.3, 7.5}}

____ 44. What is the value of vals.length in the array above?


a. 0 c. 4
b. 3 d. 16

____ 45. What is in vals[2][1]?


a. 1.3 c. 3.5
b. 3.3 d. 5.3
____ 46. How many rows are in the array above?
a. 0 c. 3
b. 2 d. 4
____ 47. How many columns are in the array above?
a. 0 c. 3
b. 2 d. 4

____ 48. What is the value of vals[4][1] in the array above?


a. 1.1 c. 7.3
b. 7.1 d. There is no such value.

____ 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];

You might also like