AP计算机教程4-5:简单多选题
0:00
What does the following code print when x
has been set to 187
?
if (x < 0) System.out.println("x is negative"); else if (x == 0) System.out.println("x is zero"); else System.out.println("x is positive");
两个
if
的条件均不成立,最终执行最后else
的分支。3
What is printed when the following code executes and x
equals 4
and y
equals 3
?
if (!(x < 3 || y > 2)) System.out.println("first case"); else System.out.println("second case");
等价于
x >=3 && y <= 2
,其值为false
。2
What is the value of grade
when the following code executes and score is 80
?
if (score >= 90) grade = "A"; if (score >= 80) grade = "B"; if (score >= 70) grade = "C"; if (score >= 60) grade = "D"; else grade = "E";
每个
if
语句事实上都会被执行,而grade
的最终结果是最后一个if
所决定的。4
What is printed when the following code executes and x
has been set to 0
and y
is set to 3
?
if (x > 0 || (y / x) == 3) System.out.println("first case"); else System.out.println("second case");
对于逻辑或而言,如果第一个表达式为
false
,还需判定第二个表达式的结果,在过程中会抛出除零的异常。3
0 条评论