1



Chapter 8

Arrays and the ArrayList Class

( Test 2

1. The following statements

final int ARRAY_SIZE ’ 10;

long[] array1 ’ new long[ARRAY_SIZE];

(a) Declares array1 to be a reference to an array of long values

(b) Creates an instance of an array of 10 long values

(c) Will allow valid subscripts in the range of 0–9

(d) All of the above

Answer: D, Introduction To Arrays

2. What will be the value of x[8] after the following code has been executed?

final int SUB ’ 12;

int[] x ’ new int[SUB];

int y ’ 20;

for(int i ’ 0; i < SUB; i++)

{

x[i] ’ y;

y +’ 5;

}

(a) 50

(b) 55

(c) 60

(d) 65

Answer: C, Introduction To Arrays

3. What will be the result of executing the following code?

int[] x ’ {0, 1, 2, 3, 4, 5};

(a) An array of 6 values ranging from 0–5 and referenced by the variable x will be created

(b) A compilation error will occur

(c) The program will crash when it is executed

(d) The value of x[0] will be 15 and x[1] – x[5] will be set to 0

Answer: A, Introduction To Arrays

4. If final int sub ’ 15 and int[] x ’ new int[sub], what would be the range of subscript values that could be used with x[]?

(a) 1–15

(b) 1–14

(c) 0–14

(d) 0–15

Answer: C, Introduction To Arrays

5. What would be the results after the following code was executed?

int[] x ’ {23, 55, 83, 19};

int[] y ’ {36, 78, 12, 24};

x ’ y;

y ’ x;

(a) y[] ’ {23, 55, 83, 19} and x[] ’ {36, 78, 12, 24}

(b) x[] ’ {36, 78, 12, 24} and y[] ’ {36, 78, 12, 24}

(c) x[] ’ {23, 55, 83, 19} and y[] ’ {23, 55, 83, 19}

(d) This is a compilation error

Answer: B, Processing Array Contents

6. What will be the value for x[1] after the following code is executed?

int[] x ’ {22, 33, 44};

arrayProcess(x[1]);



public static void arrayProcess(int a)

{

a ’ a + 5;

}

(a) 27

(b) 33

(c) 38

(d) 49

Answer: B, Passing Arrays as Arguments

7. When an individual element of an array is passed to a method

(a) A reference to the array is passed

(b) It is passed just as an object

(c) The method does not have direct access to the original array

(d) All of the above

Answer: C, Passing Arrays as Arguments

8. True/False If a[] and b[] are two integer arrays, then if( a ’’ b) compares the array contents.

Answer: False, Some Useful Array Algorithms and Operations

9. What would be the results of the following code?

int[] array1 ’ new int[25];

… // Code that will put values in arrat1

int value ’ 0;

for (int a ’ 0; a ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download