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

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

GESP 2025年6月认证 C++ 4级试题

二、判断题

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

int add(int, int);

{{ select(16) }}


第 17 题 下面C++代码的输出是 15。

void foo(int x) {
    x += 5;
}
int main() {
    int a = 10;
    foo(a);
    cout << a << endl;
}

{{ select(17) }}


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

struct Library {
    struct Book {
        struct Author {
            string name;
            int birthYear;
        };
        string title;
        int year;
        Author author;
    };
    string name;
    vector<Book> books;
};

{{ select(18) }}


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


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

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

{{ select(20) }}


第 21 题 以下程序中使用了递推方式计算阶乘(n!n!),计算结果正确。

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

{{ select(21) }}


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


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

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(23) }}


第 24 题 如果一个异常在 try 块中抛出但没有任何 catch 匹配,它将在编译时报错。 {{ select(24) }}


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

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

{{ select(25) }}