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
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);
A8 2
B9 3
C9.4 2
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 2 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
B6
C7
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 3 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);
A0
B3
C1
D2
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 4 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
B0
Cnull
DThe code does not compile because count is never assigned
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 5 of 10
MCQU1Medium No calc Diagram
The diagram shows the state of memory after two variables have been assigned. Both refer to the same Box object. variablesobjectbox1box2Boxvalue = 5 The following statements then run:
box2.setValue(9);System.out.println(box1.getValue());
What is printed?
ANothing — the program fails to compile
B9
C0
D5
Explanation
Both names refer to one object, so changing it through box2 changes what box1 sees. The diagram shows two arrows meeting a single box, which is the clue: there is one object, not two.
Question 6 of 10
MCQU4Medium No calc Diagram

The diagram shows the contents of a two-dimensional <code>int</code> array named <code>m</code>, with its row and column indices labelled outside the cells.

column indexrow index0123038161729425062

What value does <code>m[2][1]</code> hold?

A8
B0
C7
D9
Explanation
The first index selects the row and the second selects the column, so m[2][1] is row 2, column 1 — the value 0. Reading the pair in the other order gives row 1, column 2, which is 9.
Question 7 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);
A7
B9
C10
D8
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).
Question 8 of 10
MCQU4Medium No calc Diagram

The diagram shows an <code>ArrayList&lt;String&gt;</code> named <code>list</code> before and after one removal. Index labels are shown beneath each element.

before"A""B""C""D"0123after list.remove(1)"A""C""D"012

After the removal shown, what does <code>list.get(1)</code> return?

A"A"
B"C"
C"B"
D"D"
Explanation
Removing an element shifts every later element one position toward the front, so what was at index 2 is now at index 1. The list also shrinks to size 3, which is why a loop that removes while counting upward can skip elements.
Question 9 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?

A39
B23
C62
D8
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 10 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?

A20
B16
C22
D2
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.
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