#GESP202512C4T2. 判断题(每题 2 分,共 20 分)

判断题(每题 2 分,共 20 分)

  1.  \ 小杨正在调试他的温度传感器程序,其中变量 x 保存当前温度。下面这段代码运行后,变量 x 的值变成了 8。
int x = 5;
int *p = &x;
*p = *p + 3;

{{ select(1) }}

  • 正确
  • 错误

  1.  \ 一个结构体不能包含另一个结构体。

{{ select(2) }}

  • 正确
  • 错误

  1.  \ 在 C++ 中,定义如下二维数组:int a[3][4];,数组 a 在内存中是按行优先连续存放的,即 a[0][0]a[0][1]a[0][2]a[0][3] 在内存中是连续的。

{{ select(3) }}

  • 正确
  • 错误

  1.  \ 执行下面程序后,变量 a 的值会变成 15。
void add(int &x){
    x += 10;
}

int a = 5;
add(a);

{{ select(4) }}

  • 正确
  • 错误

  1.  \ 执行下面的 C++ 代码,会输出 8,因为两个指针地址相差 8 个字节(假设 int 占 4 字节)。
int arr[5] = {1, 2, 3, 4, 5};
int* p1 = arr;
int* p2 = arr + 2;
cout << p2 - p1;  // 输出结果

{{ select(5) }}

  • 正确
  • 错误

  1.  \ 考虑用如下递推方式计算斐波那契数列,时间复杂度是 O(n)O(n)
int n = 10;
int f[20];
f[0] = 0;
f[1] = 1;
for (int i = 2; i <= n; i++)
    f[i] = f[i - 1] + f[i - 2];

{{ select(6) }}

  • 正确
  • 错误

  1.  \ 冒泡排序和插入排序都是稳定排序算法。

{{ select(7) }}

  • 正确
  • 错误

  1.  \ 下面这段代码实现了选择排序算法。
void sort(int a[], int n) {
    for (int i = 1; i < n; i++) {
        int x = a[i];
        int j = i - 1;
        while (j >= 0 && a[j] > x) {
            a[j + 1] = a[j];
            j--;
        }
        a[j + 1] = x;
    }
}

{{ select(8) }}

  • 正确
  • 错误

  1.  \ 下面代码可以正常编译并输出 10。
#include <iostream>
using namespace std;

int calculate(int x, int y = 10);

int main() {
    cout << calculate(5);  // 调用1
    return 0;
}

int calculate(int x, int y) {
    return x * y;
}

int calculate(int x) {  // 重载函数
    return x * 2;
}

{{ select(9) }}

  • 正确
  • 错误

  1.  \ 执行下面代码会输出 100。
int main() {
    ofstream fout("data.txt");
    fout << 10 << " " << 20 << endl;
    fout << 30 << " " << 40;
    fout.close();

    ifstream fin("data.txt");
    int a, b, c, d;
    fin >> a >> b >> c >> d;
    fin.close();

    cout << a + b + c + d;
    return 0;
}

{{ select(10) }}

  • 正确
  • 错误