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 201–210 of 383
  1. 201
  2. 202
  3. 203
  4. 204
  5. 205
  6. 206
  7. 207
  8. 208
  9. 209
  10. 210

Question 201

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 202

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 203

Multiple choice Iteration
Consider the following code segment.
int x = 100;
int count = 0;
while (x > 1)
{
    if (x % 3 == 0)
    {
        x = x / 3;
    }
    else
    {
        x = x - 1;
    }
    count++;
}
System.out.println(count);
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 204

Free response Iteration

An electric utility models its service area as a network of power plants and demand regions. The utility keeps three parallel data structures:

  • generation — a double[] where generation[p] is the rated output of plant p, in megawatts (MW).
  • demand — a double[] where demand[r] is the instantaneous load of region r, in megawatts (MW).
  • distance — a double[][] where distance[p][r] is the length of the transmission line from plant p to region r, in kilometers. A value of -1.0 indicates that no line exists.

Power delivered to a region is reduced by transmission loss. The utility uses the following efficiency factor for a line of length d kilometers:

efficiency(d) = 1.0 - 0.002 * d / / d >= 0

If the efficiency is negative for some d, the method is still required to return the literal result of the formula (the utility will reject such a route elsewhere).

You will implement two methods of the EnergyGrid class. A partial class declaration is shown below.

public class EnergyGrid
{
    private double[] generation;
    private double[] demand;
    private double[][] distance;
    / / constructor and other methods not shown
    public double powerDelivered(int plant, int region)
    { / * to implement in part (a) * / }
    public int closestAdequatePlant(int region)
    { / * to implement in part (b) * / }
}

Part (a) — powerDelivered

Write the method powerDelivered. The method returns the power (in MW) that plant plant delivers to region region, computed as generation[plant] * efficiency(distance[plant][region]). If no line exists between the plant and the region (distance[plant][region] equals -1.0), the method must return 0.0 without computing the formula.

Method Signature

public double powerDelivered(int plant, int region)

For example, suppose generation = {400.0, 260.0}, demand = {150.0, 300.0, 90.0}, and

distance = {
    { 50.0, 120.0, - 1.0 },
    {220.0, 80.0, 30.0 }
}

Then powerDelivered(0, 0) returns 400.0 * (1.0 - 0.002 * 50.0) = 360.0. powerDelivered(0, 2) returns 0.0 because no line exists. powerDelivered(1, 1) returns 260.0 * (1.0 - 0.002 * 80.0) = 218.4.

Part (b) — closestAdequatePlant

Write the method closestAdequatePlant. The method returns the index of the plant that is closest to region region (smallest distance[plant][region]) among all plants that satisfy both of the following conditions:

  • a transmission line exists between the plant and the region; that is, distance[plant][region] is not -1.0;
  • the power delivered to the region by that plant, as computed in part (a), is at least demand[region].

If no plant satisfies both conditions, the method returns -1. If several plants tie for the smallest distance, the method returns the smallest index among them.

Assume part (a) is implemented correctly. You must call powerDelivered; do not repeat its formula.

Method Signature

public int closestAdequatePlant(int region)

Continuing the example above: for closestAdequatePlant(1), plant 0 delivers 400 * (1 - 0.002 * 120) = 304.0 MW, which meets the 300 MW demand, and plant 1 delivers 260 * (1 - 0.002 * 80) = 218.4 MW, which does not. Only plant 0 qualifies, so the method returns 0. For closestAdequatePlant(2) only plant 1 has a line (distance 30, delivery 260 * 0.94 = 244.4 MW > 90), so the method returns 1.

The answer and the full solution open in a window.

Question 205

Multiple choice Iteration
Consider the following code segment. int k = 0; for (int i = 1; i <= 10; i++) { if (i % 3 == 0…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 206

Multiple choice Iteration
Consider the following code segment. int count = 0; for (int i = 0; i < 4; i++) { for (int j = i; j < 4;…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 207

Multiple choice Iteration
Consider the following code segment. int n = 12; int step = 0; while (n > 1) { if (n % 2 == 0) n = n / 2;…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 208

Multiple choice Iteration
Consider the following code segment. int[] arr = { 2, 4, 6, 8, 10 }; int sum = 0; int i = 1; while (i <…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 209

Multiple choice Iteration
Consider the following code segment. int x = 0; int y = 10; while (x < y) { x = x + 2; y =…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 210

Multiple choice Iteration
Consider the following code segment, which is intended to remove every even value from nums. ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(2);…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 201 of 383
Page 21 of 39