AP Computer Science A · Topic 1.5
Casting and Ranges of Variables Practice
Part of Primitive Types.
Practice questions
24
Sample questions
5 of 24 — sign in to practice the rest with adaptive difficulty and mastery tracking.
Sample 1difficulty 3/5
Which expression yields <code>2.5</code> when <code>int a = 5;</code>?
- A
a / 2.0 / 2
- B
(double) (a / 2)
- C
a / 2
- Dcheck_circle
(double) a / 2
Why
<code>(double) a / 2</code> casts first, then divides → 5.0 / 2 = 2.5. <code>(double)(a/2)</code> casts after the int division → 2.0.
- A
Sample 2difficulty 3/5
int a = 9; int b = 4; double r = (double) a / b; System.out.println(r);What is printed?
- A
2
- B
2.0
- Ccheck_circle
2.25
- D
2.5
Why
Casting a to double before division forces double division, so 9.0 / 4 = 2.25.
- A
Sample 3difficulty 3/5
<code>int a = 7; double b = a / 2;</code> — <code>b</code> equals
- A
3
- Bcheck_circle
3.0
- C
Compile error
- D
3.5
Why
Right side first: int division a/2 = 3; assigned to double → 3.0.
- A
Sample 4difficulty 3/5
What is the value of <code>7.0 / 2</code>?
- Acheck_circle
3.5
- B
3
- C
Compiler error
- D
4
Why
A <code>double</code> operand promotes the other to <code>double</code>; 7.0 / 2 = 3.5.
- A
Sample 5difficulty 3/5
int a = 9; int b = 4; double r = (double)(a / b); System.out.println(r);What is printed?
- Acheck_circle
2.0
- B
2.25
- C
2
- D
2.5
Why
The cast applies after the int division a / b which is 2; converting 2 to double gives 2.0.
- A