Escolar Documentos
Profissional Documentos
Cultura Documentos
2 Points / Question
Which one of the following options is a valid line of code for displaying the twenty-eighth
element of somearray?
a) System.out.println(somearray[28]);
b) System.out.println(somearray(28));
c) System.out.println(somearray(27));
d) System.out.println(somearray[27]);
Answer: d
2) Identify the correct statement for defining an integer array named numarray of ten elements.
Answer: b
Title: Which statement defines integer array numarray with ten elements?
Difficulty: Easy
3) Which one of the following statements is a valid initialization of an array named somearray
of ten elements?
a) int[] somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
b) int somearray[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
c) int[10] somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
d) int somearray[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Answer: a
a) 1050
b) 2030
c) 3040
d) 4050
Answer: c
Title: What are values of the elements at the given array indexes?
Difficulty: Medium
Section Reference 1: 7.1 Arrays
Answer: c
6) Which one of the following statements is correct about the given code snippet?
{
somearray[i] = i + 1;
}
Answer: b
Title: Which statement is true about this for loop that initializes an array?
Difficulty: Hard
Section Reference 1: 7.1 Arrays
a) 2345
b) 1234
c) 1345
d) 1111
Answer: a
Title: What is output of this for loop that traverses/displays array elements?
Difficulty: Medium
Section Reference 1: 7.1 Arrays
a) The code snippet displays the total marks of all ten subjects.
b) The for loop causes a run-time time error on the first iteration.
c) The code snippet causes a bounds error.
d) The code snippet displays zero.
Answer: c
Title: Which is result of for loop that fills an array and totals its elements?
Difficulty: Hard
Section Reference 1: 7.1 Arrays
9) When an array myArray is only partially filled, how can the programmer keep track of the
current number of elements?
a) access myArray.length()
b) maintain a companion variable that stores the current number of elements
c) access myArray.currentElements()
d) access myArray.length() - 1
Answer: b
10) In a partially filled array, the number of slots in the array that are not currently used is
a) the length of the array minus the number of elements currently in the array
b) the number of elements currently in the array minus the length of the array
c) the length of the array plus the number of elements currently in the array
d) the number of elements currently in the array
Answer: a
Ans: d
12) When the order of the elements is unimportant, what is the most efficient way to remove an
element from an array?
a) Delete the element and move each element after that one to a lower index.
b) Replace the element to be deleted with the last element in the array.
c) Replace the element to be deleted with the first element in the array.
d) Replace the element with the next element.
Answer: b
Title: What is the most efficient way to remove an element in an unordered array?
Difficulty: Easy
Section Reference 1: 7.3 Common Array Algorithms
7.3
13) When an array reading and storing input runs out of space
a) the program could be recompiled with a bigger size for the array.
b) the array could be "grown" using the growArray method.
c) it automatically resizes to accommodate new elements.
d) the array could be "grown" using the new command and the copyOf method.
Answer: d
Answer: a
15) The binary search is faster than the linear search, providing
Answer: b
I. The array algorithms are building blocks for many programs that process arrays
II. Java contains ready-made array algorithms for every problem situation
III. It is inefficient to make multiple passes through an array if you can do everything in one pass
a) I, II
b) I, III
c) II, III
d) I, II, III
Answer: b
17.) Suppose you wish to process an array of values and eliminate any potential duplicate values
stored in the array. Which array algorithms might be adapted for this?
Answer: b
Title: Which array algorithms might be adapted to solve the "remove duplicates" problem?
Difficulty: Medium
Section Reference 1: 7.4 Problem Solving: Adapting Algorithms
18) Which code snippet calculates the sum of all the even elements in an array values?
a)
int sum = 0;
for (int i = 0; i < values.length; i++){
if ((values[i] % 2) == 0){
sum += values[i];
}
}
b)
int sum = 0;
for (int i = 0; i < values.length; i++) {
if ((values[i] % 2) == 0) {
sum++;
}
}
c)
int sum = 0;
for (int i = 0; i < values.length; i++) {
if ((values[i] / 2) == 0) {
sum += values[i];
}
}
d)
int sum = 0;
for (int i = 0; i < values.length; i++) {
if ((values[i] / 2) == 0) {
sum++;
}
}
Answer: a
Title: Which snippet calculates the sum of elements that are even?
Difficulty: Medium
Section Reference 1: 7.3 Common Array Algorithms
19) Which code snippet calculates the sum of all the elements in even positions in an array?
a)
int sum = 0;
Answer: d
Title: Which snippet calculates the sum of elements that are in even positions?
Difficulty: Medium
Section Reference 1: 7.3 Common Array Algorithms
20) Java 7 introduced enhanced syntax for declaring array lists, which is termed
a) angle brackets.
b) method lists.
c) diamond syntax.
d) symmetric slants.
Answer: c
Title: What is the term for the syntax Java 7 introduced for declaring array lists?
Difficulty: Easy
Section Reference 1: 7.7 Array Lists
21) Which statements are true regarding the differences between arrays and array lists?
a) I, II
b) I, III
c) II, III
d) I, II, III
Answer: b
Title: Which statements are true regarding the differences between arrays and array lists?
Difficulty: Medium
Section Reference 1: 7.7 Array Lists
22) What is the value of the count variable after the execution of the given code snippet?
a) 1 b) 2 c) 0 d) 3
Answer: a
Title: What is value of count variable after this for loop traverses the array list?
Difficulty: Medium
Section Reference 1: 7.7 Array Lists
In Java 6 and later, which statement copies the data array to the data2 array?
Answer: b
Difficulty: Medium
Section Reference 1: 7.3 Common Array Algorithms
a) data = Arrays.sort();
b) Arrays.sort(data);
c) data = Arrays.sort(data.length);
d) data = Arrays.sort(data);
Answer: b
Answer: c
26) Which one of the following is the correct header for a method named arrMeth that is called
like this:
Answer: d
Title: Which method header is valid for the method called in this snippet?
Difficulty: Medium
Section Reference 1: 7.3 Common Array Algorithms
Answer: a
Answer: d
Title: What is the output after method call that modifies array parameter variable
Difficulty: Hard
Section Reference 1: 7.3 Common Array Algorithims
29) Consider the following code snippet. Which statement should be used to fill in the empty line
so that the output will be [32, 54, 67.5, 29, 35]?
Answer: c
Title: What statement provides the desired output string from array
Difficulty: Hard
Section Reference 1: 7.3 Common Array Algorithms
30) Select the statement that reveals the logic error in the following method.
Answer: d
Title: What statement reveals the logic error of a method using array
Difficulty: Hard
Section Reference 1: 7.3 Common Array Algorithims
31) Consider the following 2-dimensional array. Select the statement that gives the number of
columns in the third row.
int[][] counts =
{
{ 0, 0, 1 },
{ 0, 1, 1, 2 },
{ 0, 0, 1, 4, 5 },
{ 0, 2 }
};
Answer: d
int cnt = 0;
int[][] numarray = new int[2][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
numarray[j][i] = cnt;
cnt++;
}
}
a) 2 b) 5 c) 3 d) 4
Answer: b
Title: What is the value of this 2-dimension array element after the nested for loop is executed?
Difficulty: Hard
Section Reference 1: 7.6 Two-Dimensional Arrays
33) Which one of the following statements is the correct definition for a two-dimensional array
of 20 rows and 2 columns of the type integer?
Answer: a
Title: Which is correct definition for a 2D integer array of 20 rows and 2 columns?
Difficulty: Medium
int[][] numarray =
{
{ 3, 2, 3 },
{ 0, 0, 0 }
};
System.out.print(numarray[0][0]);
System.out.print(numarray[1][0]);
a) 00 b) 31 c) 30 d) 03
Answer: c
Title: What is output of snippet that initializes 2-dimensional array and displays two of its
elements?
Difficulty: Hard
Section Reference 1: 7.6 Two-Dimensional Arrays
35) Which one of the following statements is correct for displaying the value in the second row
and the third column of a two-dimensional, size 3 by 4 array?
a) System.out.println(arr[1][2]);
b) System.out.println(arr[2][3]);
Unit 4 - Ch 7 Test KEY Page 14 of 21
CCHS Math Unit 4 (Chapter 7) Test Name: _______________
CS-A (100 Points) 12/8/2013
c) System.out.println(arr[2][1]);
d) System.out.println(arr[3][2]);
Answer: a
Title: Which statement displays the value in the second row and third column of a 2-dimensional
3 by 4 array?
Difficulty: Medium
Section Reference 1: 7.6 Two-Dimensional Arrays
What is the output of the given code snippet if the user enters 1,2,0,0,1 as the input?
Answer: c
Title: What is the size of the array list after for loop with given input values?
Difficulty: Medium
Section Reference 1: 7.7 Array Lists
ArrayList<Integer> num;
num.add(4);
System.out.println(num.size());
a) 1
b) 0
c) 4
d) Error because num is not initialized
Answer: d
Title: What is value of array list.size after this snippet with add()?
Difficulty: Easy
Section Reference 1: 7.7 Array Lists
38) What is the value of the cnt variable after the execution of the code snippet below?
a) 1 b) 2 c) 3 d) 0
Answer: a
Title: What is value of cnt variable after this for loop traverses the array list?
Difficulty: Medium
Section Reference 1: 7.7 Array Lists
39) What is the value of myArray[1][2] after this code snippet is executed?
int count = 0;
int[][] myArray = new int[4][5];
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 4; j++)
{
myArray[j][i] = count;
count++;
}
}
a) 8 b) 9 c) 19 d) 11
Answer: b
Title: What is the value of this 2-dimensional array element after the nested for loop is executed?
Difficulty: Hard
Section Reference 1: 7.6 Two-Dimensional Arrays
int[][] numarray =
{
{ 8, 7, 6 },
{ 0, 0, 0 }
};
System.out.print(numarray[0][0]);
System.out.print(numarray[1][0]);
a) 00 b) 87 c) 80 d) 08
Answer: c
Title: What is output of snippet that initializes 2-dimensional array and displays two of its
elements?
Difficulty: Hard
Section Reference 1: 7.6 Two-Dimensional Arrays
int[][] arr =
{
{ 1, 2, 3, 0 },
{ 4, 5, 6, 0 },
{ 0, 0, 0, 0 }
};
int[][] arr2 = arr;
System.out.println(arr2[2][1] + arr2[1][2]);
a) 5 b) 6 c) 7 d) 9
Answer: b
Title: What is the output of this snippet that sums two values in a copied 2-dimensional array
reference?
Difficulty: Medium
Section Reference 1: 7.6 Two-Dimensional Arrays
42) What should you check for when calculating the smallest value in an array list?
Answer: a
Title: What should be checked when calculating the smallest value in an array list?
Difficulty: Easy
Section Reference 1: 7.7 Array Lists
43) Which one of the following is the correct code snippet for calculating the largest value in an
integer array list aList?
a)
int max = 0;
for (int count = 1; count < aList.size(); count++) {
if (aList.get(count) > max) {
max = aList.get(count);
}
}
b)
int max = aList.get(0);
for (int count = 1; count < aList.size(); count++) {
if (aList.get(count) > max) {
max = aList.get(count);
}
}
c)
int max = aList[1];
for (int count = 1; count < aList.size(); count++) {
if (aList.get(count) > max) {
max = aList.get(count);
}
}
d)
int max = aList.get(0);
for (int count = 1; count > aList.size(); count++) {
if (aList.get(count) >= max) {
max = aList.get(count);
}
}
Answer: b
Title: Which is correct code for calculating the largest value in array list of size 6?
Difficulty: Medium
Section Reference 1: 7.7 Array Lists
a) 2 b) 3 c) 4 d) 5
Answer: a
Title: What is the output of the code with a 2-dimensional array having different column sizes
Difficulty: Hard
Section Reference 1: 7.6 Two-Dimensional Arrays
45) Assume the following variable has been declared and given a value as shown:
Answer: b
46) Assume the variable numbers has been declared to be an array that has at least one
element. Which is the following represents the last element in numbers?
a)numbers[numbers.length]
b)numbers.length
c)numbers[–1]
d)numbers[numbers.length – 1]
Answer: d
a)86 2 24 4
b)86 24 3 4
c)1 86 24 4
d)1 2 24 86
Answer: c
a)4 9 11 13
b)4 9 15 22
c)9 11 13 7
d)4 5 6 7
Answer: b
a)7 5 6 4
b)7 6 5 4
c)7 5 6 7
d)4 5 6 4
Answer: c
a)0
b)1
c)2
d)3
Answer: d