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 241–250 of 383
  1. 241
  2. 242
  3. 243
  4. 244
  5. 245
  6. 246
  7. 247
  8. 248
  9. 249
  10. 250

Question 241

Multiple choice Iteration
Consider the following code segment. int sum = 0; for (int k = 2; k <= 9; k = k + 2) { if (k…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 242

Multiple choice Iteration
Consider the following code segment. int n = 18; int total = 0; while (n > 0) { total…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 243

Multiple choice Iteration
Consider the following code segment. int result = 1; for (int i = 0; i <…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 244

Free response Iteration

The ClubAttendance class stores the number of students who attended an after-school club on each recorded meeting day. The declaration below shows the instance variable and method headers.

public class ClubAttendance
{
    private int[] attendance;

    public ClubAttendance(int[] data)
    {
        attendance = data;
    }

    public int countBusyDays(int minStudents)
    { /* to be implemented in part (a) */ }

    public int firstLongRun(int minStudents, int runLength)
    { /* to be implemented in part (b) */ }
}

Part (a). Write the method countBusyDays, which returns the number of entries in attendance that are greater than or equal to minStudents.

For example, if attendance contains {12, 18, 16, 9, 18, 20}, then countBusyDays(18) returns 3.

Part (b). Write the method firstLongRun, which returns the starting index of the first sequence of runLength consecutive entries in attendance that are each greater than or equal to minStudents. If no such sequence exists, the method returns -1.

For example, if attendance contains {12, 18, 16, 19, 20, 10}, then firstLongRun(16, 3) returns 1 because the first run of three qualifying values begins at index 1. The run {18, 16, 19} qualifies, and no earlier run does.

The answer and the full solution open in a window.

Question 245

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

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 246

Multiple choice Iteration

Consider the following code segment.

int x = 4;
int y = 6;

if (x + y > 10)
{
    x = y - x;
}
else
{
    y = x + y;
}

if (x * 2 >= y)
{
    y = y - x;
}
else
{
    x = x + y;
}

System.out.println(x + " " + y);

What is printed?

Pick an answer, then check it. The solution opens in a window.

Question 247

Multiple choice Iteration
Consider the following code segment. String s = "banana"; int count = 0; while (s.indexOf("na") >=…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 248

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

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 249

Multiple choice Iteration
Consider the following code segment. int n = 19; int total = 0; while (n > 0) {…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 250

Multiple choice Iteration
Consider the following code segment. String s = "aaaa"; int n = 0; while (s.length() > 1…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 241 of 383
Page 25 of 39