AP计算机教程4-6:中等多选题
0:00
Which of the following expressions is equivalent to !(c || d)
?
直接应用德摩根定律。
1
Which of the following is equivalent to the code segment below?
if (x > 2) x = x * 2; if (x > 4) x = 0;
注意当
x
变为自身的两倍以后,两个if
语句条件是等价的。在都会被执行的前提下,x
的最终结果是最后一个if
所决定的。3
Which of the following is equivalent to the code segment below?
if (x > 0) x = -x; if (x < 0) x = 0;
无论
x
是正数、负数还是零,最终结果都会被清零。1
At a certain high school students receive letter grades based on the following scale.
Integer Score | Letter Grade |
---|---|
93 or above | A |
From 84 to 92 inclusive | B |
From 75 to 83 inclusive | C |
Below 75 | F |
Which of the following code segments will assign the correct string to grade
for a given integer score
?
I.
if (score >= 93) grade = "A"; if (score >= 84 && score <= 92) grade = "B"; if (score >= 75 && score <= 83) grade = "C"; if (score < 75) grade = "F";
II.
if (score >= 93) grade = "A"; if (score >= 84) grade = "B"; if (score >=75) grade = "C"; if (score < 75) grade = "F";
III.
if (score >= 93) grade = "A"; else if (score >= 84) grade = "B"; else if (score >= 75) grade = "C"; else grade = "F";
II是错的,对于高分后面的
if
也同样成立,会把grade
给覆盖掉。注意到III的else if
隐式的包含了上一步条件的否定。1
0 条评论