Learn AP Comp Sci

☰

Problem of the Day

Friday, November 20, 2026


Problem:

A partial listing of the Cookie class is given here.

public class Cookie
{
private String type;

public Cookie(String type)
{
this.type = type;
}

public String getType()
{
return type;
}
}

A separate Runner class contains the following static method.

public static int unknown(ArrayList<Cookie> cookies, String type)
{
int c = 0;
for (Cookie cookie : cookies)
if (cookie.getType().equals(type))
c++;
return cookies.size() - c;
}

What does this static method unknown do?

  1. identifies how many cookies of the indicated type there are
  2. identifies how many cookies in the array are not of the indicated type
  3. removes all cookies that are not of the specified type from the ArrayList cookies
  4. removes all of the cookies of the indicated type from the ArrayList cookies

Show solution:

The correct answer is b. The method unknown initializes a counter variable c and uses a loop to work through all of the cookies in the ArrayList, incrementing c whenever one of the indicated type is found. This number is subtracted from the total number of cookies and returned, identifying the number of cookies that are not of the specified type.