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.

Questions 381–390 of 740
  1. 381
  2. 382
  3. 383
  4. 384
  5. 385
  6. 386
  7. 387
  8. 388
  9. 389
  10. 390

Question 381

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 382

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 383

Multiple choice Boolean Expressions
Consider the following code segment.
int[] a = { 2, 4, 6, 8 };
int i = 0;
while (i < a.length && a[i] % 2 == 0)
{
    i++;
}
System.out.println(i);
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 384

Multiple choice Boolean Expressions
Which of the following boolean expressions is equivalent to !(a > 5 && b <= 10) for all int values of a and b?
Pick an answer, then check it. The solution opens in a window.

Question 385

Multiple choice Boolean Expressions
Consider the following code segment.
int x = 15;
int y = 20;
String r;
if (x > 10 && y < 15)
    r = "P";
else if (x > 10 || y > 15)
    r = "Q";
else if (x < 20 && y != 20)
    r = "R";
else
    r = "S";
What value is stored in r after the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 386

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 387

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 388

Multiple choice Iteration
Consider the following code segment.
int n = 50;
int k = 0;
while (n >= 1)
{
    n = n / 2;
    k++;
}
System.out.println(k);
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 389

Multiple choice Iteration
Consider the following code segment.
int n = 0;
int m = 1;
while (m < 1000)
{
    m = m * 2;
    n++;
}
System.out.println(n);
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 390

Multiple choice Boolean Expressions
Consider the following code segment.
int[] arr = { 3, 7, 2, 8, 5, 6 };
int target = 5;
boolean found = false;
int i = 0;
while (!found && i < arr.length)
{
    if (arr[i] == target)
    {
        found = true;
    }
    i++;
}
System.out.println(i);
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.
Question 381 of 740
Page 39 of 74