Verityprep
Question Bank

AP® Computer Science A Practice Questions

Work through AP Computer Science A practice questions unit by unit, 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 1,121–1,130 of 3,140
  1. 1121
  2. 1122
  3. 1123
  4. 1124
  5. 1125
  6. 1126
  7. 1127
  8. 1128
  9. 1129
  10. 1130

Question 1121

Multiple choice Unit 2: Selection and Iteration

What is printed?

int score = 78;
String grade = "";
if (score >= 90) {
    grade = "A";
} else if (score >= 80) {
    grade = "B";
} else if (score >= 70) {
    grade = "C";
} else {
    grade = "F";
}
int bonus = score % 10;
if (bonus >= 5) {
    grade = grade + "+";
}
System.out.println(grade + " " + bonus);
Pick an answer, then check it. The solution opens in a window.

Question 1122

Multiple choice Unit 2: Selection and Iteration

What is printed?

String s = "ABCDE";
String result = "";
for (int i = 0; i < s.length(); i++) {
    for (int j = 0; j <= i; j++) {
        result = result + s.substring(j, j + 1);
    }
}
System.out.println(result.length() + " " + result.substring(result.length() - 5));
Pick an answer, then check it. The solution opens in a window.

Question 1123

Multiple choice Unit 2: Selection and Iteration
What is printed?

int x = 5, y = 10; boolean p = !(x > 3 && y < 15); boolean q = x <= 3 || y >= 15; boolean r = !(x > 3) || !(y < 15); System.out.println(p + " " + (p == q) + " " + (q == r));
Pick an answer, then check it. The solution opens in a window.

Question 1124

Multiple choice Unit 2: Selection and Iteration

What is printed?

String s = "ABCDEF";
String result = "";
for (int i = 0; i < s.length(); i += 2) {
    result = s.substring(i, i + 1) + result;
}
System.out.println(result);
Pick an answer, then check it. The solution opens in a window.

Question 1125

Multiple choice Unit 2: Selection and Iteration

What is printed?

int total = 0;
for (int i = 1; i <= 100; i++) {
    if (i % 3 == 0 && i % 5 == 0) {
        total += i;
    }
}
System.out.println(total);
Pick an answer, then check it. The solution opens in a window.

Question 1126

Multiple choice Unit 2: Selection and Iteration

What is printed?

int count = 0;
for (int i = 1; i <= 20; i++) {
    boolean divisible = false;
    for (int j = 2; j * j <= i; j++) {
        if (i % j == 0) {
            divisible = true;
        }
    }
    if (!divisible && i > 1) {
        count++;
    }
}
System.out.println(count);
Pick an answer, then check it. The solution opens in a window.

Question 1127

Free response Unit 2: Selection and Iteration

A regional airport uses a computerized scheduling system to manage daily flight departures. Each flight has a scheduled departure time and an actual departure time, both stored as integers in 24-hour format where the value represents hours and minutes combined. For example, 1430 represents 2:30 PM and 905 represents 9:05 AM. You will write two methods for the FlightScheduler class to help the operations team analyze the day's performance.

PART A

Write the method public int minutesBetween(int time1, int time2) that converts two times from HHMM format to total minutes since midnight and returns the absolute difference. For example, minutesBetween(1430, 900) should return 330 because \(14\times60+30 = 870\) minutes and \(9\times60+0 = 540\) minutes, and the absolute difference is 330.

public int minutesBetween(int time1, int time2) {

PART B

Write the method public int countDelays(int[] scheduled, int[] actual, int threshold) that returns the number of flights where the actual departure was more than threshold minutes after the scheduled time. You may call the minutesBetween method from Part A. A flight is only considered delayed if the actual time is later than the scheduled time and the difference exceeds the threshold.

For example, if scheduled is [800, 1200, 1500] and actual is [830, 1155, 1545] with threshold 20, the method returns 2 because the first flight (30 minutes late) and the third flight (45 minutes late) exceed the threshold.

public int countDelays(int[] scheduled, int[] actual, int threshold) {
The answer and the full solution open in a window.

Question 1128

Multiple choice Unit 2: Selection and Iteration
int x = (int)(Math.random() * 10); while (x < 5) { System.out.print(x + " "); x =…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1129

Multiple choice Unit 2: Selection and Iteration
Consider the following code. int x = (int)(Math.random() * 5); while (x > 0) {…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1130

Multiple choice Unit 2: Selection and Iteration
Consider the following code. int x = 0; while (x < 5) {…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1,121 of 3,140
Page 113 of 314