Learn AP Comp Sci

Problem of the Day

Friday, May 22, 2026


Problem:

Tickets for a bus are sold for a price based on the following system.

Consider the method below, intended to identity the total purchase price of a quantity of tickets.

/**
* Calculates bus ticket prices
* @param ticketQuantity the number of tickets being bought
* @param student true if ticket is for a student (20% discount)
* @param senior true if ticket is for a senior (20% discount)
* @param weekend true if ticket(s) purchased on a weekend ($1.00 off total purchase price)
*/
public static double getTotalPrice(int ticketQuantity, boolean student, boolean senior, boolean weekend)
{
final double FULL_PRICE_TICKET = 2.00;

/* missing code */

return purchasePrice;
}

Which of the following can be used to replace /* missing code */ so that the method works as intended?


  1. if (student || senior)
    FULL_PRICE_TICKET *= 0.80;
    double purchasePrice = ticketQuantity * FULL_PRICE_TICKET;
    if (weekend)
    purchasePrice -= 1.00;

  2. double purchasePrice = ticketQuantity * FULL_PRICE_TICKET;
    if (student || senior)
    purchasePrice *= 0.80;
    if (weekend)
    purchasePrice -= 1.00 * ticketQuantity;

  3. double purchasePrice = ticketQuantity * FULL_PRICE_TICKET;
    if (student && weekend || senior && weekend)
    purchasePrice *= 0.80 - 1.00;

  4. double purchasePrice = ticketQuantity * FULL_PRICE_TICKET;
    if (student || senior)
    purchasePrice *= 0.80;
    if (weekend)
    purchasePrice -= 1.00;

Show solution:

The correct answer is d. In choice (a), the value of FULL_PRICE_TICKET cannot be altered. In (b), the code incorrectly discounts every ticket by $1.00 rather than a single discount for the entire purchase. And in (c), students or seniors purchasing a ticket not on a weekend will not get a discount.