佛大附属信奥 在线评测 · 分级学习
登录

GESP C++ 四级 · 2025 年 9 月认证理论卷

登录 后作答才能记录成绩

一、单选题(每题 2 分)

1. 运行下面程序后变量a的值是( )。 ```cpp int a = 42; int* p = &a; *p = *p + 1; ```
2. 以下关于数组的描述中,( )是错误的。
3. 给定如下定义的数组arr,则*(*(arr + 1) + 2)的值是( )。 ```cpp int arr[2][3] = {{1, 2, 3}, {4, 5, 6}}; ```
4. 下面这段代码会输出( )。 ```cpp int add(int a, int b = 1); // 函数声明 int main() { cout << add(2) << " " << add(2, 3); return 0; } int add(int a, int b) { // 函数定义 return a + b; } ```
5. 下面这段代码会输出( )。 ```cpp int x = 5; void foo() { int x = 10; cout << x << " "; } void bar() { cout << x << " "; } int main() { foo(); bar(); } ```
6. 下面程序运行的结果是( )。 ```cpp void increaseA(int x) { x++; } void increaseB(int* p) { (*p)++; } int main() { int a = 5; increaseA(a); cout << a << " "; increaseB(&a); cout << a; } ```
7. 关于结构体初始化,以下哪个选项中正确的是( )。 ```cpp struct Point {int x,y;}; ```
8. 运行如下代码会输出( )。 ```cpp struct Cat { string name; int age; }; void birthday(Cat& c) { c.age++; } int main() { Cat kitty{"Mimi", 2}; birthday(kitty); cout << kitty.name << " " << kitty.age; } ```
9. 关于排序算法的稳定性,以下说法错误的是( )。
10. 下面代码试图实现选择排序,使其能对数组 nums 排序为升序,则横线上应分别填写( )。 ```cpp void selectionSort(vector<int>& nums) { int n = nums.size(); for (int i = 0; i < n - 1; ++i) { int minIndex = i; for (int j = i + 1; j < n; ++j) { if ( __________ ) { // 在此处填入代码 minIndex = j; } } ____________________; // 在此处填入代码 } } ```
11. 下面程序实现插入排序(升序排序),则横线上应分别填写( )。 ```cpp void insertionSort(int arr[], int n) { for (int i = 1; i < n; i++) { int key = arr[i]; int j = i - 1; while ( j >= 0 && ____________________ ) { // 在此处填入代码 arr[j + 1] = arr[j]; j--; } ____________________; // 在此处填入代码 } } ```
12. 关于插入排序的时间复杂度,下列说法正确的是( )。
13. 小杨正在爬楼梯,需要n阶才能到达楼顶,每次可以爬1阶或2阶,求小杨有多少种不同的方法可以爬到楼顶,横线上应填写( )。 ```cpp int climbStairs(int n) { if (n <= 2) return n; int prev2 = 1; int prev1 = 2; int current = 0; for (int i = 3; i <= n; ++i) { ________________ // 在此处填入代码 } return current; } ```
14. 假设有一个班级的成绩单,存储在一个长度为 n 的数组 scores 中,每个元素是一个学生的分数。老师想要找出所有满足 scores[i] + scores[j] + scores[k] == 300 的三元组,其中 i < j < k。下面代码实现该功能,请问其时间复杂度是( )。 ```cpp int cnt = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { for (int k = j + 1; k < n; k++) { if (scores[i] + scores[j] + scores[k] == 300) { cnt++; } } } } ```
15. 关于异常处理,以下说法错误的是( )。

二、判断题(每题 2 分)

1. 以下代码能正确初始化指针。 ```cpp int a = 5; int *p = a; ``` ( )
2. 执行下面C++代码将输出 11。 ```cpp int x = 10; void f() { int x = x + 1; cout << x << endl; } int main() { f(); } ``` ( )
3. 以下C++代码合法。 ```cpp struct Student { string name; int age; float score; }; Student* students = new Student[20]; ``` ( )
4. 执行下面C++代码将输出 10。 ```cpp void func(int* p) { *p = 10; } int main() { int a = 5; func(&a); cout << a << endl; return 0; } ``` ( )
5. 下面代码将二维数组arr传递给函数f,函数内部用arr[i][j]访问元素,函数参数声明为int arr[][4]是错误的。 ```cpp void f(int arr[][4], int rows) { // 访问 arr[i][j] } int main() { int arr[3][4] = { /* 初始化 */ }; f(arr, 3); } ``` ( )
6. 递推是在给定初始条件下,已知前一项(或前几项)求后一项的过程。 ( )
7. 虽然插入排序的时间复杂度为$O(n^2)$,但由于单元操作相对较少,因此在小数据量的排序任务中非常受欢迎。 ( )
8. 对整数数组{4, 1, 3, 1, 5, 2}进行冒泡排序(将最大元素放到最后),执行一轮之后是{4, 1, 3, 1, 2, 5}。 ( )
9. 以下代码只能捕获int类型异常。 ```cpp int main() { try { throw 42; } catch (...) { cout << "Caught" << endl; } return 0; } ``` ( )
10. 以下代码将 Hello 写入文件 data.txt。 ```cpp ofstream file("data.txt"); cout<<"Hello"<< endl; file.close(); ``` ( )