CH 8
CH 8
Chapter 8
2) What will be the value of x[8] after the following code has been executed?
int[ ] x = {55, 33, 88, 22, 99, 11, 44, 66, 77};
int a = 10;
if(x[2] > x[5])
a = 5;
else
a = 8;
A) a = 5
B) a = 8
C) a = 10
D) This is a compilation error, you cannot compare array elements
Answer: A
1
Starting Out with Java: From Control Structures through Data Structures, 1e
Chapter 8
5) What would be the results after the following code was executed?
6) What will be the value for x[1] after the following code is executed?
8) To compare the contents of two arrays, you must compare the elements of the two arrays.
Answer: True False
11) To return an array of long values from a method, use ________ as the return type for the method.
A) long
B) long[ ]
C) long[array_size]
D) [ ]long
Answer: B
13) Given String[ ] str has been initialized, to convert all the characters in the String str[0] to upper case, use the following
statement:
A) str.uppercase();
B) str[0].upperCase();
C) str.toUpperCase();
D) str[0].toUpperCase();
Answer: D
14) When an array of objects is declared, but not initialized, the array values are set to null.
Answer: True False
3
Starting Out with Java: From Control Structures through Data Structures, 1e
Chapter 8
[Inst. 8.1] Use the below mentioned code to answer following question(s).
18) A sorting algorithm is a technique for scanning through an array and rearranging its contents in some specific order.
Answer: True False
20) Any items typed on the command-line, separated by space, after the name of the class are considered to be one or more
arguments that are to be passed into the main method.
Answer: True False
4
Starting Out with Java: From Control Structures through Data Structures, 1e
Chapter 8
int [ ] [ ] scores = { {88, 80, 79, 92}, {75, 84, 93, 80}, {98, 95, 92, 94},
{91, 84, 88, 96} };
A) 94
B) 84
C) 93
D) 95
Answer: A
22) If numbers is a two-dimensional array, which of the following would give the length of row, r?
A) numbers.length
B) numbers.length[r]
C) numbers[r].length[r]
D) numbers[r].length
Answer: D
23) Which of the following is a correct method header for receiving a two-dimensional array as an argument?
A) public static void passArray(int[1,2] intArray)
B) public static void passArray(int [ ][ ] intArray)
C) public static void passArray(int[1],[2] intArray)
D) public static void passArray(int[ ], int[ ]intArray)
Answer: B
25) Java limits the number of dimensions that an array may have to 15.
Answer: True False
5
Starting Out with Java: From Control Structures through Data Structures, 1e
Chapter 8
27) What will be the value of x[8] after the following code has been executed?
29) 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
30) What would be the results after the following code was executed?
6
Starting Out with Java: From Control Structures through Data Structures, 1e
Chapter 8
31) What will be the value for x[1] after the following code is executed?
33) If a[ ] and b[ ] are two integer arrays, then if( a == b) compares the array contents.
Answer: True False
7
Starting Out with Java: From Control Structures through Data Structures, 1e
Chapter 8
37) For the following code, what would be the value of str[2]?
38) Which of the following statements is a valid for statement, given the following?
39) Objects in an array are accessed with subscripts, just like any other data type in an array.
Answer: True False
8
Starting Out with Java: From Control Structures through Data Structures, 1e
Chapter 8
[Inst. 8.2] Use the below mentioned code to answer following question(s).
43) A sorting algorithm is used to locate a specific item in a larger collection of data.
Answer: True False
45) The String[ ] args parameter in the main method header allows the program to receive arguments from the operating
system command-line.
Answer: True False
9
Starting Out with Java: From Control Structures through Data Structures, 1e
Chapter 8
46) Given the following two-dimensional array declaration, which statement is true?
47) If numbers is a two-dimensional int array that has been initialized and total is an int that has been set to 0, which of the
following will sum all the elements in the array?
A) for (int row = 1; row < numbers.length; row++)
{
for (int col = 1; col < numbers.length; col++}
total += numbers[row][col];
}
B) for (int row = 0; row < numbers.length; row++)
{
for (int col = 0; col < numbers.length; col++}
total += numbers[row][col];
}
C) for (int row = 0; row < numbers[row].length; row++)
{
for (int col = 0; col < numbers.length; col++}
total += numbers[row][col];
}
D) for (int row = 0; row < numbers.length; row++)
{
for (int col = 0; col < numbers[row].length; col++}
total += numbers[row][col];
}
Answer: D
48) Which of the following is a correct method header for receiving a two-dimensional array as an argument?
A) public static void passArray(int[2] intArray)
B) public static void passArray(int [ ][ ] intArray)
C) public static void passArray(int[1][2] intArray)
D) public static void passArray(int[ ] intArray, int[ ]intArray)
Answer: B
49) Which of the following is a valid declaration for a ragged array, after which you would declare each row?
A) int[ ] ragged = new int[5];
B) int[ ][ ] ragged = new int[5][6];
C) int[ ][ ] ragged = new int[5][ ];
D) int[ ][ ] ragged = new int[ ][5];
Answer: C
50) Java does not limit the number of dimensions that an array may have.
Answer: True False
10
Starting Out with Java: From Control Structures through Data Structures, 1e
Chapter 8
11