haiyi 的赛博食粮仓

期中测验

Midterm Exam · 第 1–5 章 · 40 道选择题。下面是 2026-10-28 那次交卷的题目和正确答案,得分 97.5 / 100。唯一错题是第 15 题。

用时 23 分钟。交卷时间 Oct 28, 10:34pm。

12345678910111213141516171819202122232425262728293031323334353637383940
问题 12.5 / 2.5 pts

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?

  1. int worldPopulation;
  2. long worldPopulation;
  3. 正确答案long long worldPopulation;
  4. long long long worldPopulation;

7 billion 超过 32 位有符号整数的上限。long long 至少 64 位,是能保证装下的最短合法类型。long long long 不是合法类型。

问题 22.5 / 2.5 pts

Which expression correctly evaluates 13 < num < 30?

  1. (num < 13) && (num < 30)
  2. (num < 13) || (num < 30)
  3. 正确答案(num > 13) && (num < 30)
  4. (num > 13) || (num < 30)

13 < num < 30 表示大于 13 并且小于 30,两端都不包含,用 && 把两个比较接起来。

问题 32.5 / 2.5 pts

Select the statement XXX that will correctly swap the value in smallArray[0] with smallArray[2].

int smallArray[] = {5, 12, 16};
XXX
  1. smallArray[0] = smallArray[2];
    smallArray[2] = smallArray[0];
  2. smallArray[2] = smallArray[0];
    smallArray[0] = smallArray[2];
  3. 正确答案
    temp = smallArray[0];
    smallArray[0] = smallArray[2];
    smallArray[2] = temp;
  4. temp = smallArray[2];
    smallArray[0] = smallArray[2];
    smallArray[2] = temp;

先把 smallArray[0] 的 5 存进 temp,再把 16 放进 [0],最后把 5 放回 [2]。从 temp 存 [2] 会把两个位置都写成 16。

问题 42.5 / 2.5 pts

Which statement appends an exclamation point at the end of myStr?

char myStr[10] = "Yay";
  1. strncpy(myStr, "!", 2);
  2. strcpy(myStr, "!");
  3. strcat(myStr, '!');
  4. 正确答案strcat(myStr, "!");

strcat 把字符串接在后面。第二个参数要写 "!"。单引号的 '!' 是一个字符,strcpy 会把原来的 Yay 覆盖掉。

问题 52.5 / 2.5 pts

What is the ending value of y?

int x;
int y;
x = 2;
y = ((x + 1) / 5) + (x / 2) ;
  1. 正确答案1
  2. 1.6
  3. 4.5
  4. Error: Division not possible

x 和 y 都是 int。 (2 + 1) / 5 是 0,2 / 2 是 1,y 是 1。1.6 是把除法当成浮点才会得到的结果。

问题 62.5 / 2.5 pts

What is the ending value of x?

x = 160 / 20 / 4;
  1. 正确答案2
  2. 16
  3. 32
  4. No value; runtime error

/ 从左往右结合。(160 / 20) / 4 先得到 8,再得到 2。

问题 72.5 / 2.5 pts

What is output, if the input is 3 2 1 0?

scanf("%d", &x);
while (x > 0) {
   printf("%d ", 2 * x);
}
  1. 6
  2. 6 4 2
  3. 6 4 2 0
  4. 正确答案6 6 6 6 6 ... (infinite loop)

scanf 只读一次,x 停在 3。循环里只印 2 * x,没有再读入,也没有改 x,所以一直印 6。

问题 82.5 / 2.5 pts

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);
  1. 0
  2. 正确答案1
  3. 3
  4. 4

输入 5,5 > 9 为假,z 保持 0。z = z + 1 写在 if 外面,所以印 1。

问题 92.5 / 2.5 pts

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];
  1. 正确答案
    for (i = 0; i < SIZE_LIST; ++i) {
       printf("%d, %d \n", keysList[i], itemsList[i]);
    }
  2. printf("%d, %d \n", keysList[SIZE_LIST - 1], itemsList[SIZE_LIST - 1]);
  3. printf("%d, %d \n", keysList[SIZE_LIST], itemsList[SIZE_LIST]);
  4. 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 会越界。

问题 102.5 / 2.5 pts

What is the output?

int num = 10;
while (num <= 15) {
   printf("%d ", num);
   if (num == 12) {
      break;
   }
   ++num;
}
printf("Done");
  1. 10 Done
  2. 10 11 Done
  3. 正确答案10 11 12 Done
  4. 10 11 12 13 14 15 Done

先印再判断。num 为 10、11、12 时都会先印出来,印完 12 才 break,然后印 Done。

问题 112.5 / 2.5 pts

Which identifier name shows good practice?

  1. m_v
  2. revPhyMgt
  3. cost_of_shipping_furniture_crates_by_sea
  4. 正确答案sizeOfBox

sizeOfBox 能看懂,长度也合适。m_v 和 revPhyMgt 看不出意思,最长的那个名字太长。

问题 122.5 / 2.5 pts

What is the ending value of x?

int y = 6;
int x = (4 / 2) * y + 8;
  1. 8
  2. 正确答案20
  3. 28
  4. 32

括号先算,4 / 2 是 2。再乘 y:2 * 6 是 12。最后加 8,得到 20。

问题 132.5 / 2.5 pts

Which statement correctly assigns 12 into the fifth row and third column of array itemArray?

  1. numVals[5][3] = 12;
  2. numVals[3][5] = 12;
  3. 正确答案numVals[4][2] = 12;
  4. numVals[2][4] = 12;

行和列的下标都从 0 开始。第 5 行是 [4],第 3 列是 [2]。

问题 142.5 / 2.5 pts

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?

  1. 正确答案Limited bits may cause some rounding of the mantissa.
  2. If a value is too large for a double, a float should be used instead.
  3. If a value is too large for a double, a long should be used instead.
  4. The compiler may sometimes use 63 bits for the mantissa.

double 的尾数只有有限位,有些小数存不精确,会被舍入。float 比 double 更短,装不下 double 的数时不能改用 float。

问题 150 / 2.5 pts

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';
  1. 交卷时选了这项The first four characters of the array userText are Book.
  2. The first five characters of the array userText are Bookt.
  3. The compiler generates an error because element 3 can't be overwritten.
  4. 正确答案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”,那一项是错的。

问题 162.5 / 2.5 pts

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;
}
  1. 正确答案w < 100 / scanf("%d", &w)
  2. w >= 100 / (nothing)
  3. w < 100 / (nothing)
  4. w >= 100 / scanf("%d", &w)

循环条件是 w < 100。循环里面要再 scanf 下一个整数,否则 w 不变,第一次小于 100 就会一直转。

问题 172.5 / 2.5 pts

Given a vector/array with values 5, 10, 15, 20, 25, what are the fewest number of swaps needed to reverse the list?

  1. 正确答案2
  2. 3
  3. 4
  4. 5

五个数反转成 25 20 15 10 5。交换第 1 个和第 5 个,再交换第 2 个和第 4 个。中间的 15 不用动,一共 2 次。

问题 182.5 / 2.5 pts

Which operator is evaluated first: x + y < y - z * 2 ?

  1. +
  2. <
  3. -
  4. 正确答案*

* 比 +、- 先算,比较 < 最后算。这条式子里最先算的是 z * 2。

问题 192.5 / 2.5 pts

Which generates a random integer in the range 13..19?

  1. rand() % 19
  2. rand() % (19 - 13)
  3. (rand() % (19 - 13)) + 13
  4. 正确答案(rand() % (19 - 13 + 1)) + 13

闭区间的个数是上界减下界再加 1。(rand() % 7) + 13 得到 13 到 19。少写 + 1 时最大只到 18。

问题 202.5 / 2.5 pts

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]);
}
  1. 10, 15, 20, 5
  2. 15, 25, 35, 20
  3. 15, 25, 35, 25
  4. 正确答案Error: Invalid access

数组只有下标 0 到 3。i 走到 3 时要读 x[4],越界。

问题 212.5 / 2.5 pts

What is the purpose of a variable?

  1. To assign values
  2. To perform calculations
  3. 正确答案To hold a value
  4. To hold a constant value

变量是一块用来存放数值的内存,里面的值可以改。一直不变的值用常量。

问题 222.5 / 2.5 pts

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?

  1. srand();
  2. srand(100);
  3. time(srand());
  4. 正确答案srand((int)time(0));

srand((int)time(0)) 用当前时间做种子,每次运行的随机序列才不一样。种子写成固定的 100,每次结果都相同。

问题 232.5 / 2.5 pts

Which statement declares a two-dimensional integer array called myArray with 3 rows and 4 columns?

  1. int myArray[3, 4];
  2. int myArray[7];
  3. 正确答案int myArray[3][4];
  4. int myArray[4][3];

二维数组先写行数,再写列数:int myArray[3][4]。方括号里写逗号不是这种声明。

问题 242.5 / 2.5 pts

What will a compiler do for the following code?

/*
  numItems = 2;  /* Total items to buy */
  rate = 0.5;
*/
  1. Ignore numItems = 2, but generate code for rate = 0.5.
  2. Generate code for numItems = 2, but ignore rate = 0.5.
  3. Ignore both statements.
  4. 正确答案Generate an error.

C 的块注释不能嵌套。第一个 */ 就把注释结束了,最后多出来的 */ 会让编译报错。

问题 252.5 / 2.5 pts

Why should programmers avoid making the following comparison with double x?

x == 0.3
  1. 正确答案Because 0.3 may not be exactly represented as 0.3
  2. Because doubles should not be compared with integers
  3. Because variables should not be compared with literals
  4. Because the 0.3 should come first: 0.3 == x

0.3 在 double 里常常存不成精确的 0.3,用 == 比较可能得到假。

问题 262.5 / 2.5 pts

What is the output?

int n;
for (n = 0; n < 10; n = n + 3) {
   printf("%d ", n);
}
  1. 0 1 2 3 4 5 6 7 8 9
  2. 1 4 7 10
  3. 正确答案0 3 6 9
  4. 0 3 6 9 12

n 从 0 开始,每次加 3,条件是 n < 10。会印 0 3 6 9。n 变成 12 时循环已经停了。

问题 272.5 / 2.5 pts

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
  1. sum = val / sum = val
  2. sum = val / sum = sum + val
  3. 正确答案sum = 0 / sum = sum + val
  4. sum = 0 / sum = val

循环前把 sum 设成 0。循环里先把当前的 val 加进去,再读下一个数。读到 -1 时循环停住,-1 不会被加进去。

问题 282.5 / 2.5 pts

Which declares two related integer arrays named personName and personAge each with 50 elements?

  1. int personName, personAge;
  2. int personName, personAge = 50;
  3. int personName = 50;
    int personAge = 50;
  4. 正确答案
    int personName[50];
    int personAge[50];

两个相关数组都要带上长度:personName[50] 和 personAge[50]。写成 = 50 只是一个整数。

问题 292.5 / 2.5 pts

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 );
   }
}
  1. studentNames[i] / studentNames[i]
  2. studentNames[i] / studentScores[i]
  3. 正确答案studentScores[i] / studentNames[i]
  4. studentScores[i] / studentScores[i]

用 studentScores[i] 和 80 比较,成立时印 studentNames[i]。

问题 302.5 / 2.5 pts

Why might style guidelines limit the number of characters per line?

  1. To speed up the code’s execution
  2. To limit the size of the executable file
  3. To force use of short variable names
  4. 正确答案To make the code more readable

限制每行字符数是为了好读。它不会让程序跑得更快,也不会缩小可执行文件。

问题 312.5 / 2.5 pts

Which expression can be used to decide if x is not between 10 and 20?

  1. 正确答案!(10 < x && x < 20)
  2. !(x < 10 && x < 20)
  3. !(x < 10 || x < 20)
  4. !(x > 10 || x < 20)

“在 10 和 20 之间”写成 10 < x && x < 20。外面加 !,就是不在这个开区间里。

问题 322.5 / 2.5 pts

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];
}
  1. 4, 4, 7, 3, 0
  2. 正确答案7, 3, 0, 8, 8
  3. 7, 3, 0, 8, 4
  4. Error: Invalid access

每次把右边那一格抄到左边。i 只走到 3,读的是还没改过的下一格,最后一格 8 留在原地,结果是 7, 3, 0, 8, 8。

问题 332.5 / 2.5 pts

Which expression fails to compute the area of a triangle having base b and height h (area is one-half base time height)?

  1. (1.0 / 2.0 ) * b * h
  2. 正确答案(1 / 2) * b * h
  3. (b * h) / 2.0
  4. 0.5 * b * h

1 / 2 是两个 int 相除,结果是 0,面积变成 0。另外三项里有小数,除法会按浮点来算。

问题 342.5 / 2.5 pts

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)
  1. 正确答案false OR (true AND false) --> false OR false --> false
  2. false OR (true AND false) --> false OR true --> true
  3. (false OR true) AND false --> true AND false --> false
  4. (false OR true) AND false --> true AND false --> true

&& 比 || 先结合。1 == 5 为假,2 == 2 为真,3 == 5 为假。真并且假得到假,假或者假还是假。

问题 352.5 / 2.5 pts

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
  1. val > 0 / min = val
  2. min < val / val = min
  3. val > min / val = min
  4. 正确答案val < min / min = val

新读到的非负数比 min 更小,才把 min 换成它。负数会让 while 停住,不会写进 min。

问题 362.5 / 2.5 pts

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 _____ .

  1. always evaluate to true
  2. always evaluate to false
  3. 正确答案sometimes evaluate to true, sometimes to false, depending on the values
  4. sometimes immediately exit the program, for values that are slightly different

两个 double 用 == 比较时,能精确表示的值会得到真,有舍入误差的值会得到假。

问题 372.5 / 2.5 pts

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;
}
  1. 正确答案almostEmpty == true && empty == false
  2. full == true || halfFull == true
  3. almostEmpty == true || empty == false
  4. almostEmpty == true && halfFull == true

只有 else if (almostEmpty && !empty) 这一支把 continue 和 needFill 都设成真。油箱满或者过半时,needFill 是假。

问题 382.5 / 2.5 pts

Which expression evaluates to 3.5?

int x = 7;
int y = 1;
  1. x * (1 / 2)
  2. 正确答案(x / 2.0)
  3. x.0 / 2
  4. (x / 2) * 1.0

7 / 2.0 是浮点除法,得到 3.5。(x / 2) 和 (1 / 2) 都是整数除法,小数会被丢掉。

问题 392.5 / 2.5 pts

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);
}
  1. score > 90 / "Nice try" / "Great job"
  2. score < 91 / "Great job" / "Nice try"
  3. 正确答案score < 91 / "Nice try" / "Great job"
  4. (Not possible for given code)

分数是整数时,score < 91 就是不超过 90,走 if 印 Nice try。91 以及更高走 else,印 Great job。

问题 402.5 / 2.5 pts

What code for XXX, YYY correctly swaps a and b? Choices are written as XXX / YYY.

XXX;
a = b;
YYY;
b = tmp;
  1. 正确答案tmp = a / (nothing)
  2. tmp = a / tmp = b
  3. tmp = b / (nothing)
  4. (nothing) / tmp = a

先把 a 存进 tmp,再执行 a = b,最后 b = tmp。中间那一行不用再赋值。