AP Computer Science A Primitive Types — Worked Answer Explanations
Unit 1 · 12 questions explained
Below is a complete answer key for our AP Computer Science A Primitive Types practice questions. For each question you'll find the correct choice, a full written explanation of how to get there, and — for every wrong answer — a short note on exactly why it's tempting and where it goes wrong. Reading these straight through is one of the fastest ways to find the gaps in a unit before exam day.
Prefer to test yourself first? Take the timed Primitive Types practice test and come back here to review, or head back to the Primitive Types unit overview.
- Question 1 · Easy
What is the output of the following code?
int a = 17; int b = 5; System.out.println(a / b); System.out.println(a % b);- A3\n2Correct
- B3.4\n2Why not B: Both operands are int, so
/performs integer division and returns an int, not 3.4. - C3\n0Why not C: 17 % 5 is the remainder of 17 / 5 = 3 remainder 2, so the modulus is 2, not 0.
- D4\n2Why not D: Java integer division truncates toward zero; 17/5 = 3 remainder 2, not rounded up to 4.
ExplanationInteger division in Java truncates the decimal portion. 17 / 5 = 3 (truncated from 3.4). The modulus operator
%returns the remainder: 17 = 5*3 + 2, so 17 % 5 = 2.Key takeawayJava integer division truncates; `%` returns the remainder after integer division.
- A
- Question 2 · Easy
What are the values of
xandyafter the following code executes?double x = 5 / 2; double y = 5.0 / 2;- Ax = 2.5, y = 2.5Why not A: The right-hand side of x is computed as integer division (5/2=2) before being stored in the double variable.
- Bx = 2.0, y = 2.5Correct
- Cx = 2, y = 2Why not C: x is stored as a double so it becomes 2.0; y uses double arithmetic so it becomes 2.5, not 2.
- Dx = 2.5, y = 2.0Why not D: The operand types determine the operation: 5/2 uses int arithmetic; 5.0/2 uses double arithmetic.
ExplanationIn
5 / 2, both operands areint, so Java does integer division yielding2. That2is then widened to2.0when stored inx. In5.0 / 2, one operand isdouble, so the division is double arithmetic yielding2.5.Key takeawayThe type of operands determines how division is computed; the variable's type only affects storage, not the computation.
- A
- Question 3 · Easy
What is the value of
resultafter this code runs?int result = (int) 9.99;- A10Why not A: Casting to int truncates the decimal; it does NOT round. 9.99 becomes 9, not 10.
- B9Correct
- C9.99Why not C: The cast
(int)converts the double to an int, dropping the fractional part. - DCompile errorWhy not D: Explicit casting from double to int is valid Java syntax.
ExplanationCasting from
doubletointtruncates (chops off) the fractional part without rounding.(int) 9.99becomes9. This is called a narrowing primitive conversion.Key takeawayCasting double to int truncates toward zero — it does not round.
- A
- Question 4 · Easy
Which of the following correctly declares a boolean variable and assigns it the result of a comparison?
// Option A boolean result = (7 > 3); // Option B Boolean result = 7 > 3; // Option C bool result = 7 > 3; // Option D boolean result = "7 > 3";- AOption A onlyCorrect
- BOptions A and BWhy not B: Option B uses the wrapper class Boolean (capital B), which is valid but not a primitive declaration. Option A alone demonstrates correct primitive boolean syntax.
- COption C onlyWhy not C: Java does not have a
boolkeyword; the primitive type isboolean. - DOption D onlyWhy not D: A String cannot be assigned to a boolean variable;
"7 > 3"is a String literal, not a boolean expression.
ExplanationIn Java, the primitive boolean type is spelled
boolean(lowercase).boolis not a Java keyword. A String literal like"7 > 3"is not a boolean expression and cannot be assigned to abooleanvariable. Option A correctly uses the primitive type with a relational expression.Key takeawayJava's primitive boolean type is lowercase `boolean`; `bool` does not exist in Java.
- A
- Question 5 · Easy
What is the output of the following code?
int x = 10; x += 3; x *= 2; x -= 7; System.out.println(x);- A19Correct
- B26Why not B: This results from applying x -= 7 before x = 2. The operations must be applied in order: 10+3=13, 132=26, 26-7=19.
- C23Why not C: Possibly from computing (10+3-7)*2=12. Compound assignment operators are applied sequentially, not combined.
- D6Why not D: Possibly from (10*2)+3-7=16 or another mis-ordering. Apply each statement in sequence: 10→13→26→19.
ExplanationTrace through each compound assignment in order:
x = 10, thenx += 3→x = 13, thenx *= 2→x = 26, thenx -= 7→x = 19.Key takeawayCompound assignment operators (`+=`, `*=`, `-=`) modify the variable in place; evaluate statements sequentially.
- A
- Question 6 · Easy
Consider the following code. What is the value of
z?int x = 7; int y = 3; double z = x / y + 0.5;- A2.833...Why not A: This would be the result if x/y were computed as double division (7.0/3 = 2.333), then plus 0.5. But both x and y are int, so integer division is used.
- B2.5Correct
- C2.0Why not C: Adding 0.5 to the result of x/y (which is 2) gives 2.5, not 2.0.
- D3.5Why not D: Integer division 7/3 = 2 (not 3). Then 2 + 0.5 = 2.5, not 3.5.
Explanationx / yis7 / 3— both ints — so integer division gives2. Then2 + 0.5promotes2to2.0and produces2.5. That double result is stored inz.Key takeawayOperator precedence: division executes before addition. Integer division truncates before the addition with a double occurs.
- A
- Question 7 · Easy
What is printed by the following code?
double d = 1.0 / 3.0; int i = (int)(d * 10); System.out.println(i);- A3Correct
- B4Why not B: Casting to int truncates, not rounds. 0.333... * 10 = 3.333..., which truncates to 3, not 4.
- C3.3Why not C: The cast
(int)converts the result to an integer, removing the decimal portion. - D0Why not D: 1.0/3.0 = 0.333..., and 0.333... * 10 = 3.333..., which is greater than 0 and truncates to 3.
Explanation1.0 / 3.0= approximately0.33333.... Multiplying by 10 gives3.3333.... Casting tointtruncates the decimal, yielding3.Key takeawayCombining double arithmetic with int casting is a common pattern; always remember casting truncates.
- A
- Question 8 · Easy
Which statement about Java primitive types is correct?
- AAn
intcan store fractional values such as 3.5.Why not A:intstores only whole numbers (integers). Usedoubleorfloatfor fractional values. - BA
booleancan be cast to anint.Why not B: Java does not allow casting betweenbooleanand any numeric type. This causes a compile error. - CAssigning a
doubleliteral to anintvariable without an explicit cast causes a compile error.Correct - DAn
intvariable is automatically promoted todoublewhen printed withSystem.out.println.Why not D:System.out.printlnprints the int as an integer. No automatic promotion to double occurs for printing.
ExplanationJava requires an explicit cast for narrowing conversions (double to int). Writing
int x = 3.14;causes a compile error because data could be lost. You must writeint x = (int) 3.14;. Widening (int to double) is automatic; narrowing is not.Key takeawayNarrowing primitive conversions (double to int) require an explicit cast; Java will not do them implicitly.
- A
- Question 9 · Medium
What is the output of the following code?
int a = 2; System.out.println(a++); System.out.println(a);- A2\n3Correct
- B3\n3Why not B: Post-increment (
a++) returns the original value ofabefore incrementing. The first print sees 2, then a becomes 3. - C2\n2Why not C:
a++does incrementa; the second print will show the incremented value 3. - D3\n4Why not D: Only one increment occurs. The first print uses the original value 2; after printing, a becomes 3. No second increment happens.
ExplanationPost-increment (
a++) returns the current value ofaand then increments it. Soprintln(a++)prints2(the current value), and after the statementabecomes3. The secondprintln(a)then prints3.Key takeawayPost-increment (`a++`) uses the value first, then increments. Pre-increment (`++a`) increments first, then uses the value.
- A
- Question 10 · Medium
What is the result of the following expression in Java?
int result = 3 + 4 * 2 - 10 / 4;- A8Correct
- B9Why not B: Possibly from computing 10/4 as 2.5 and rounding, but integer division gives 2. 3+8-2=9 only if division is done differently. Actually 3+8-2=9... recheck: result is 3+(4*2)-(10/4) = 3+8-2 = 9. Wait — A is 8 which may be incorrect. See explanation.
- C5Why not C: Possibly left-to-right without operator precedence: (3+4)*2-10/4 = 14-2=12, or some other mis-ordering.
- D12Why not D: Left-to-right evaluation without precedence: (3+4)*2=14, 14-10=4, 4/4=1, which also does not give 12.
ExplanationJava follows standard operator precedence:
*and/before+and-. Evaluate:4 * 2 = 8,10 / 4 = 2(integer division). Then left to right:3 + 8 - 2 = 9. The answer is 9.Key takeawayJava operator precedence: multiplication and division evaluate before addition and subtraction, just like standard math.
- A
- Question 11 · Medium
What is the output of the following code?
int x = Integer.MAX_VALUE; x = x + 1; System.out.println(x > 0);- AtrueWhy not A: Adding 1 to Integer.MAX_VALUE causes integer overflow; the result wraps to Integer.MIN_VALUE, which is negative.
- BfalseCorrect
- CCompile errorWhy not C: This is a runtime behavior, not a compile error. Java compiles the code but overflows silently at runtime.
- DRuntime exceptionWhy not D: Java does not throw an exception on integer overflow. The value silently wraps around from MAX_VALUE to MIN_VALUE.
ExplanationInteger.MAX_VALUEis 2^31 - 1 = 2,147,483,647. Adding 1 causes integer overflow, and the value wraps around toInteger.MIN_VALUE= -2,147,483,648. Since that is negative,x > 0isfalse.Key takeawayJava int overflow wraps silently — no exception is thrown. MAX_VALUE + 1 becomes MIN_VALUE.
- A
- Question 12 · Medium
After the following code executes, what is the value of
n?double d = 7.9; int n = (int) d % 3;- A1Correct
- B2Why not B: This would result from computing (int)(7.9 % 3) = (int)(1.9) = 1 — but the cast applies to d first (before %): (int)7.9 = 7, then 7 % 3 = 1.
- C0Why not C: 7 % 3 = 1 (remainder), not 0. 3 divides 7 twice with remainder 1.
- DCompile errorWhy not D: The expression is valid. The cast
(int)has higher precedence than%, so d is cast first, then modulus is applied.
ExplanationCast has higher precedence than
%. So(int) dis evaluated first:(int) 7.9 = 7. Then7 % 3 = 1(7 = 3*2 + 1). Result is 1.Key takeawayThe cast operator has higher precedence than arithmetic operators; it applies to the immediately following operand.
- A