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 441–450 of 740
  1. 441
  2. 442
  3. 443
  4. 444
  5. 445
  6. 446
  7. 447
  8. 448
  9. 449
  10. 450

Question 441

Multiple choice Iteration
Consider the following code segment.
int count = 0;
for (int i = 1; i <= 4; i++)
{
    for (int j = i; j <= 4; j++)
        count++;
}
What is the value of count after the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 442

Multiple choice Boolean Expressions
Consider the following declarations.
int x = 4;
int y = 9;
boolean b = x > 5 || y / x == 2;
What is the value of b after the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 443

Multiple choice Boolean Expressions
Which of the following conditions is true exactly…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 444

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 445

Multiple choice Boolean Expressions
Suppose a, b, and c are int variables. Which of the…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 446

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 447

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 448

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 449

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 450

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 441 of 740
Page 45 of 74