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

    ID: 3463 Type: Objective Tried: 2 Accepted: 2 Difficulty: 3 Uploaded By: Tags>GESP四级程序设计基础数据结构数组排序递推

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

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

第 1 题

下面 C++ 代码正确声明了一个返回 int 类型、接受两个 int 参数的函数。

int add(int, int);

{{ select(1) }}

  • 正确
  • 错误

第 2 题

下面 C++ 代码的输出是 1515

void foo(int x) {
    x += 5;
}

int main() {
    int a = 10;
    foo(a);
    cout << a << endl;
}

{{ select(2) }}

  • 正确
  • 错误

第 3 题

下面 C++ 代码在一个结构体中又定义了别的结构体。这种结构体嵌套定义的方式语法不正确。

#include <string>
#include <vector>
using namespace std;

struct Library {
    struct Book {
        struct Author {
            string name;
            int birthYear;
        };

        string title;
        int year;
        Author author;
    };

    string name;
    vector<Book> books;
};

{{ select(3) }}

  • 正确
  • 错误

第 4 题

在 C++ 中,相比于值传递,使用引用传递作的优点可以直接操作和修改原始变量,避免数据拷贝,提高效率。

{{ select(4) }}

  • 正确
  • 错误

第 5 题

下面这段代码不合法,因为每一行都必须显式初始化 33 个元素。

int arr[2][3] = {{1, 2}, {3}};

{{ select(5) }}

  • 正确
  • 错误

第 6 题

以下程序中使用了递推方式计算阶乘(n!=1×2××nn! = 1 \times 2 \times \ldots \times n),计算结果正确。

int factorial(int n) {
    int res = 1;
    for (int i = 0; i < n; ++i) {
        res *= i;
    }
    return res;
}

{{ select(6) }}

  • 正确
  • 错误

第 7 题

无论初始数组是否有序,选择排序都执行 O(n2)O(n^2) 次比较。

{{ select(7) }}

  • 正确
  • 错误

第 8 题

以下 C++ 代码,尝试对有 nn 个整数的数组 arr 进行排序。这个代码实现了选择排序算法。

for (int i = 0; i < n - 1; ++i) {
    int minIndex = i;
    for (int j = i + 1; j < n; ++j) {
        if (arr[j] < arr[minIndex])
            minIndex = j;
    }
    if (minIndex != i)
        swap(arr[i], arr[minIndex]);
}

{{ select(8) }}

  • 正确
  • 错误

第 9 题

如果一个异常在 try 块中抛出但没有任何 catch 匹配,它将在编译时报错。

{{ select(9) }}

  • 正确
  • 错误

第 10 题

下面 C++ 代码实现将 Hello 写入 data.txt

ofstream out("data.txt");
out << "Hello";
out.close();

{{ select(10) }}

  • 正确
  • 错误