帮忙解答这道题

来源:百度知道 编辑:UC知道 时间:2024/05/13 15:59:35
Given that x is a float variable and num is an int variable containing the value 38, what will x contain after execution of the following statement:

x = num / 4 + 3.0;

a. 12.5

b. 13

c. 12

d. 12.0

e. nothing; a compile-time error occurs

答案是D,因为 num=(int)38, 虽然38÷4=9.5,但是(int)9.5就是9了。也就是说num/4实际上是得到9,9+3.0就是12.0了。 D正解。

个人觉得是d

num是一个整数变量,所以num/4将得到整数结果,在赋值给float类型变量X时,需要从整形转化为float,所以选d!