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 402
x and y are int variables.
(x > 0 && y > 0) || (x > 0 && y <= 0)
Which of the following expressions is equivalent for all values of x and y?Main Solution
Answer
Answer: C. Factor the common clause: (x > 0 && y > 0) || (x > 0 && y <= 0) is x > 0 && (y > 0 || y <= 0). The y disjunction is always true, so the whole expression reduces to x > 0.
Answer: Correct: C
Answer images
Question 403
Question 404
int age = 25;
if (age >= 18 && age < 65)
{
System.out.print("adult ");
}
if (age < 18 || age >= 65)
{
System.out.print("special ");
}
if (!(age >= 18) || !(age < 65))
{
System.out.print("edge ");
}
What is printed when the code segment executes?Main Solution
Answer
Answer: B. With age = 25: first condition 25 >= 18 && 25 < 65 is true — prints "adult ". Second: 25 < 18 || 25 >= 65 is false. Third: the condition !(25 >= 18) || !(25 < 65) is !true || !true = false. Only "adult " prints.
Answer: Correct: B
Answer images
Question 405
Question 406
Question 407
Question 408
public static String g(int n)
{
if (n < 10)
{
return "" + n;
}
return (n % 10) + g(n / 10);
}
What is returned by the call g(1423)?Main Solution
Answer
Answer: D. g(1423) = "3" + g(142) = "3" + "2" + g(14) = "3" + "2" + "4" + g(1). Base: g(1) = "1". Concatenation yields "3" + "2" + "4" + "1" = "3241". The method outputs the digits of n in reverse order.
Answer: Correct: D
Answer images
Question 409
Question 410
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.

