32 #8. GESP C++ 四级 2024年12月 客观题
GESP C++ 四级 2024年12月 客观题
1. 单选题(每题2分,共30分)
第1题
下面的语句中,()正确定义了一个计算浮点数x的平方()的函数,并成功调用该函数。
{{ select(1) }}
-
1 float square(float x) { 2 return x * x; 3 } 4 float area = square(2); -
1 square(float x) { 2 return x * x; 3 } 4 float area = square(2); -
1 void square(float x) { 2 return x * x; 3 } 4 area = square(2.0); -
1 void square(float x) { 2 x * x; 3 return; 4 } 5 area = square(2);
第2题
下面代码的描述中,正确的是()。
1 void n_chars(char c, int n) {
2 while (n-- > 0)
3 cout << c;
4 }
5
6 char my_char = 'w';
7 int times = 5;
8 n_chars(my_char, times);
{{ select(2) }}
- 代码执行结束后,
times的值为0 n是形参,times是实参n是实参,times是形参- 代码最后一行换成
n_chars(times, my_char);也可以
第3题
给定以下代码:
1 void func(int& x) {
2 x = x * 2;
3 }
4
5 int a = 5;
6 func(a);
执行上述代码后,变量a的值为()。
{{ select(3) }}
- 5
- 10
- 15
- 20
第4题
运行下面代码,屏幕上输出是()。
1 double* p_arr = new double[3];
2 p_arr[0] = 0.2;
3 p_arr[1] = 0.5;
4 p_arr[2] = 0.8;
5 p_arr += 1;
6 cout << p_arr[0] << endl;
7 p_arr -= 1;
8 delete p_arr;
{{ select(4) }}
- 0.2
- 0.5
- 1.2
- 1.5
第5题
运行下面代码片段后,x和*p的结果分别是()。
1 int x = 20;
2 int* p = &x;
3 *p = *p + 2;
{{ select(5) }}
- 20 20
- 20 22
- 22 20
- 22 22
第6题
下面的描述中,()不能正确定义一个名为Student的结构体以及一个包含20个元素的结构数组。
{{ select(6) }}
-
1 struct Student { 2 string name; 3 int age; 4 float score; 5 }; 6 struct Student students[20]; -
1 struct Student { 2 string name; 3 int age; 4 float score; 5 }; 6 Student students[20]; -
1 struct Student { 2 string name; 3 int age; 4 float score; 5 }; 6 Student* students = new Student[20]; -
1 struct Student { 2 string name; 3 int age; 4 float score; 5 }; 6 Student students = new Student[20];
第7题
假定整型是32位,对一个2行3列的二维整数数组array,假设数组第一个元素在内存中的地址为0x7ffee4065820,则第2行第2个元素的地址&array[1][1]为()。
1 int array[2][3] = {
2 {0, 1, 2},
3 {3, 4, 5}
4 };
{{ select(7) }}
- 0x7ffee4065824
- 0x7ffee4065828
- 0x7ffee406582c
- 0x7ffee4065830
第8题
下面()正确定义二维数组。
{{ select(8) }}
int a[3][];int a[][];int a[][4];int a[][2] = {{1, 2}, {1, 2}, {3, 4}} ;
第9题
下面代码采用递推算法来计算斐波那契数列 ,则横线上应填写()。
1 int fib(int n) {
2 if (n == 0 || n == 1)
3 return n;
4
5 int f1 = 0;
6 int f2 = 1;
7 int result = 0;
8 for (int i = 2; i <= n; i++) {
9 // 在此处填入代码
10 }
11 return result;
12 }
{{ select(9) }}
-
result = f1 + f2; f1 = f2; f2 = result; -
result += f1 + f2; f1 = f2; f2 = result; -
result += f1 + f2; f2 = result; f1 = f2; -
result = f1 + f2; f2 = result; f1 = f2;
第10题
下面关于排序算法(冒泡排序、插入排序和选择排序)的描述中,不正确的是()。
{{ select(10) }}
- 冒泡排序基于元素交换实现,需借助临时变量,共涉及3个单元操作;而插入排序基于元素赋值实现,仅需1个单元操作。因此冒泡排序的计算开销通常比插入排序更高。
- 选择排序在任何情况下的时间复杂度都为 。
- 冒泡排序在任何情况下的时间复杂度都为 。
- 如果给定数据部分有序,插入排序通常比选择排序效率更高。
第11题
冒泡排序的第一轮操作是从左到右遍历数组,通过两两比较相邻元素,将当前最大的元素移动到末尾。给定数组 arr[] = {4, 1, 3, 1, 5, 2},执行第一轮冒泡排序后数组 arr 中的内容为()。
{{ select(11) }}
- 1, 4, 3, 1, 5, 2
- 1, 3, 1, 4, 2, 5
- 1, 4, 3, 1, 2, 5
- 4, 1, 3, 1, 5, 2
第12题
给定如下代码,其时间复杂度为()。
1 int cellRecur(int n) {
2 if (n == 1)
3 return 1;
4 return cellRecur(n - 1) + cellRecur(n - 1) + 1;
5 }
{{ select(12) }}
第13题
下面代码实现了插入排序函数,则横线上应填写()。
1 void insertion_sort(vector<int>& nums) {
2 for (int i = 1; i < nums.size(); i++) {
3 // 在此处填入代码
4 while (j >= 0 && nums[j] > base) {
5 nums[j + 1] = nums[j];
6 j--;
7 }
8 nums[j + 1] = base;
9 }
10 }
{{ select(13) }}
int base = nums[i], j = i - 1;int base = nums[i], j = i;int base = nums[0], j = i - 1;int base = nums[0], j = i;
第14题
下面哪种方式不能实现将字符串"Welcome to GESP!"输出重定向到文件 log.txt()。
{{ select(14) }}
-
freopen("log.txt", "w", stdout); cout << "Welcome to GESP!" << endl; fclose(stdout); -
std::ofstream outFile("log.txt"); outFile << "Welcome to GESP!" << endl; outFile.close(); -
std::ofstream outFile("log.txt"); cout << "Welcome to GESP!" << endl; outFile.close(); -
ofstream log_file("log.txt"); streambuf* org_cout = cout.rdbuf(); cout.rdbuf(log_file.rdbuf()); cout << "This output will go to the log file." << endl; cout.rdbuf(org_cout);
第15题
运行下面的代码,将出现什么情况?()
1 double hmean(double a, double b) {
2 if (a == -b)
3 throw runtime_error("Runtime error occurred");
4 return 2.0 * a * b / (a + b);
5 }
6
7 int main() {
8 double x = 10;
9 double y = -10;
10
11 try {
12 int result = hmean(x, y);
13 cout << "hmean: " << result << endl;
14 } catch (const runtime_error& e) {
15 cout << "Caught: " << e.what() << endl;
16 } catch (...) {
17 cout << "Caught an unknown exception." << endl;
18 }
19 return 0;
20 }
{{ select(15) }}
- 屏幕上输出
Caught: Runtime error occurred - 屏幕上输出
Caught an unknown exception - 程序调用
std::terminate() - 编译错误
2. 判断题(每题2分,共20分)
第1题
在C++中,下面代码可以正确定义指针和初始化指针。
1 int* ptr;
2 *ptr = 10;
{{ select(16) }}
- 对
- 错
第2题
一个函数必须在调用之前既声明又定义。
{{ select(17) }}
- 对
- 错
第3题
函数参数可以通过值传递、引用传递和指针传递,这样函数内对参数的修改可以直接修改传入变量的值。
{{ select(18) }}
- 对
- 错
第4题
int arr[3][] 是一个正确的二维数组的声明。
{{ select(19) }}
- 对
- 错
第5题
递推是一种通过已知的初始值和递推公式,逐步求解目标值的算法。
{{ select(20) }}
- 对
- 错
第6题
某算法的递推关系式为 (n为正整数)及 ,则该算法的时间复杂度为 。
{{ select(21) }}
- 对
- 错
第7题
冒泡排序的平均时间复杂度为 ,但最优情况下为 。
{{ select(22) }}
- 对
- 错
第8题
冒泡排序和插入排序都是稳定的排序算法。
{{ select(23) }}
- 对
- 错
第9题
选择排序是稳定的排序算法。
{{ select(24) }}
- 对
- 错
第10题
在C++语言中,如果一个函数可能抛出异常,那么一定要在 try 子句里调用这个函数。
{{ select(25) }}
- 对
- 错