Verityprep
Question Bank · Unit 2

AP® Computer Science A Selection and Iteration Practice Questions

Work through AP Computer Science A practice questions on Selection and Iteration, then check each answer against a worked solution.

Free · no sign-up Open questions need no account. Sign up or log in to open the rest.

Subtopic: Iteration Clear filter

Questions 191–200 of 383
  1. 191
  2. 192
  3. 193
  4. 194
  5. 195
  6. 196
  7. 197
  8. 198
  9. 199
  10. 200

Question 191

Free response Iteration
The FestivalManager class contains methods used for a game booth at a school festival. public class FestivalManager { / * * Returns a random integer from 1…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 192

Multiple choice Iteration
What is printed?
int n = 100;
int count = 0;
while (n > 1)
{
    n = n / 2;
    count++;
}
System.out.println(count);
Pick an answer, then check it. The solution opens in a window.

Question 193

Multiple choice Iteration
What is printed?
int count = 0;
for (int i = 0; i < 5; i++)
{
    for (int j = i; j < 5; j++)
    {
        count++;
    }
}
System.out.println(count);
Pick an answer, then check it. The solution opens in a window.

Question 194

Multiple choice Iteration
What is printed?
int sum = 0;
for (int k = 2; k <= 20; k += 3)
{
    sum += k;
}
System.out.println(sum);
Pick an answer, then check it. The solution opens in a window.

Question 195

Multiple choice Iteration
What is printed?
int n = 1;
int total = 0;
while (total < 30)
{
    total = total + n;
    n = n + 2;
}
System.out.println(n + " " + total);
Pick an answer, then check it. The solution opens in a window.

Question 196

Multiple choice Iteration
What is printed?
for (int i = 1; i <= 3; i++)
{
    for (int j = 1; j <= i; j++)
    {
        System.out.print(i + "" + j + " ");
    }
}
System.out.println();
Pick an answer, then check it. The solution opens in a window.

Question 197

Multiple choice Iteration
What is printed?
int count = 0;
for (int k = 1; k <= 50; k++)
{
    if (k % 3 == 0 && k % 5 != 0)
    {
        count++;
    }
}
System.out.println(count);
Pick an answer, then check it. The solution opens in a window.

Question 198

Free response Iteration

A small neighborhood grocery store tracks how many customers are waiting in each of its checkout lanes. The store uses the class GroceryStore, whose skeleton is shown below.

public class GroceryStore
{
    /** The number of customers currently waiting in each lane.
     *  laneQueues[k] stores the customers in lane k (0-based).
     *  The array is never null and has at least one element. */
    private int[] laneQueues;

    /** The number of seconds it takes to serve one customer
     *  in any lane. Always positive. */
    private int secondsPerCustomer;

    /** Constructs a GroceryStore with the given queue snapshot and
     *  per-customer service time. */
    public GroceryStore(int[] queues, int secs)
    {
        /* not shown */;
    }

    /** Returns the estimated wait time, in seconds, for a customer
     *  who joins the back of lane laneIndex right now. Part (a) */
    public int estimatedWait(int laneIndex)
    {
        /* part (a) */;
    }

    /** Returns the index of the lane with the shortest estimated wait.
     *  If multiple lanes are tied, the lowest such index is returned.
     *  Part (b) */
    public int fastestLane()
    {
        /* part (b) */;
    }

    // Other methods and constructors are not shown.
}

Part A

Write the method estimatedWait. A customer joining the back of a lane must wait for every customer already in that lane to be served plus the time to be served themselves. Each customer takes exactly secondsPerCustomer seconds.

For example, suppose secondsPerCustomer is 45 and laneQueues is {3, 1, 4, 2}. The following table shows the expected return values.

Call Returned Value Explanation
estimatedWait(0) 180 3 ahead + 1 self = 4 × 45
estimatedWait(1) 90 1 ahead + 1 self = 2 × 45
estimatedWait(2) 225 4 ahead + 1 self = 5 × 45
estimatedWait(3) 135 2 ahead + 1 self = 3 × 45

Assume laneIndex is a valid index in laneQueues.

Part B

Write the method fastestLane. It returns the index of the lane with the shortest estimated wait for a new customer joining now. If two or more lanes are tied for the minimum, return the smallest such index.

For example, if secondsPerCustomer is 30 and laneQueues is {2, 0, 2, 0, 3}, then the estimated waits are {90, 30, 90, 30, 120}. The minimum is 30, achieved by lanes 1 and 3, so fastestLane() returns 1.

You must call estimatedWait appropriately to receive full credit. Assume estimatedWait works as specified regardless of what you wrote in Part A.

The answer and the full solution open in a window.

Question 199

Multiple choice Iteration
Consider the following code segment.
int count = 0;
for (int i = 1; i < 10; i++)
{
    for (int j = i; j < 10; j += 2)
    {
        count++;
    }
}
What value is stored in count after the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 200

Multiple choice Iteration
Consider the following code segment.
for (int i = 0; i < 4; i++)
{
    for (int j = 0; j <= i; j++)
    {
        System.out.print("*");
    }
    System.out.println();
}
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.
Question 191 of 383
Page 20 of 39