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 461–470 of 740
  1. 461
  2. 462
  3. 463
  4. 464
  5. 465
  6. 466
  7. 467
  8. 468
  9. 469
  10. 470

Question 461

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 462

Multiple choice Boolean Expressions
Consider the following method.
public static int process(int n)
{
    int count = 0;

    while (n >= 10)
    {
        if (n % 2 == 0)
            n = n / 2;
        else
            n = n - 1;
        count++;
    }

    return count;
}
What value is returned by process(19) ?
Pick an answer, then check it. The solution opens in a window.

Question 463

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 464

Multiple choice Boolean Expressions
Consider the following code segment. int x = 3; int y = 8; if (x > 4 && y++ > 7) {…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 465

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 466

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 467

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 468

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 469

Multiple choice Boolean Expressions

Consider the following code segment.

boolean a = true;
boolean b = false;
boolean c = true;

boolean value = !(a && b) && (b || c) && !(a && !c);

What value is stored in value?

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

Question 470

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 461 of 740
Page 47 of 74