AP® Computer Science A
Quick Drill · 10 Questions · 30 min
30:00Exit
1
2
3
4
5
6
7
8
9
10
FR
Question 1 of 10
MCQU4Topic 1D arrays: max algorithmMedium No calc
int[] data = {23, 41, 8, 62, 15, 39};int m = data[0];for (int i = 1; i < data.length; i++) {    if (data[i] > m) {        m = data[i];    }}System.out.println(m);

What is printed?

A23
B8
C39
D62
Explanation
This is the standard maximum search. m starts at data[0] = 23, then is replaced by any later element that is larger: 41 replaces 23, then 62 replaces 41, and nothing after exceeds 62, so m ends at 62. If the comparison were < instead of >, m would track the minimum, 8. Forgetting to ever update m leaves the first element 23, and reading only the last element gives 39.
Question 2 of 10
MCQU4Topic 1D arrays: enhanced-for with conditionMedium No calc
int[] vals = {4, 7, 2, 9};int t = 0;for (int v : vals) {    if (v > 4) {        t += v;    }}System.out.println(t);

What is printed?

A22
B2
C20
D16
Explanation
The enhanced-for visits each value and adds it to t only when it is strictly greater than 4. The values 7 and 9 qualify (7 + 9 = 16); 4 is not greater than 4, and 2 is too small. Adding every value gives 22, and treating the test as >= 4 would wrongly include the 4 for a total of 20. Counting how many values pass rather than summing them gives 2.
Question 3 of 10
MCQU1Topic Unit 1 arithmetic (integer division and modulus)Easy No calc
What is printed by the following code?
int a = 47;int b = 5;System.out.println(a / b + " " + a % b);
A9.4 2
B8 2
C9 3
D9 2
Explanation
Both a and b are int, so a / b is integer division: 47 / 5 discards the fractional part and yields 9 (not 9.4 — no double is involved). The modulus a % b is the remainder of 47 divided by 5, which is 47 - 45 = 2. Concatenating 9, a space, and 2 gives the two numbers separated by a space. The 9.4 trap assumes division produces a decimal, but integer division truncates.
Question 4 of 10
MCQU2Topic 2.D short-circuit evaluationMedium No calc

What is printed?

int[] data = {4, 0, 8};int i = 0, hits = 0;while (i < data.length && data[i] != 0) {    hits++;    i++;}System.out.println(hits);
A3
B2
C0
D1
Explanation
The loop keeps going only while BOTH i is in bounds AND the current element is non-zero. First pass: i = 0, data[0] = 4 != 0, so hits becomes 1 and i becomes 1. Second test: i = 1 is in bounds but data[1] = 0, so data[i] != 0 is false and && short-circuits the whole condition to false. The loop stops immediately, so the 8 at index 2 is never reached. A student who assumes the loop counts every non-zero element in the array gets 2, but the loop terminates at the first zero it meets.
Question 5 of 10
MCQU1Topic Unit 1 Math methodsMedium No calc
What is printed by the following code?
System.out.println(Math.abs(-6) + (int) Math.pow(2, 3) + (int) Math.sqrt(49));
A23
B21
C42
D20
Explanation
Math.abs(-6) returns 6. Math.pow(2, 3) computes 2 to the 3rd power, which is 8.0, and the cast to int makes it 8. Math.sqrt(49) is 7.0, cast to int gives 7. Since all three are int, the additions are ordinary arithmetic: 6 + 8 + 7 = 21. Nothing here is string concatenation, so the digits are not glued together into 42.
Question 6 of 10
MCQU1Topic Unit 1 indexOf and substringMedium No calc
What is printed by the following code?
String email = "user@site.org";int at = email.indexOf("@");System.out.println(email.substring(at + 1));
Asite
Buser
C@site.org
Dsite.org
Explanation
indexOf("@") returns the index of the first '@'. Counting from 0: 'u'0 's'1 'e'2 'r'3 '@'4, so at is 4. substring(at + 1) is substring(5), which returns everything from index 5 to the end: 'site.org'. Using substring(at) instead of substring(at + 1) would keep the '@' and give '@site.org'. There is no second argument, so nothing is trimmed from the end.
Question 7 of 10
MCQU3Topic 3.A default field values / constructorsMedium No calc
Given this class:
public class Meter {    private int count;    public Meter() { }    public int getCount() { return count; }}
What is printed?
Meter m = new Meter();System.out.println(m.getCount());
AAn unpredictable garbage value
BThe code does not compile because count is never assigned
C0
Dnull
Explanation
The no-argument constructor has an empty body, so it never assigns count. In Java, instance variables are given a default value when the object is created: for an int that default is 0. So getCount() returns 0. The 'garbage value' trap comes from languages where uninitialized memory is undefined, but Java guarantees a zero default for numeric fields. 'null' is wrong because null is the default for object references, not for the primitive type int. It compiles fine because reading a field that holds its default is legal.
Question 8 of 10
MCQU2Topic 2.E if / else-if ladder vs separate ifMedium No calc

What is the value of r after this segment runs?

int x = 15;int r = 0;if (x > 5) r += 1;else if (x > 10) r += 2;if (x % 3 == 0) r += 4;System.out.println(r);
A5
B7
C6
D3
Explanation
The first if/else-if is one linked structure: x > 5 is true so r += 1 makes r = 1, and because that branch ran, the attached else if (x > 10) is skipped even though x > 10 is also true. The second if is a completely separate statement: 15 % 3 == 0 is true, so r += 4 makes r = 5. A student who thinks both the if and the else if can fire adds 1 and 2 and 4 to get 7; an else-if branch never runs once an earlier branch in the same ladder has executed.
Question 9 of 10
MCQU3Topic 3.E toString and printing an objectMedium No calc
Given this class:
public class Coin {    private int cents;    public Coin(int c) { cents = c; }    public String toString() { return cents + "c"; }}
What is printed?
Coin q = new Coin(25);System.out.println(q);
ACoin
BCoin@ followed by a hash code
CThe code does not compile because you cannot print an object
D25c
Explanation
When an object is passed to println, Java automatically calls that object's toString() method to obtain the text to display. Because Coin overrides toString() to return cents followed by the letter c, and cents holds 25, the result is 25c. The 'Coin@ hash code' trap is what the inherited Object.toString() would produce, but that version is overridden here, so it is never used. It compiles because println is defined to accept an Object and call toString on it.
Question 10 of 10
MCQU2Topic 2.G while loops / counting iterationsMedium No calc

How many times does the loop body execute (final value of steps)?

int n = 40;int steps = 0;while (n > 1) {    if (n % 2 == 0) n = n / 2;    else n = 3 * n + 1;    steps++;}System.out.println(steps);
A9
B8
C10
D7
Explanation
Trace n and increment steps each pass: 40->20 (1), 20->10 (2), 10->5 (3), 5 is odd so 3*5+1=16 (4), 16->8 (5), 8->4 (6), 4->2 (7), 2->1 (8). When n becomes 1 the condition n > 1 is false and the loop stops. steps was incremented 8 times. A common off-by-one error stops counting the final 2->1 step (giving 7) or counts one extra phantom pass after n reaches 1 (giving 9).
Free Response 1 · Section II
FRQMethods and Control StructuresU2 No calc

A city parking app is modeled by the ParkingMeter class below. A meter stores the number of minutes of paid time remaining. The constructor, getMinutes, and expired methods are already written for you.

public class ParkingMeter {    private int minutes;   // minutes of paid time remaining    public ParkingMeter(int m) {        minutes = m;    }    /** Returns the minutes of paid time remaining. */    public int getMinutes() {        return minutes;    }    /** Returns true if no paid time remains. */    public boolean expired() {        return minutes <= 0;    }    // Part (a): write tick    // Part (b): write plateState}

Part (a) Write the method tick, which simulates n minutes passing. For each of the n minutes, if the meter is not already expired, one minute of remaining time is used up. The method returns the number of minutes that were actually counted down (which may be fewer than n if the time runs out). Your method must call expired. For example, a meter created with 5 minutes on which tick(3) is called returns 3 and has 2 minutes left; a meter created with 2 minutes on which tick(5) is called returns 2 and has 0 minutes left.

/** Uses up one minute for each of n passing minutes while time remains; *  returns how many minutes were counted down. */public int tick(int n)

Part (b) Write the method plateState, which is given a license-plate string in the form state-abbreviation, a hyphen, then the plate characters, such as "TX-ABC123". The method returns the part of the string before the hyphen (for example "TX"). If the string contains no hyphen, the method returns "??".

/** Returns the state prefix (text before the first '-'), or "??" if no '-'. */public String plateState(String plate)
Free response is self-scored — work it out, then reveal the model answer and scoring checklist to compare.

Score
Correct
Wrong
Try Again Exit