CCF GESP 2025年6月认证 C++ 4级
二
判断题
第 1 题
下面 C++ 代码正确声明了一个返回 int 类型、接受两个 int 参数的函数。
int add(int, int);
第 2 题
下面 C++ 代码的输出是 15。
void foo(int x) {
x += 5;
}
int main() {
int a = 10;
foo(a);
cout << a << endl;
}
第 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;
};
第 4 题
在 C++ 中,相比于值传递,使用引用传递作的优点可以直接操作和修改原始变量,避免数据拷贝,提高效率。
第 5 题
下面这段代码不合法,因为每一行都必须显式初始化 3 个元素。
int arr[2][3] = {{1, 2}, {3}};
第 6 题
以下程序中使用了递推方式计算阶乘($n! = 1 \times 2\ \times\ ...\ \times n$),计算结果正确。
int factorial(int n) {
int res = 1;
for (int i = 0; i < n; ++i) {
res *= i;
}
return res;
}
第 7 题
无论初始数组是否有序,选择排序都执行 $O(n^2)$ 次比较。
第 8 题
以下 C++ 代码,尝试对有 n 个整数的数组 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]);
}
第 9 题
如果一个异常在 try 块中抛出但没有任何 catch 匹配,它将在编译时报错。
第 10 题
下面 C++ 代码实现将 Hello 写入 data.txt。
ofstream out("data.txt");
out << "Hello";
out.close();
判断题部分已到底了。