AP CSA Primitives & Casting Practice Test
AP CSA Practice Test: Primitives, Casting & Expressions
Unit 1: Using Objects | 10 Questions | AP Computer Science A
What is the value of result after the following code executes?
Integer division: 17 / 5 = 3 (truncates decimal). Modulus: 17 % 5 = 2 (remainder). So result = 3 + 2 = 5.
Which of the following expressions evaluates to 2.5?
5 / 2
(double) 5 / 2
(double) (5 / 2)
5 / 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.
What is printed by the following code?
x++ uses current value (5), then increments x to 6. ++x increments x to 7, then uses that value. So: 5 + 7 = 12.
Consider the following code segment. Which statement about the error is correct?
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.
What is the value of n after this code runs?
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.
Consider the following statements about Java primitive types. Which are TRUE?
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.
What value is stored in result?
Follow operator precedence: * and / before + and -. So: 4*2=8, 8/4=2. Then left to right: 3 + 8 - 2 + 1 = 10.
The following code is intended to calculate 20% of a price. What is the problem?
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.
What is printed?
1 2.33
2 1
1 2
1.0 2.0
7 % 3 = 1 (remainder when 7 divided by 3). 7 / 3 = 2 (integer division). Output: "1 2".
Which line contains an error that will NOT compile?
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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]