0% found this document useful (0 votes)
7 views4 pages

AP Computer Science Exam Practice 10

The document contains multiple-choice questions related to Java programming concepts, including array manipulation, ArrayList methods, and algorithm analysis. It presents code segments and asks for the expected output or identifies issues in the code. The questions test understanding of loops, conditions, and data structures in Java.
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)
7 views4 pages

AP Computer Science Exam Practice 10

The document contains multiple-choice questions related to Java programming concepts, including array manipulation, ArrayList methods, and algorithm analysis. It presents code segments and asks for the expected output or identifies issues in the code. The questions test understanding of loops, conditions, and data structures in Java.
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/ 4

33. Consider the following code segment.

String[][] letters = {{"A", "B", "C", "D"},


{"E", "F", "G", "H"},
{"I", "J", "K", "L"}};

for (int col = 1; col < letters[0].length; col++)


{
for (int row = 1; row < letters.length; row++)
{
System.out.print(letters[row][col] + " ");
}

System.out.println();
}
What is printed as a result of executing this code segment?

(A) A E I
F J
K

(B) B F J
C G K
D H L

(C) E I
F J
G K
H L

(D) F G H
J K L

(E) F J
G K
H L

GO ON TO THE NEXT PAGE.

42 AP Computer Science A Practice Exam


34. The following method is intended to remove all elements of an ArrayList of integers that are divisible
by key and add the removed elements to a new ArrayList, which the method returns.
public static ArrayList<Integer> match(ArrayList<Integer> numList, int key)
{
ArrayList<Integer> returnList = new ArrayList<Integer>();

int i = 0;
while (i < numList.size())
{
int num = numList.get(i);
if (num % key == 0)
{
numList.remove(i);
returnList.add(num);
}
i++;
}
return returnList;
}
As an example, if the method is called with an ArrayList containing the values [5, 2, 10, 20, 16]
and the parameter key has the value 5, then numList should contain [2, 16] at the end of the
method and an ArrayList containing [5, 10, 20] should be returned.

Which of the following best explains why the method does not always work as intended?

(A) The method attempts to add an element to returnList after that element has already been removed
from numList.
(B) The method causes a NullPointerException to be thrown when no matches are found.
(C) The method causes an IndexOutOfBoundsException to be thrown.
(D) The method fails to correctly determine whether an element of numList is divisible by key.
(E) The method skips some elements of numList during the traversal.

GO ON TO THE NEXT PAGE.

AP Computer Science A Practice Exam 43


35. Consider the mode method, which is intended to return the most frequently occurring value (mode) in
its int[] parameter arr. For example, if the parameter of the mode method has the contents
{6, 5, 1, 5, 2, 6, 5}, then the method is intended to return 5.
/** Precondition: arr.length >= 1 */
public static int mode(int[] arr)
{
int modeCount = 1;
int mode = arr[0];

for (int j = 0; j < arr.length; j++)


{
int valCount = 0;
for (int k = 0; k < arr.length; k++)
{
if ( /* missing condition 1 */ )
{
valCount++;
}
}
if ( /* missing condition 2 */ )
{
modeCount = valCount;
mode = arr[j];
}
}
return mode;
}
Which of the following can replace /* missing condition 1 */ and /* missing condition 2 */ so the
code segment works as intended?

/* missing condition 1 */ /* missing condition 2 */


(A) arr[j] == arr[k] valCount > modeCount

(B) arr[j] == arr[k] modeCount > valCount

(C) arr[j] != arr[k] valCount > modeCount

(D) arr[j] != arr[k] modeCount > valCount

(E) arr[j] != arr[k] modeCount != valCount

GO ON TO THE NEXT PAGE.

44 AP Computer Science A Practice Exam


36. Consider the following methods.
/** Precondition: a > 0 and b > 0 */
public static int methodOne(int a, int b)
{
int loopCount = 0;
for (int i = 0; i < a / b; i++)
{
loopCount++;
}
return loopCount;
}

/** Precondition: a > 0 and b > 0 */


public static int methodTwo(int a, int b)
{
int loopCount = 0;
int i = 0;
while (i < a)
{
loopCount++;
i += b;
}
return loopCount;
}
Which of the following best describes the conditions under which methodOne and methodTwo return
the same value?

(A) When a and b are both even


(B) When a and b are both odd
(C) When a is even and b is odd
(D) When a % b is equal to zero
(E) When a % b is equal to one

GO ON TO THE NEXT PAGE.

AP Computer Science A Practice Exam 45

You might also like