AP CSA Primitives & Casting Practice Test

AP CSA Practice Test: Primitives, Casting & Expressions

Unit 1: Using Objects | 10 Questions | AP Computer Science A

Question 1

What is the value of result after the following code executes?

int a = 17; int b = 5; int result = a / b + a % b;
A3
B4
C5
D6

Integer division: 17 / 5 = 3 (truncates decimal). Modulus: 17 % 5 = 2 (remainder). So result = 3 + 2 = 5.

Question 2

Which of the following expressions evaluates to 2.5?

A5 / 2
B(double) 5 / 2
C(double) (5 / 2)
D5 / 2.0 + 0 cast to int

A: 5/2 = 2 (integer division). B: Cast 5 to double first, then 5.0/2 = 2.5. Correct! C: 5/2 = 2 first, then cast to 2.0. D: 5/2.0 = 2.5 but casting to int gives 2.

Question 3

What is printed by the following code?

int x = 5; System.out.println(x++ + ++x);
A10
B11
C13
D12

x++ uses current value (5), then increments x to 6. ++x increments x to 7, then uses that value. So: 5 + 7 = 12.

Question 4

Consider the following code segment. Which statement about the error is correct?

double price = 19.99; int dollars = price;
ACompile error: possible lossy conversion from double to int
BRuntime error: cannot convert double to int
CNo error; dollars equals 19
DNo error; dollars equals 20

Java does not allow implicit narrowing conversions. Assigning a double to an int without an explicit cast causes a compile-time error. You must write int dollars = (int) price; to compile.

Question 5

What is the value of n after this code runs?

int n = 10; n += n-- - --n;
A10
B11
C12
D8

Compound assignment evaluates left side first: n=10. Then n-- returns 10, n becomes 9. Then --n decrements to 8 and returns 8. Expression: 10 - 8 = 2. Finally: n = 10 + 2 = 12.

Question 6

Consider the following statements about Java primitive types. Which are TRUE?

I. An int can store any value a short can store. II. A float can store any value an int can store exactly. III. A long can store any value an int can store.
AI only
BI and III only
CI, II, and III
DII and III only

I: True - int (32-bit) can hold all short (16-bit) values. II: False - float has only 24 bits of precision, so large int values (beyond 2^24) lose precision. III: True - long (64-bit) can hold all int (32-bit) values exactly.

Question 7

What value is stored in result?

int result = 3 + 4 * 2 - 8 / 4 + 1;
A4
B6
C8
D10

Follow operator precedence: * and / before + and -. So: 4*2=8, 8/4=2. Then left to right: 3 + 8 - 2 + 1 = 10.

Question 8

The following code is intended to calculate 20% of a price. What is the problem?

int price = 50; double discount = price * 20 / 100;
AInteger division causes discount to be 10.0 instead of 10.0 (no issue for this input, but fails for price=55)
BCompile error: cannot multiply int by int
CRuntime error: division by zero
DThere is no problem; the code works correctly for all prices

The expression price * 20 / 100 uses integer arithmetic throughout. For price=50: 50*20=1000, 1000/100=10, which happens to be correct. But for price=54: 54*20=1080, 1080/100=10 (should be 10.8). Integer division truncates the decimal. Fix: use price * 20.0 / 100 or price * 0.20.

Question 9

What is printed?

int a = 7, b = 3; System.out.println(a % b + " " + a / b);
A1 2.33
B2 1
C1 2
D1.0 2.0

7 % 3 = 1 (remainder when 7 divided by 3). 7 / 3 = 2 (integer division). Output: "1 2".

Question 10

Which line contains an error that will NOT compile?

int x = 5; // Line 1 double y = x; // Line 2 int z = y; // Line 3 int w = (int) y; // Line 4
ALine 2
BLine 3
CLine 4
DAll lines compile successfully

Line 1: Valid int declaration. Line 2: Valid - int to double is widening (automatic). Line 3: Error - double to int is narrowing and requires explicit cast. Line 4: Valid - explicit cast provided.

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]