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 231–240 of 383
  1. 231
  2. 232
  3. 233
  4. 234
  5. 235
  6. 236
  7. 237
  8. 238
  9. 239
  10. 240

Question 231

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

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 232

Multiple choice Iteration
Consider the following code segment.
int n = 81;
int count = 0;
while (n > 1)
{
    if (n % 3 == 0)
        n = n / 3;
    else
        n = n - 1;
    count++;
}
What value is stored in count after the loop terminates?
Pick an answer, then check it. The solution opens in a window.

Question 233

Multiple choice Iteration
Consider the following code segment. int value = 3; for (int k = 0; k…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 234

Multiple choice Iteration
Consider the following method.
public static int process(int n)
{
    int result = 0;
    while (n > 0)
    {
        result = result + n % 10;
        n = n / 100;
    }
    return result;
}
What value is returned by process(58364) ?
Pick an answer, then check it. The solution opens in a window.

Question 235

Free response Iteration

A technical workshop offers several sessions throughout the day. The workshop stores information about each session in three parallel arrays.

  • capacity[i] is the maximum number of students allowed in session i.
  • enrolled[i] is the number of students currently enrolled in session i.
  • canceled[i] is true if session i has been canceled and false otherwise.

You will write two methods of the WorkshopSchedule class. A partial class declaration is shown below.

public class WorkshopSchedule
{
    private int[] capacity;
    private int[] enrolled;
    private boolean[] canceled;

    // constructor not shown

    public int openSeats(int session)
    { /* to be written in part (a) */ }

    public int firstAvailableSession(int needed)
    { /* to be written in part (b) */ }
}

Part (a). Write the method openSeats, which returns the number of open seats in session session. If the session is canceled, the method returns 0.

For example, if capacity[2] = 24, enrolled[2] = 19, and canceled[2] = false, then openSeats(2) returns 5. If canceled[2] = true, then openSeats(2) returns 0.

Part (b). Write the method firstAvailableSession, which returns the index of the first session that has at least needed open seats. The method must ignore canceled sessions. If no session has at least needed open seats, the method returns -1.

Assume part (a) works as intended. Your solution to part (b) must call openSeats.

The answer and the full solution open in a window.

Question 236

Multiple choice Iteration
Consider the following code segment. int total = 0; for (int i = 1; i <= 6; i++) { if (i % 2 == 0 && i > 2)…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 237

Multiple choice Iteration
Consider the following code segment. int n = 50; int count = 0; while (n > 4) { if (n % 2 ==…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 238

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

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 239

Multiple choice Iteration
Consider the following method. public static int mystery(int n) { int count = 0; while (n > 0) {…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 240

Multiple choice Iteration
Consider the following code segment. int total = 0; for (int i = 1; i <= 7; i++) { total…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 231 of 383
Page 24 of 39