一.单选
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),然后您可以缩放到所需的范围。实现将初始种子选择到随机数生成算法;它不能被用户选择或重置。