25.0k views
1 vote
Which of the following expressions evaluates to 3.5?

I. (double) 2 / 4 + 3
II. (double) ( 2 / 4 ) + 3
III. (double) ( 2 / 4 + 3 )
A. I only
B. II only
C. I and II only
D. II and III only
E. I, II, and III

User Malgaur
by
7.6k points

1 Answer

2 votes

Answer:

A. I only

Step-by-step explanation:

To determine which of the expressions evaluates to 3.5, let's analyze each option:

I. `(double) 2 / 4 + 3`

First, `(double) 2` converts the integer 2 to a double, resulting in 2.0. Then, `2.0 / 4` performs division and evaluates to 0.5. Finally, adding 3 to 0.5 gives 3.5. So, option I evaluates to 3.5.

II. `(double) (2 / 4) + 3`

Here, `2 / 4` performs integer division, resulting in 0. The `(double) 0` conversion to double yields 0.0. Adding 3 to 0.0 gives 3.0. Therefore, option II evaluates to 3.0 and not 3.5.

III. `(double) (2 / 4 + 3)`

Inside the parentheses, `2 / 4 + 3` performs arithmetic operations. `2 / 4` evaluates to 0, and adding 3 gives 3. So, `(double) (2 / 4 + 3)` is equivalent to `(double) 3`. The conversion to double doesn't change the value, so this expression evaluates to 3.0 and not 3.5.

Based on the analysis, the only expression that evaluates to 3.5 is option I. Therefore, the correct answer is:

A. I only

User Lloyd McKenzie
by
8.4k points