AP Computer Science A · Unit practice

AP Computer Science A Unit 3: 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 school library program models each book with a title, an author, and a number of pages. Books can be checked out and returned. Which of the following is best represented as an attribute of a Book class?

  • A. Checking the book out to a student
  • B. The number of pages the book contains
  • C. Returning the book to the library
  • D. Printing a label for the book

Answer: The number of pages the book contains

Attributes hold the data that describes each object, and a page count is such a value. Checking out, returning, and printing a label are actions the program performs, so they are behaviors.

Question 2

A music app models a playlist that stores a name and the songs a listener has chosen. Which of the following is best represented as a behavior of a Playlist class?

  • A. The name the listener gave the playlist
  • B. The number of songs in the playlist
  • C. Adding a song to the end of the playlist
  • D. The total playing time of the playlist

Answer: Adding a song to the end of the playlist

Behaviors are the actions an object performs, and adding a song changes the playlist. The name, the song count, and the total playing time are values that describe a playlist, so they are attributes.

Question 3

A driving game includes a Car class. Code that uses a Car object needs to start the car, steer it, and read its current speed. Which of the following details is best hidden from code that uses the Car class?

  • A. The method other code calls to start the engine
  • B. The method other code calls to read the speed
  • C. The method other code calls to steer the wheels
  • D. The formula that converts engine revolutions into a speed

Answer: The formula that converts engine revolutions into a speed

Abstraction hides internal computation while leaving the operations other code needs visible. The three methods make up the interface that game code calls, so hiding them would leave the class unusable.

Question 4

A vending machine program models each slot in the machine. The program displays each slot's item and price and reports when a slot is empty. Which of the following sets of attributes best models one slot of the machine?

  • A. The name of the item, the price of the item, and the number of items left
  • B. The name of the item, the price of the item, and the name of the buyer
  • C. The name of the item, the number of items left, and the current time of day
  • D. The price of the item, the name of the buyer, and the machine's street address

Answer: The name of the item, the price of the item, and the number of items left

A slot must describe what it holds, what that item costs, and how many of it remain. A buyer's name, the time of day, and the machine's address describe other things, so they do not belong to a slot.

Question 5

A program prints a report card for each student in a school. Which of the following would be the LEAST appropriate attribute of the Student class?

  • A. The student's identification number
  • B. The name of the school's principal
  • C. The student's current grade level
  • D. The student's grade in each course

Answer: The name of the school's principal

The principal is the same for every student, so that value describes the school rather than an individual student. An identification number, a grade level, and course grades differ from one student to the next.

More AP Computer Science A practice

Other units