AP Computer Science A · Unit practice

AP Computer Science A Unit 2: practice questions

5 exam-style questions with answers and explanations. Answer them on FiveStudy to keep a streak and see your AP® score estimate.

Question 1

A store program must read a purchase total, apply a 10 percent discount when the total is greater than 50 dollars, and then print the amount the customer owes. Which of the following orderings of the steps produces the correct amount owed?

  • A. Read the total, then print the total, then reduce the total by 10 percent if the total is greater than 50.
  • B. Read the total, then reduce the total by 10 percent, then print the total if the total is greater than 50.
  • C. Reduce the total by 10 percent if the total is greater than 50, then read the total, then print the total.
  • D. Read the total, then reduce the total by 10 percent if the total is greater than 50, then print the total.

Answer: Read the total, then reduce the total by 10 percent if the total is greater than 50, then print the total.

The total must be read first, tested and discounted only when it is greater than 50, and printed last. The other orderings print before the discount is applied, discount every purchase, or test a total that has not been read yet.

Question 2

An algorithm has three steps. Step 1: set a counter to 0. Step 2: for each student in the class, print the student's name and add 1 to the counter. Step 3: print the value of the counter. Which part of the algorithm requires repetition?

  • A. Printing a name and adding 1 to the counter for each student
  • B. Setting the counter to 0 before any names are printed
  • C. Printing the value of the counter after the last name
  • D. Deciding the order in which the three steps are listed

Answer: Printing a name and adding 1 to the counter for each student

A step that must happen once for each student is carried out by repetition. Setting the counter and printing it at the end each happen exactly one time, and the order of the steps is a design decision rather than a step.

Question 3

A program stores two different int values in the variables first and second and must print the larger of the two values. Which of the following algorithms accomplishes this goal?

  • A. If first is greater than second, print second; otherwise, print first.
  • B. If first is greater than second, print first; otherwise, print second.
  • C. If first is greater than second, print first, and then print second.
  • D. Print first, and then, if first is greater than second, print second.

Answer: If first is greater than second, print first; otherwise, print second.

Two-way selection prints whichever of the two values is greater, which is exactly what the correct algorithm does. The other algorithms print the smaller value, print both values, or print a value before any comparison is made.

Question 4

A check-in program reads each arriving guest's name, admits the guest if the name appears on the guest list, adds 1 to a count of admitted guests, and prints that count after the last guest arrives. Which of the following steps of this algorithm is a selection step?

  • A. Reading the name of each guest who arrives at the event
  • B. Adding 1 to the count of guests who have been admitted
  • C. Admitting the guest when the name appears on the guest list
  • D. Printing the number of guests admitted at the end

Answer: Admitting the guest when the name appears on the guest list

A selection step performs an action only when a condition holds, which is what admitting a guest whose name is on the list does. Reading a name, adding 1 to a count, and printing a final total each happen without testing a condition.

Question 5

An algorithm sets a counter to 0 and then examines each item on a shopping list. Each time it finds an item that costs more than 10 dollars, it adds 1 to the counter. After the last item is examined, the algorithm prints the counter. Which of the following best describes the value printed by the algorithm?

  • A. The total cost of the items on the list that cost more than 10 dollars
  • B. The number of items that appear on the shopping list
  • C. The cost of the most expensive item on the shopping list
  • D. The number of items on the list that cost more than 10 dollars

Answer: The number of items on the list that cost more than 10 dollars

The counter increases by 1 for each qualifying item, so it ends as a count of the expensive items. Adding each item's cost instead of 1 would give a total cost, and removing the condition would count every item on the list.

More AP Computer Science A practice

Other units