#CCF4481. [GESP202509四级] 客观题
[GESP202509四级] 客观题
一、单选题(每题 分,共计 分;每题有且仅有一个正确选项)
第 1 题 运行下面程序后变量 的值是 ( )
01 int a = 42;
02 int* p = &a;
03 *p = *p + 1;
{{ select(1) }}
- 编译错误
- 不确定
第 2 题 以下关于数组的描述中,( ) 是错误的。
{{ select(2) }}
- 数组名是一个指针常量
- 随机访问数组的元素方便快捷
- 数组可以像指针一样进行自增操作
sizeof(arr)
返回的是整个数组arr
占用的字节数
第 3 题 给定如下定义的数组 arr
,则 *(*(arr + 1) + 2)
的值是 ( )
01 int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
{{ select(3) }}
第 4 题 下面这段代码会输出 ( )
01 int add(int a, int b = 1); // 函数声明
02
03 int main() {
04 cout << add(2) << " " << add(2, 3);
05 return 0;
06 }
07
08 int add(int a, int b) { // 函数定义
09 return a + b;
10 }
{{ select(4) }}
3 5
- 编译失败:定义处少了默认参数
- 运行错误
- 链接失败:未定义引用
第 5 题 下面这段代码会输出 ( )
01 int x = 5;
02
03 void foo() {
04 int x = 10;
05 cout << x << " ";
06 }
07
08 void bar() {
09 cout << x << " ";
10 }
11
12 int main() {
13 foo();
14 bar();
15 }
{{ select(5) }}
5 5
10 10
5 10
10 5
第 6 题 下面程序运行的结果是 ( )
01 void increaseA(int x) {
02 x++;
03 }
04 void increaseB(int* p) {
05 (*p)++;
06 }
07 int main() {
08 int a = 5;
09 increaseA(a);
10 cout << a << " ";
11 increaseB(&a);
12 cout << a;
13 }
{{ select(6) }}
6 7
6 6
5 6
5 5
第 7 题 关于结构体初始化,以下哪个选项中正确的是 ( )
01 struct Point {int x,y;};
{{ select(7) }}
Point p = (1,2);
Point p = {1,2};
Point p = new {1,2};
Point p = <1,2>;
第 8 题 运行如下代码会输出 ( )
01 struct Cat {
02 string name;
03 int age;
04 };
05
06 void birthday(Cat& c) {
07 c.age++;
08 }
09
10 int main() {
11 Cat kitty{"Mimi", 2};
12 birthday(kitty);
13 cout << kitty.name << " " << kitty.age;
14 }
{{ select(8) }}
Mimi 2
Mimi 3
kitty 3
kitty 2
第 9 题 关于排序算法的稳定性,以下说法错误的是 ( )
{{ select(9) }}
- 稳定的排序算法不改变相等元素的相对位置
- 冒泡排序是稳定的排序算法
- 选择排序是稳定的排序算法
- 插入排序是稳定的排序算法
第 10 题 下面代码试图实现选择排序,使其能对数组 nums
排序为升序,则横线上应分别填写 ( )
01 void selectionSort(vector<int>& nums) {
02 int n = nums.size();
03 for (int i = 0; i < n - 1; ++i) {
04 int minIndex = i;
05 for (int j = i + 1; j < n; ++j) {
06 if ( __________ ) { // 在此处填入代码
07 minIndex = j;
08 }
09 }
10 ____________________; // 在此处填入代码
11 }
12 }
{{ select(10) }}
-
01 nums[j] < nums[minIndex] 02 swap(nums[i], nums[minIndex])
-
01 nums[j] > nums[minIndex] 02 swap(nums[i], nums[minIndex])
-
01 nums[j] <= nums[minIndex] 02 swap(nums[j], nums[minIndex])
-
01 nums[j] <= nums[minIndex] 02 swap(nums[i], nums[j])
第 11 题 下面程序实现插入排序 (升序排序),则横线上应分别填写 ( )
01 void insertionSort(int arr[], int n) {
02 for (int i = 1; i < n; i++) {
03 int key = arr[i];
04 int j = i – 1;
05 while ( j >= 0 && ____________________ ) { // 在此处填入代码
06 arr[j + 1] = arr[j];
07 j––;
08 }
09 ____________________; // 在此处填入代码
10 }
11 }
{{ select(11) }}
-
01 arr[j] > key 02 arr[j + 1] = key
-
01 arr[j] < key 02 arr[j + 1] = key
-
01 arr[j] > key 02 arr[j] = key
-
01 arr[j] < key 02 arr[j] = key
第 12 题 关于插入排序的时间复杂度,下列说法正确的是 ( )
{{ select(12) }}
- 最好情况和最坏情况的时间复杂度都是
- 最好情况是 ,最坏情况是
- 最好情况是 ,最坏情况是
- 最好情况是 ,最坏情况是
第 13 题 小杨正在爬楼梯,需要 阶才能到达楼顶,每次可以爬 阶或 阶,求小杨有多少种不同的方法可以爬到楼顶,横线上应填写 ( )
01 int climbStairs(int n) {
02 if (n <= 2) return n;
03 int prev2 = 1;
04 int prev1 = 2;
05 int current = 0;
06 for (int i = 3; i <= n; ++i) {
07 ________________ // 在此处填入代码
08
09 }
10 return current;
11 }
{{ select(13) }}
-
01 prev2 = prev1; 02 prev1 = current; 03 current = prev1 + prev2;
-
01 current = prev1 + prev2; 02 prev2 = prev1; 03 prev1 = current;
-
01 current = prev1 + prev2; 02 prev1 = current; 03 prev2 = prev1;
-
01 prev1 = current; 02 prev2 = prev1; 03 current = prev1 + prev2;
第 14 题 假设有一个班级的成绩单,存储在一个长度为 的数组 中,每个元素是一个学生的分数。老师想要找出 所有满足 的三元组,其中 。下面代码实现该功能,请问其时间复杂度是 ( )
01 int cnt = 0;
02 for (int i = 0; i < n; i++) {
03 for (int j = i + 1; j < n; j++) {
04 for (int k = j + 1; k < n; k++) {
05 if (scores[i] + scores[j] + scores[k] == 300) {
06 cnt++;
07 }
08 }
09 }
10 }
{{ select(14) }}
第 15 题 关于异常处理,以下说法错误的是 ( )
{{ select(15) }}
try
块中的代码可能会抛出异常catch
块可以有多个,处理不同类型的异常throw
语句用于抛出异常- 所有异常都必须被捕获,否则程序会崩溃
二、判断题(每题 2 分,共 20 分)
第 1 题 以下代码能正确初始化指针。
01 int a = 5;
02 int *p = a;
{{ select(16) }}
- 正确
- 错误
第 2 题 执行下面 C++
代码将输出 。
01 int x = 10;
02 void f() {
03 int x = x + 1;
04 cout << x << endl;
05 }
06
07 int main() {
08 f();
09 }
{{ select(17) }}
- 正确
- 错误
第 3 题 以下 C++
代码合法。
01 struct Student {
02 string name;
03 int age;
04 float score;
05 };
06 Student* students = new Student[20];
{{ select(18) }}
- 正确
- 错误
第 4 题 执行下面 C++
代码将输出 。
01 void func(int* p) {
02 *p = 10;
03 }
04
05 int main() {
06 int a = 5;
07 func(&a);
08 cout << a << endl;
09 return 0;
10 }
{{ select(19) }}
- 正确
- 错误
第 5 题 下面代码将二维数组 arr
传递给函数 f
,函数内部用 arr[i][j]
访问元素,函数参数声明为 int arr[] [4]
是错误的。
01 void f(int arr[][4], int rows) {
02 // 访问 arr[i][j]
03 }
04
05 int main() {
06 int arr[3][4] = { /* 初始化 */ };
07 f(arr, 3);
08 }
{{ select(20) }}
- 正确
- 错误
第 6 题 递推是在给定初始条件下,已知前一项 (或前几项) 求后一项的过程。
{{ select(21) }}
- 正确
- 错误
第 7 题 虽然插入排序的时间复杂度为 ,但由于单元操作相对较少,因此在小数据量的排序任务中非常受欢迎。
{{ select(22) }}
- 正确
- 错误
第 8 题 对整数数组 {4, 1, 3, 1, 5, 2}
进行冒泡排序 (将最大元素放到最后),执行一轮之后是 {4, 1, 3, 1, 2, 5}
。
{{ select(23) }}
- 正确
- 错误
第 9 题 以下代码只能捕获 int
类型异常。
01 int main() {
02 try {
03 throw 42;
04 } catch (...) {
05 cout << "Caught" << endl;
06 }
07 return 0;
08 }
{{ select(24) }}
- 正确
- 错误
第 10 题 以下代码将 Hello
写入文件 data.txt
。
01 ofstream file("data.txt");
02 cout<<"Hello"<< endl;
03 file.close();
{{ select(25) }}
- 正确
- 错误