Java-基础题目集-Chapter 2,3 Elementary Programming, Selections

一.单选

1.Math.pow(2, 3) returns _____(D)_____.

A.9

B.8

C.9.0

D.8.0

2.Are the following four statements equivalent?A

  number += 1;
  number = number + 1;
  number++;
  ++number;

A.Yes

B.No

3.Analyze the following code fragments that assign a boolean value to the variable even.(E

Code 1:

if (number % 2 == 0)
  even = true;
else
  even = false;

Code 2:

even = (number % 2 == 0) ? true: false;

Code 3:

even = number % 2 == 0;

A.Code 2 has a compile error, because you cannot have true and false literals in the conditional expression.

B.Code 3 has a compile error, because you attempt to assign number to even.

C.All three are correct, but Code 1 is preferred.

D.All three are correct, but Code 2 is preferred.

E.All three are correct, but Code 3 is preferred.

4.What is 1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 == 0.5?C

A.true

B.false

C.There is no guarantee that 1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 == 0.5 is true.

解析:由于表达式涉及浮点数,并且浮点数是近似的,浮点数不能直接使用==进行比较

5.Suppose x=10 and y=10. What is x after evaluating the expression (y > 10) && (x-- > 10)?(B

A.9

B.10

C.11

6.Suppose x=10 and y=10. What is x after evaluating the expression (y > 10) && (x++ > 10).(B

A.9

B.10

C.11

解析:&&是短路操作符,

为什么被称为短路运算符?

这是因为一旦可以确定结果,解析就会停止。解析的顺序是从左至右,所以后边运算不执行,因此被形象的称作短路

二.多选

1.Which of the following assignment statements is incorrect?(CD

A.i = j = k = 1;

B.i = 1; j = 1; k = 1;

C.i = 1 = j = 1 = k = 1;

D.i == j == k == 1;

2.Which of the following are so called short-circuit operators?(AC

A.&&

B.&

C.||

D.|

解析:以下是所谓的短路操作符:&& ,||

3.What is the possible output from System.out.println((Math.random() * 4))?(ABCD

A.0

B.1

C.2

D.3

E.4

解析:Math.random() 函数返回一个浮点数,伪随机数在范围从0 到小于1,也就是说,从 0(包括 0)往上,但是不包括 1(排除 1),然后您可以缩放到所需的范围。实现将初始种子选择到随机数生成算法;它不能被用户选择或重置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xxx_xiyuyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值