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

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

登录 后作答才能记录成绩

一、单选题(每题 2 分)

1. 小杨想让指针 p 指向整数变量 x,正确写法是( )。
2. 小杨写了如下的指针接力程序,程序执行完后变量 a、*p1和*p2的值分别是( )。 ```cpp int a = 5; int* p1 = &a; int* p2 = p1; *p2 = 10; ```
3. 小杨用一个二维数组表示棋盘,其中 1 表示有棋子,0 表示没有棋子。他想知道第 2 行 第 3 列有没有棋子,可采用的代码是:( )。 ```cpp int a[3][4] = { {1, 0, 1, 0}, {0, 1, 0, 1}, {1, 1, 0, 0} }; ```
4. 执行完下面的代码后,*(p + 5) 和 arr[1][1]的值分别是( )。 ```cpp int arr[3][4] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}}; int* p = &arr[0][0]; ```
5. 执行完下面的代码后,sum 的值是( )。 ```cpp int arr[2][3][2] = { {{1,2}, {3,4}, {5,6}}, {{7,8}, {9,10}, {11,12}} }; int sum = 0; for(int i = 0; i < 2; i++) for(int j = 0; j < 3; j++) for(int k = 0; k < 2; k++) if((i+j+k) % 2 == 0) sum += arr[i][j][k]; ```
6. 执行完下面的代码后,输出是( )。 ```cpp int a = 1; void test() { int a = 2; { int a = 3; a++; } a++; cout << a << " "; } int main() { test(); cout << a; return 0; } ```
7. 执行完下面的代码后,a 、b 和 c 的值分别是( )。 ```cpp void byValue(int x) { x = 100; } void byRef(int& x) { x = 200; } void byPointer(int* x) { *x = 300; } int main() { int a = 1, b = 2, c = 3; byValue(a); byRef(b); byPointer(&c); return 0; } ```
8. 运行如下代码会输出( )。 ```cpp struct Point { int x, y; }; struct Rectangle { Point topLeft; Point bottomRight; }; int main() { Rectangle rect = {{10, 10}, {20, 20}}; rect.topLeft.x = 5; Point* p = &rect.bottomRight; p->y = 5; cout << rect.topLeft.x + rect.bottomRight.y; return 0; } ```
9. 给定函数 climbStairs(int n) 的定义如下,则 climbStairs(5) 的返回的值是( )。 ```cpp int climbStairs(int n) { if(n <= 2) return n; int a = 1, b = 2; for(int i = 3; i <= n; i++) { int temp = a + b; a = b; b = temp; } return b; } ```
10. 对如下4个扑克牌进行排序, ```cpp struct Card { int value; char suit; // 花色 }; Card cards[4] = {{5,'A'}, {3,'B'}, {5,'C'}, {3,'D'}}; ``` 使用某排序算法按value排序后,结果为: {3,'D'}, {3,'B'}, {5,'A'}, {5,'C'},则这个排序算法是稳定的吗?
11. 下面的函数 selectTopK() 实现从 n 个学生中选出前 k 名成绩最好的学生颁发奖学金(不需要对所有学生完全排序,只需要找出前 k 名),则横线上应填写( )。 ```cpp struct Student { string name; int score; }; void selectTopK(Student students[], int n, int k) { for (int i = 0; i < k; i++) { int maxIdx = i; for (____________________) { // 在此处填入代码 if (students[j].score > students[maxIdx].score) { maxIdx = j; } } if (maxIdx != i) { Student temp = students[i]; students[i] = students[maxIdx]; students[maxIdx] = temp; } } } ```
12. 某游戏的排行榜系统需要实时更新玩家分数。每次只有一个玩家的分数发生变化,排行榜已经是按分数降序排列的。现在需要将更新后的玩家调整到正确位置。下面的函数 updateRanking() 要实现上述功能,则两处横线上应分别填写( )。 ```cpp struct Player { string name; int score; }; // 玩家索引playerIdx的分数刚刚更新,需要调整位置 void updateRanking(Player players[], int size, int playerIdx) { Player updatedPlayer = players[playerIdx]; if (playerIdx > 0 && updatedPlayer.score > players[playerIdx - 1].score) { int i = playerIdx; while (____________________) { // 在此处填入代码 players[i] = players[i - 1]; i--; } players[i] = updatedPlayer; } else if (playerIdx < size - 1 && updatedPlayer.score < players[playerIdx + 1].score) { int i = playerIdx; while (____________________) { // 在此处填入代码 players[i] = players[i + 1]; i++; } players[i] = updatedPlayer; } } ```
13. 给定如下算法,其时间复杂度为( )。 ```cpp bool f(int arr[], int n, int target) { for (int i = 0; i < n; i++) { int sum = 0; for (int j = 0; j < n; j++) { if (i & (1 << j)) { sum += arr[j]; } } if (sum == target) return true; } return false; } ```
14. 执行下面 C++ 程序,会输出( )。 ```cpp int main() { ofstream fout("test.txt"); fout << "Happy" << endl; fout << "New Year"; fout.close(); ifstream fin("test.txt"); string s1, s2; fin >> s1; getline(fin, s2); fin.close(); cout << s1 << "|" << s2; return 0; } ```
15. 执行下面C++代码,会输出( )。 ```cpp int divide(int a, int b) { if(b == 0) throw "Division by zero"; return a / b; } int main() { int result = 0; try { result = divide(10, 0); cout << "A"; } catch(const char* msg) { cout << "B"; result = -1; } cout << result; return 0; } ```

二、判断题(每题 2 分)

1. 小杨正在调试他的温度传感器程序,其中变量 x 保存当前温度。下面这段代码运行后,变量 x 的值变成了 8。 ```cpp int x = 5; int *p = &x; *p = *p + 3; ``` ( )
2. 一个结构体不能包含另一个结构体。 ( )
3. 在 C++ 中,定义如下二维数组:int a[3][4];,数组 a 在内存中是按行优先连续存放的,即 a[0][0]、a[0][1]、a[0][2]、a[0][3] 在内存中是连续的。 ( )
4. 执行下面程序后,变量 a 的值会变成 15。 ```cpp void add(int &x){ x += 10; } int a = 5; add(a); ``` ( )
5. 执行下面的C++代码,会输出 8,因为两个指针地址相差 8 个字节(假设 int 占 4 字节)。 ```cpp int arr[5] = {1, 2, 3, 4, 5}; int* p1 = arr; int* p2 = arr + 2; cout << p2 - p1; // 输出结果 ``` ( )
6. 考虑用如下递推方式计算斐波那契数列,时间复杂度是$O(n)$。 ```cpp 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]; ``` ( )
7. 冒泡排序和插入排序都是稳定排序算法。 ( )
8. 下面这段代码实现了选择排序算法。 ```cpp 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; } } ``` ( )
9. 下面代码可以正常编译并输出 10。 ```cpp #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; } ``` ( )
10. 执行下面代码会输出 100。 ```cpp 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; } ``` ( )