AP® Computer Science A Selection and Iteration Multiple Choice Questions
Work through AP Computer Science A multiple-choice 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 372
Question 373
Question 374
Question 375
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(2);
nums.add(4);
nums.add(4);
nums.add(6);
nums.add(4);
for (int i = 0; i < nums.size(); i++)
{
if (nums.get(i) == 4)
{
nums.remove(i);
}
}
System.out.println(nums);
What is printed when the code segment executes?Main Solution
Answer
Answer: B. This is the classic index-shift bug. When remove(i) is called, every later element slides one position to the left, but i is still incremented. Trace: at i=0 value 2 (skip); at i=1 value 4 (remove) → list becomes [2, 4, 6, 4]; at i=2 value 6 (skip, because the middle 4 shifted into index 1 and was jumped over); at i=3 value 4 (remove) → list becomes [2, 4, 6]; the loop ends because i = 4 is no longer less than size() = 3.
Answer: Correct: B
A
[2, 6]
B
[2, 4, 6]
C
[2, 4, 4, 6, 4]
D
[2, 6, 4]
Answer images
Question 376
Question 377
Question 378
Question 379
Question 380
a, b, and c are boolean variables.
!(a && (b || !c))
Which of the following is logically equivalent to the expression above?Main Solution
Answer
Answer: C. Apply De Morgan's law twice. !(a && X) becomes !a || !X, where X = (b || !c). Then !(b || !c) becomes !b && !!c = !b && c. So the equivalent is !a || (!b && c). Option A applies De Morgan to (b || !c) incorrectly (using || instead of &&); option B incorrectly turns the outer || into &&; option D is just !a && the original inner expression, which is wrong on both sides.
Answer: Correct: C
A
!a || (!b || c)
B
!a && (!b || c)
C
!a || (!b && c)
D
!a && (b || !c)
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.

