Learn AP Comp Sci

Problem of the Day

Problem:

A t-shirt can be considered to have four holes in it: one hole for the head, two for the arms, and one at the bottom for the body. The static method holes, given a number of shirts num, is intended to calculate the total number of holes in the those shirts. Which of these static methods works as intended?


  1. public static int holes(int num)
    {
    return num * 4;
    }

  2. public static int holes(int num)
    {
    if (num <= 0)
    return 0;
    else
    return 4 + holes(num - 1);
    }

  3. public static int holes(int num)
    {
    int holeCount = 0;
    for (int i = 0; i < num; i++)
    holeCount += 4;
    return holeCount;
    }
  1. I only
  2. I and II only
  3. I and III only
  4. I, II, and III

Show solution:

The correct answer is d. The first strategy uses multiplication to quickly arrive at the answer, the second strategy uses recursion, and the third strategy uses a loop to calculate the total number of holes in the shirts.