You are on page 1of 7

Binalatongan Community College

JAVA Programming

TEST 1: True/False
Indicate whether the statement is true or false.

____ 1. Arrays have a fixed number of elements.


____ 2. Arrays are made up only of integers.
____ 3. The statement

int[] list = new int[25];


creates list to be an array of 26 components because array index starts
at 0.

____ 4. Given the declaration

int[] list = new int[20];

the statement

list[12] = list[5] + list[7];

updates the content of the twelfth component of the array list.

____ 5. The instance variable length contains the value of the last index in the
array.
____ 6. Arrays can be initialized when they are created.
____ 7. Loops can be used to step through the elements of a one-dimensional
array.
____ 8. Given the declaration

int[] list = new int[50];

the statement

System.out.println(list);

outputs all 50 components of the array list, one component per line.

____ 9. If an array index is less than zero, an InvalidIndexException exception


is thrown.
____ 10. An ArrayOutOfBoundsException is thrown if an index is greater than or
equal to the size of the array minus 1.
____ 11. In a method call statement, when passing an array as an actual
parameter, you use only its name.
____ 12. The base address of an array is the memory location of the first array
component.
____ 13. When you pass an array as a parameter, the base address of the actual
array is passed to the formal parameter.
____ 14. If the value in each index of an array is related to the value in the
corresponding index of a different array, the two arrays are parallel.
____ 15. A method can have both a variable length formal parameter and other
formal parameters.
____ 16. If a method has both a variable length formal parameter and other types
of formal parameters, then the variable length formal parameter must be
the last formal parameter of the formal parameter list.
____ 17. Two-dimensional arrays generally contain data in list form.
____ 18. The following statement creates alpha to be a two-dimensional array of
25 rows and 10 columns.

int[][] alpha = new int[25][10];

____ 19. Suppose that you have the following declaration.

int[] alpha = new int[100];


int[][] beta = new int[25][4];

In this declaration, the array alpha has more components than the array
beta.
____ 20. The statement dataType[][][] arrayName; would declare a two-dimensional
array.

TEST 2: Multiple Choice


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

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

int[] num = new int[100];

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


num[i] = i;

num[5] = 10;
num[55] = 100;
____ 22. What is the data type of the array above?
a. int c. list
b. char d. num
____ 23. How many components are in the array above?
a. 0 c. 99
b. 50 d. 100
____ 24. What is the value of num.length in the array above?
a. 0 c. 100
b. 99 d. 101
____ 25. Which of the following is the array subscripting operator in Java?
a. . c. new
b. {} d. []
____ 26. 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);
____ 27. 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 d. 10
hits
____ 28. 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
____ 29. 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
____ 30. 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
____ 31. 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
____ 32. 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
____ 33. 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}
____ 34. 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}
____ 35. 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

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]);
____ 36. 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]);

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


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

int[] x = new int[10];

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

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


____ 38. 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.
____ 39. 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
____ 40. 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
____ 41. Given the method heading

public static void strange(int a, int b)

and the declaration

int[] alpha = new int[10];


int[] beta = new int[10];

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

a. strange(alpha[0], alpha[1]);
b. strange(alpha, beta);
c. strange(alpha[0], beta);
d. strange(alpha, beta[0]);
____ 42. 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
____ 43. 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
char[][] array1 = new char[15][10];
____ 44. How many columns are in the array above?
a. 0 c. 10
b. 2 d. 15
____ 45. What is the value of array1[3].length?
a. 0 c. 10
b. 2 d. 15
____ 46. 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}}
____ 47. What is in vals[2][1]?
a. 1.3 c. 3.5
b. 3.3 d. 5.3
____ 48. How many columns are in the array above?
a. 0 c. 3
b. 2 d. 4
____ 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