AP CSA Practice Test: Primitives and Casting

AP CSA Practice Test: Primitives and Casting

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

Question 1
What is the value of result after the following code executes?
int a = 7;
int b = 2;
double result = a / b;
A3.5
B3.0
C3
D4.0

Explanation: Since a and b are both int, the division 7 / 2 performs integer division, producing 3. This integer 3 is then assigned to a double, becoming 3.0. To get 3.5, you would need to cast first: (double) a / b.

Common Mistake: Choosing 3.5 because the result is stored in a double. The cast to double happens AFTER the integer division.

Question 2
Consider the following code segment. What is printed?
int x = 13;
int y = 5;
System.out.println(x % y + x / y);
A5
B4
C5.6
D6

Explanation: x % y = 13 % 5 = 3 (remainder). x / y = 13 / 5 = 2 (integer division). So 3 + 2 = 5.

Common Mistake: Forgetting that / with two ints truncates. 13/5 is 2, not 2.6.

Question 3
Which of the following expressions evaluates to 7.5?

I. (double)(15 / 2)
II. (double) 15 / 2
III. 15.0 / 2
AI and II only
BII and III only
CI and III only
DI, II, and III

Explanation: I: 15 / 2 does integer division first (= 7), then casts to 7.0, not 7.5. II: casts 15 to 15.0 first, then divides by 2 = 7.5. III: 15.0 is already a double, so 15.0 / 2 = 7.5. Only II and III produce 7.5.

Common Mistake: Thinking (double)(15/2) casts before dividing. Parentheses force the integer division first.

Question 4
What is the output of the following code?
double d = 9.7;
int n = (int) d;
System.out.println(n);
A10
B9
C9.7
DCompile error

Explanation: Casting a double to an int truncates (drops the decimal), it does NOT round. (int) 9.7 = 9.

Common Mistake: Choosing 10 by rounding. Java casting always truncates toward zero.

Question 5
Which line of code will cause a compile-time error?
Line 1: int a = 5;
Line 2: double b = a;
Line 3: int c = 3.14;
Line 4: double d = 10;
ALine 1
BLine 2
CLine 3
DLine 4

Explanation: Line 3 attempts to assign a double literal (3.14) to an int without an explicit cast. Java does not allow narrowing conversions implicitly. Line 2 and Line 4 are fine because widening from int to double is automatic.

Common Mistake: Thinking Line 4 is an error. Assigning an int to a double (widening) is always allowed.

Question 6
What is the value of z after this code runs?
int x = 5;
int y = 3;
int z = x + y * 2 - x % y;
A9
B14
C11
D13

Explanation: Order of operations: y * 2 = 6, x % y = 5 % 3 = 2. Then: 5 + 6 - 2 = 9. Multiplication and modulus have higher precedence than addition and subtraction.

Common Mistake: Evaluating left-to-right without respecting operator precedence.

Question 7
What does the following code print?
System.out.println(2 + 3 + "hello" + 4 + 5);
A"5hello45"
B"5hello9"
C"23hello45"
D"14"

Explanation: Java evaluates left-to-right. 2 + 3 = 5 (both ints). Then 5 + "hello" = "5hello" (now a String). Then "5hello" + 4 = "5hello4". Then "5hello4" + 5 = "5hello45".

Common Mistake: Thinking all numbers are added first or that 4+5 evaluates before concatenation.

Question 8
Consider the following statements about Java primitive types.

I. A boolean variable can store the value 0 or 1.
II. An int variable can store a decimal value like 3.14.
III. A double variable can store an integer value like 5.
AI only
BIII only
CI and III
DNone of the above

Explanation: I is false: boolean stores only true or false, not 0/1 (unlike C/C++). II is false: int cannot hold decimals. III is true: a double can hold 5 (it becomes 5.0 via widening).

Common Mistake: Confusing Java with other languages where booleans can be 0/1.

Question 9
What is the output?
int a = Integer.MAX_VALUE;
System.out.println(a + 1);
A2147483648
BA compile-time error
CA runtime exception
DA large negative number

Explanation: Integer overflow wraps around. Integer.MAX_VALUE is 2147483647. Adding 1 causes overflow, producing -2147483648 (Integer.MIN_VALUE). Java does not throw an error for integer overflow.

Common Mistake: Expecting an error. Java silently overflows integers without any warning.

Question 10
What is the value of result?
double result = 1 / 2 + 3 / 4 + 0.25;
A1.0
B0.25
C1.25
D0.5

Explanation: 1 / 2 = 0 (integer division). 3 / 4 = 0 (integer division). 0 + 0 + 0.25 = 0.25. Both divisions involve only ints, so they truncate before the double addition.

Common Mistake: Assuming the double on the left side forces all divisions to use decimal math.

0% 0/10

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]