期中测验
Midterm Exam · 第 1–5 章 · 40 道选择题。下面是 2026-10-28 那次交卷的题目和正确答案,得分 97.5 / 100。唯一错题是第 15 题。
用时 23 分钟。交卷时间 Oct 28, 10:34pm。
The world population is over 7 billion. Which declaration uses the fewest bits while guaranteeing that worldPopulation can be assigned the value 7 billion without error?
- int worldPopulation;
- long worldPopulation;
- 正确答案long long worldPopulation;
- long long long worldPopulation;
7 billion 超过 32 位有符号整数的上限。long long 至少 64 位,是能保证装下的最短合法类型。long long long 不是合法类型。
Which expression correctly evaluates 13 < num < 30?
- (num < 13) && (num < 30)
- (num < 13) || (num < 30)
- 正确答案(num > 13) && (num < 30)
- (num > 13) || (num < 30)
13 < num < 30 表示大于 13 并且小于 30,两端都不包含,用 && 把两个比较接起来。
Select the statement XXX that will correctly swap the value in smallArray[0] with smallArray[2].
int smallArray[] = {5, 12, 16};
XXX
smallArray[0] = smallArray[2];
smallArray[2] = smallArray[0];
smallArray[2] = smallArray[0];
smallArray[0] = smallArray[2];
- 正确答案
temp = smallArray[0];
smallArray[0] = smallArray[2];
smallArray[2] = temp;
temp = smallArray[2];
smallArray[0] = smallArray[2];
smallArray[2] = temp;
先把 smallArray[0] 的 5 存进 temp,再把 16 放进 [0],最后把 5 放回 [2]。从 temp 存 [2] 会把两个位置都写成 16。
Which statement appends an exclamation point at the end of myStr?
char myStr[10] = "Yay";
- strncpy(myStr, "!", 2);
- strcpy(myStr, "!");
- strcat(myStr, '!');
- 正确答案strcat(myStr, "!");
strcat 把字符串接在后面。第二个参数要写 "!"。单引号的 '!' 是一个字符,strcpy 会把原来的 Yay 覆盖掉。
What is the ending value of y?
int x;
int y;
x = 2;
y = ((x + 1) / 5) + (x / 2) ;
- 正确答案1
- 1.6
- 4.5
- Error: Division not possible
x 和 y 都是 int。 (2 + 1) / 5 是 0,2 / 2 是 1,y 是 1。1.6 是把除法当成浮点才会得到的结果。
What is the ending value of x?
x = 160 / 20 / 4;
- 正确答案2
- 16
- 32
- No value; runtime error
/ 从左往右结合。(160 / 20) / 4 先得到 8,再得到 2。
What is output, if the input is 3 2 1 0?
scanf("%d", &x);
while (x > 0) {
printf("%d ", 2 * x);
}
- 6
- 6 4 2
- 6 4 2 0
- 正确答案6 6 6 6 6 ... (infinite loop)
scanf 只读一次,x 停在 3。循环里只印 2 * x,没有再读入,也没有改 x,所以一直印 6。
If the input is 5, what is the output?
int x;
int z = 0;
scanf("%d", &x);
if(x > 9) {
z = 3;
}
z = z + 1;
printf("%d",z);
- 0
- 正确答案1
- 3
- 4
输入 5,5 > 9 为假,z 保持 0。z = z + 1 写在 if 外面,所以印 1。
Given two arrays, which code will output all the arrays' elements, in the order key, item followed by a newline?
int keysList[SIZE_LIST];
int itemsList[SIZE_LIST];
- 正确答案
for (i = 0; i < SIZE_LIST; ++i) {
printf("%d, %d \n", keysList[i], itemsList[i]);
}
- printf("%d, %d \n", keysList[SIZE_LIST - 1], itemsList[SIZE_LIST - 1]);
- printf("%d, %d \n", keysList[SIZE_LIST], itemsList[SIZE_LIST]);
for (i = 0; i < SIZE_LIST - 1; ++i) {
printf("%d, %d \n", keysList[i], itemsList[i]);
}
i 从 0 走到 SIZE_LIST - 1,每一行先印 key 再印 item,然后换行。只写下标 SIZE_LIST - 1 会漏掉前面的元素,写下标 SIZE_LIST 会越界。
What is the output?
int num = 10;
while (num <= 15) {
printf("%d ", num);
if (num == 12) {
break;
}
++num;
}
printf("Done");
- 10 Done
- 10 11 Done
- 正确答案10 11 12 Done
- 10 11 12 13 14 15 Done
先印再判断。num 为 10、11、12 时都会先印出来,印完 12 才 break,然后印 Done。
Which identifier name shows good practice?
- m_v
- revPhyMgt
- cost_of_shipping_furniture_crates_by_sea
- 正确答案sizeOfBox
sizeOfBox 能看懂,长度也合适。m_v 和 revPhyMgt 看不出意思,最长的那个名字太长。
What is the ending value of x?
int y = 6;
int x = (4 / 2) * y + 8;
- 8
- 正确答案20
- 28
- 32
括号先算,4 / 2 是 2。再乘 y:2 * 6 是 12。最后加 8,得到 20。
Which statement correctly assigns 12 into the fifth row and third column of array itemArray?
- numVals[5][3] = 12;
- numVals[3][5] = 12;
- 正确答案numVals[4][2] = 12;
- numVals[2][4] = 12;
行和列的下标都从 0 开始。第 5 行是 [4],第 3 列是 [2]。
A double variable stores a value in 64 bits, using some bits for mantissa, some for exponent, and one for sign. Which is true about a double variable?
- 正确答案Limited bits may cause some rounding of the mantissa.
- If a value is too large for a double, a float should be used instead.
- If a value is too large for a double, a long should be used instead.
- The compiler may sometimes use 63 bits for the mantissa.
double 的尾数只有有限位,有些小数存不精确,会被舍入。float 比 double 更短,装不下 double 的数时不能改用 float。
Which is true for the following code?
char userText[10];
userText[0] = 'B';
userText[1] = 'o';
userText[2] = 'o';
userText[3] = 'k';
userText[4] = '\0';
...
userText[3] = 't';
- 交卷时选了这项The first four characters of the array userText are Book.
- The first five characters of the array userText are Bookt.
- The compiler generates an error because element 3 can't be overwritten.
- 正确答案Printing the array userText will work fine because the new string is 4 characters and the array size is 10.
userText[3] 从 k 改成 t 之后,字符串是 Boot,结尾的 '\0' 还在下标 4。四个字符加结束符,数组长度 10 够用,用 %s 打印会停在 '\0'。交卷时选了“前四个字符是 Book”,那一项是错的。
Which XXX and YYY will loop as long as the input is an integer less than 100? Choices are in the form XXX / YYY.
scanf("%d", &w);
while (XXX) {
// Do something
YYY;
}
- 正确答案w < 100 / scanf("%d", &w)
- w >= 100 / (nothing)
- w < 100 / (nothing)
- w >= 100 / scanf("%d", &w)
循环条件是 w < 100。循环里面要再 scanf 下一个整数,否则 w 不变,第一次小于 100 就会一直转。
Given a vector/array with values 5, 10, 15, 20, 25, what are the fewest number of swaps needed to reverse the list?
- 正确答案2
- 3
- 4
- 5
五个数反转成 25 20 15 10 5。交换第 1 个和第 5 个,再交换第 2 个和第 4 个。中间的 15 不用动,一共 2 次。
Which operator is evaluated first: x + y < y - z * 2 ?
- +
- <
- -
- 正确答案*
* 比 +、- 先算,比较 < 最后算。这条式子里最先算的是 z * 2。
Which generates a random integer in the range 13..19?
- rand() % 19
- rand() % (19 - 13)
- (rand() % (19 - 13)) + 13
- 正确答案(rand() % (19 - 13 + 1)) + 13
闭区间的个数是上界减下界再加 1。(rand() % 7) + 13 得到 13 到 19。少写 + 1 时最大只到 18。
Given that integer array x has elements 5, 10, 15, 20, what is the output?
int i;
for (i = 0; i < 4; ++i) {
printf("%d ", x[i] + x[i + 1]);
}
- 10, 15, 20, 5
- 15, 25, 35, 20
- 15, 25, 35, 25
- 正确答案Error: Invalid access
数组只有下标 0 到 3。i 走到 3 时要读 x[4],越界。
What is the purpose of a variable?
- To assign values
- To perform calculations
- 正确答案To hold a value
- To hold a constant value
变量是一块用来存放数值的内存,里面的值可以改。一直不变的值用常量。
Which statement, executed once at the start of main(), enables a program to generate a different sequence of pseudo-random numbers from rand() each time the program is run?
- srand();
- srand(100);
- time(srand());
- 正确答案srand((int)time(0));
srand((int)time(0)) 用当前时间做种子,每次运行的随机序列才不一样。种子写成固定的 100,每次结果都相同。
Which statement declares a two-dimensional integer array called myArray with 3 rows and 4 columns?
- int myArray[3, 4];
- int myArray[7];
- 正确答案int myArray[3][4];
- int myArray[4][3];
二维数组先写行数,再写列数:int myArray[3][4]。方括号里写逗号不是这种声明。
What will a compiler do for the following code?
/*
numItems = 2; /* Total items to buy */
rate = 0.5;
*/
- Ignore numItems = 2, but generate code for rate = 0.5.
- Generate code for numItems = 2, but ignore rate = 0.5.
- Ignore both statements.
- 正确答案Generate an error.
C 的块注释不能嵌套。第一个 */ 就把注释结束了,最后多出来的 */ 会让编译报错。
Why should programmers avoid making the following comparison with double x?
x == 0.3
- 正确答案Because 0.3 may not be exactly represented as 0.3
- Because doubles should not be compared with integers
- Because variables should not be compared with literals
- Because the 0.3 should come first: 0.3 == x
0.3 在 double 里常常存不成精确的 0.3,用 == 比较可能得到假。
What is the output?
int n;
for (n = 0; n < 10; n = n + 3) {
printf("%d ", n);
}
- 0 1 2 3 4 5 6 7 8 9
- 1 4 7 10
- 正确答案0 3 6 9
- 0 3 6 9 12
n 从 0 开始,每次加 3,条件是 n < 10。会印 0 3 6 9。n 变成 12 时循环已经停了。
For the given pseudocode, which XXX and YYY will output the sum of the input integers (stopping when -1 is input)? Choices are in the form XXX / YYY.
val = Get next input
XXX
While val is not -1
YYY
val = Get next input
Put sum to output
- sum = val / sum = val
- sum = val / sum = sum + val
- 正确答案sum = 0 / sum = sum + val
- sum = 0 / sum = val
循环前把 sum 设成 0。循环里先把当前的 val 加进去,再读下一个数。读到 -1 时循环停住,-1 不会被加进去。
Which declares two related integer arrays named personName and personAge each with 50 elements?
- int personName, personAge;
- int personName, personAge = 50;
int personName = 50;
int personAge = 50;
- 正确答案
int personName[50];
int personAge[50];
两个相关数组都要带上长度:personName[50] 和 personAge[50]。写成 = 50 只是一个整数。
Given two arrays, studentNames that maintains a list of students, and studentScores that has a list of the scores for each student, which XXX and YYY prints out only the student names whose score is above 80? Choices are in the form XXX / YYY.
char studentNames[NUM_STUDENTS];
int studentScores[NUM_STUDENTS];
int i;
for (i = 0; i < NUM_STUDENTS; ++i) {
if (XXX > 80) {
printf("%s \n", YYY );
}
}
- studentNames[i] / studentNames[i]
- studentNames[i] / studentScores[i]
- 正确答案studentScores[i] / studentNames[i]
- studentScores[i] / studentScores[i]
用 studentScores[i] 和 80 比较,成立时印 studentNames[i]。
Why might style guidelines limit the number of characters per line?
- To speed up the code’s execution
- To limit the size of the executable file
- To force use of short variable names
- 正确答案To make the code more readable
限制每行字符数是为了好读。它不会让程序跑得更快,也不会缩小可执行文件。
Which expression can be used to decide if x is not between 10 and 20?
- 正确答案!(10 < x && x < 20)
- !(x < 10 && x < 20)
- !(x < 10 || x < 20)
- !(x > 10 || x < 20)
“在 10 和 20 之间”写成 10 < x && x < 20。外面加 !,就是不在这个开区间里。
Given that integer array x has elements 4, 7, 3, 0, 8, what are the elements after the loop?
int i;
for (i = 0; i < 4; ++i) {
x[i] = x[i + 1];
}
- 4, 4, 7, 3, 0
- 正确答案7, 3, 0, 8, 8
- 7, 3, 0, 8, 4
- Error: Invalid access
每次把右边那一格抄到左边。i 只走到 3,读的是还没改过的下一格,最后一格 8 留在原地,结果是 7, 3, 0, 8, 8。
Which expression fails to compute the area of a triangle having base b and height h (area is one-half base time height)?
- (1.0 / 2.0 ) * b * h
- 正确答案(1 / 2) * b * h
- (b * h) / 2.0
- 0.5 * b * h
1 / 2 是两个 int 相除,结果是 0,面积变成 0。另外三项里有小数,除法会按浮点来算。
Given x = 1, y = 2, and z = 3, how is the expression evaluated? In the choices, items in parentheses are evaluated first.
(x == 5) || (y == 2) && (z == 5)
- 正确答案false OR (true AND false) --> false OR false --> false
- false OR (true AND false) --> false OR true --> true
- (false OR true) AND false --> true AND false --> false
- (false OR true) AND false --> true AND false --> true
&& 比 || 先结合。1 == 5 为假,2 == 2 为真,3 == 5 为假。真并且假得到假,假或者假还是假。
For the given pseudocode, which XXX and YYY will output the smallest non-negative input (stopping when a negative value is input)? Choices are in the form XXX / YYY.
min = 0
val = Get next input
min = val
While val is not negative
If XXX
YYY
val = Get next input
Put min to output
- val > 0 / min = val
- min < val / val = min
- val > min / val = min
- 正确答案val < min / min = val
新读到的非负数比 min 更小,才把 min 换成它。负数会让 while 停住,不会写进 min。
A programmer compares x == y, where x and y are doubles. Many different values are expected for x and y. For values that a programmer expects to be equal, the comparison will _____ .
- always evaluate to true
- always evaluate to false
- 正确答案sometimes evaluate to true, sometimes to false, depending on the values
- sometimes immediately exit the program, for values that are slightly different
两个 double 用 == 比较时,能精确表示的值会得到真,有舍入误差的值会得到假。
In the following code, which conditions will lead to the booleans continue and needFill to both be true?
halfFull = (currentTank > fullTank * .5);
full = (currentTank == fullTank);
empty = (currentTank == 0);
almostEmpty = (currentTank < fullTank * .25)
if (full || halfFull) {
continue= true;
needFill = false;
}
else if (almostEmpty && !Empty ) {
continue = true;
needFill = true;
}
else {
continue = false;
needfill = true;
}
- 正确答案almostEmpty == true && empty == false
- full == true || halfFull == true
- almostEmpty == true || empty == false
- almostEmpty == true && halfFull == true
只有 else if (almostEmpty && !empty) 这一支把 continue 和 needFill 都设成真。油箱满或者过半时,needFill 是假。
Which expression evaluates to 3.5?
int x = 7;
int y = 1;
- x * (1 / 2)
- 正确答案(x / 2.0)
- x.0 / 2
- (x / 2) * 1.0
7 / 2.0 是浮点除法,得到 3.5。(x / 2) 和 (1 / 2) 都是整数除法,小数会被丢掉。
Which expressions for XXX, YYY, and ZZZ output "Great job" for scores above 90, and "Nice try" otherwise? Choices are in the form XXX / YYY / ZZZ.
int score;
scanf("%d", &score);
if (XXX) {
printf(YYY);
}
else {
printf(ZZZ);
}
- score > 90 / "Nice try" / "Great job"
- score < 91 / "Great job" / "Nice try"
- 正确答案score < 91 / "Nice try" / "Great job"
- (Not possible for given code)
分数是整数时,score < 91 就是不超过 90,走 if 印 Nice try。91 以及更高走 else,印 Great job。
What code for XXX, YYY correctly swaps a and b? Choices are written as XXX / YYY.
XXX;
a = b;
YYY;
b = tmp;
- 正确答案tmp = a / (nothing)
- tmp = a / tmp = b
- tmp = b / (nothing)
- (nothing) / tmp = a
先把 a 存进 tmp,再执行 a = b,最后 b = tmp。中间那一行不用再赋值。