AP® Computer Science A Selection and Iteration Practice Questions
Work through AP Computer Science A practice questions on Selection and Iteration, then check each answer against a worked solution.
Free · no sign-up Open questions need no account. Sign up or log in to open the rest.
Question 462
public static int process(int n)
{
int count = 0;
while (n >= 10)
{
if (n % 2 == 0)
n = n / 2;
else
n = n - 1;
count++;
}
return count;
}
process(19) ?Main Solution
Answer
Answer: A. Since 19 is odd, it becomes 18. Then 18 is even, so it becomes 9. At that point the loop stops because 9 < 10. The method returns 2.
Answer: Correct: A
A
2
B
3
C
1
D
4
Answer images
Question 463
The ClubAttendance class stores the number of students who attended an after-school club on each recorded meeting day. The declaration below shows the instance variable and method headers.
public class ClubAttendance
{
private int[] attendance;
public ClubAttendance(int[] data)
{
attendance = data;
}
public int countBusyDays(int minStudents)
{ /* to be implemented in part (a) */ }
public int firstLongRun(int minStudents, int runLength)
{ /* to be implemented in part (b) */ }
}
Part (a). Write the method countBusyDays, which returns the number of entries in attendance that are greater than or equal to minStudents.
For example, if attendance contains {12, 18, 16, 9, 18, 20}, then countBusyDays(18) returns 3.
Part (b). Write the method firstLongRun, which returns the starting index of the first sequence of runLength consecutive entries in attendance that are each greater than or equal to minStudents. If no such sequence exists, the method returns -1.
For example, if attendance contains {12, 18, 16, 19, 20, 10}, then firstLongRun(16, 3) returns 1 because the first run of three qualifying values begins at index 1. The run {18, 16, 19} qualifies, and no earlier run does.
Main Solution
Sample Solution
public int countBusyDays(int minStudents)
{
int count = 0;
for (int i = 0; i < attendance.length; i++)
{
if (attendance[i] >= minStudents)
{
count++;
}
}
return count;
}
public int firstLongRun(int minStudents, int runLength)
{
for (int start = 0; start <= attendance.length - runLength; start++)
{
boolean good = true;
for (int k = 0; k < runLength; k++)
{
if (attendance[start + k] < minStudents)
{
good = false;
}
}
if (good)
{
return start;
}
}
return -1;
}
Scoring (9 pts): Part (a): +1 correct traversal, +1 correct comparison >=, +1 increments count correctly, +1 returns the count. Part (b): +1 checks every possible starting index, +1 uses a nested check of runLength entries, +1 rejects runs containing any nonqualifying entry, +1 returns the first valid starting index, +1 returns -1 when no run exists.
Answer images
Question 464
Question 465
Question 466
Consider the following code segment.
int x = 4;
int y = 6;
if (x + y > 10)
{
x = y - x;
}
else
{
y = x + y;
}
if (x * 2 >= y)
{
y = y - x;
}
else
{
x = x + y;
}
System.out.println(x + " " + y);
What is printed?
Main Solution
Answer
Answer: D. The first condition is false because 4 + 6 is not greater than 10, so y becomes 10. Then x * 2 is 8, which is less than 10, so x becomes 14.
Answer: Correct: D
A
10 6
B
4 10
C
2 4
D
14 10
Answer images
Question 467
Question 468
Question 469
Consider the following code segment.
boolean a = true;
boolean b = false;
boolean c = true;
boolean value = !(a && b) && (b || c) && !(a && !c);
What value is stored in value?
Main Solution
Answer
Answer: C. !(a && b) is true, (b || c) is true, and !(a && !c) is also true because !c is false.
Answer: Correct: C
A
false, because a && b is false
B
false, because b is false
C
true
D
false, because a is true
Answer images
AP® and Advanced Placement® are registered trademarks of the College Board, which is not affiliated with, and does not endorse, Verityprep. Names are used for identification purposes only.

