跳到正文
OJ
佛大附属信奥
在线评测 · 分级学习
学习
题库
测验
提交
作业
留言
登录
菜单
学习
题库
测验
提交
作业
留言
登录
GESP C++ 四级 · 2024 年 9 月认证理论卷
登录
后作答才能记录成绩
一、单选题(每题 2 分)
1.
在 C++ 中,( )正确定义了一个返回整数值并接受两个整数参数的函数。
A. int add(int a, int b) { return a + b; }
B. void add(int a, int b) { return a + b; }
C. int add(a, b) { return a + b; }
D. void add(int a, int b) { return a - b; }
2.
在C++中,形参与实参的关系描述正确的是( )。
A. 形参在函数调用时指定,实参在函数定义时传递
B. 形参在函数定义时指定,实参在函数调用时传递
C. 形参和实参可以互换
D. 形参和实参必须是完全相同的类型,不能有任何差异。
3.
运行以下代码,屏幕上将输出( )。 ```cpp #include <iostream> using namespace std; int var = 100; void function() { int var = 200; cout << var << " "; cout << ::var << " "; } int main() { cout << var << " "; function(); var += 100; cout << var << " "; return 0; } ```
A. 100 200 100 200
B. 100 200 100 300
C. 100 200 200 200
D. 100 200 200 300
4.
运行下面代码,屏幕上输出是( )。 ```cpp int arr[3] = {24, 9, 7}; int* p = arr; p++; cout << *p << endl; ```
A. 24
B. 9
C. 7
D. 不确定
5.
运行下面代码片段的结果是( )。 ```cpp int x = 20; int y = 24; int* p = &x; int* q = &y; p = q; ```
A. 将x赋值为24
B. 将y赋值为20
C. 将p指向x的地址
D. 将p指向y的地址
6.
在 C++ 中,( )正确定义一个名为student的结构体,其中包含一个name字符数组和一个age整数?
A. struct student { char name[20]; int age; };
B. student struct { char name[20]; int age; };
C. student struct { string name; int age; };
D. struct student { char[20] name; int age; };
7.
在 C++ 中,( )正确声明了一个 3 行 4 列的二维数组。
A. int arr[3, 4];
B. int arr[3][4];
C. int arr[4][3];
D. int arr(3, 4);
8.
一个二维数组定义为 int arr[3][4];(假设一个int变量占4个字节),则int arr[0]占用( )个字节的内存。
A. 3
B. 4
C. 12
D. 16
9.
下面代码采用递推算法来实现整数n的阶乘(n!),则横线上应填写( )。 ```cpp int factorial(int n) { int result = 1; for (int i = 2; i <= n; i++) { ________________________________ // 在此处填入代码 } return result; } ```
A. result *= i;
B. result += i;
C. result *= result;
D. result += result;
10.
在排序算法中,稳定性指的是( )。
A. 排序后数据不会丢失
B. 排序后相同元素的相对顺序保持不变
C. 排序后数据不会被修改
D. 排序后数据的时间复杂度不变
11.
下面代码实现了冒泡排序函数,则横线上应填写( )。 ```cpp //交换数组arr的第i个元素和第j个元素 void swap(vector<int> &arr, int i, int j) { int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } int bubble_sort(vector<int> &arr) { for (int i = arr.size() - 1; i > 0; i--) { bool flag = false; // 标志位 ________________________________ { // 在此处填入代码 if (arr[j] > arr[j + 1]) { swap(arr, i, j); flag = true; } } if (!flag) break; // 此轮"冒泡"未交换任何元素 } } ```
A. for (int j = 0; j < arr.size() - 1; j++)
B. for (int j = arr.size() - 1; j > 0; j--)
C. for (int j = 0; j < i; j++)
D. for (int j = i-1; j <=0; j--)
12.
上一题算法的时间复杂度为( )。
A. $O(n)$
B. $O(n^2)$
C. $O(n \log n)$
D. $O(n^3)$
13.
下面代码实现了插入排序函数(升序),则横线上应填写( )。 ```cpp void insertion_sort(vector<int> &nums) { for (int i = 1; i < nums.size(); i++) { int base = nums[i], j = i - 1; ________________________________ { // 在此处填入代码 nums[j + 1] = nums[j]; j--; } nums[j + 1] = base; } } ```
A. while (j >= 0 && nums[j] > base)
B. while (j > 0 && nums[j] > base)
C. while (j >= 0 && nums[j] < base)
D. while (j > 0 && nums[j] < base)
14.
小杨用文件重定向实现在log.txt文件中输出日志,则下面横线上应填写( )。 ```cpp #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ofstream log_file("log.txt"); streambuf* original_cout = cout.rdbuf(); cout.rdbuf(log_file.rdbuf()); ___________________________________ // 在此处填入代码 cout.rdbuf(original_cout); // 恢复原始的标准输出缓冲区 return 0; } ```
A. cout << "This output will go to the log file." << endl;
B. log_file << "This output will go to the log file." << endl;
C. cout >> "This output will go to the log file." >> endl;
D. log_file >> "This output will go to the log file." >> endl;
15.
运行下面的代码,屏幕上将输出( )。 ```cpp #include <iostream> using namespace std; int divide(int a, int b) { if (b == 0) { throw runtime_error("division by zero error "); } return a / b; } int main() { int x = 10; int y = 0; // 设为 0 会导致除零错误 try { int result = divide(x, y); cout << "result: " << result << endl; } catch (const runtime_error& e) { cout << "caught an exception: " << e.what() << endl; } return 0; } ```
A. division by zero error result: caught an exception:
B. result: caught an exception: division by zero error
C. caught an exception: division by zero error
D. division by zero error caught an exception: division by zero error
二、判断题(每题 2 分)
1.
代码int a = 10; int* p = &a;可以正确定义指针和初始化指针。 ( )
对
错
2.
在 C++ 中,引用传递允许函数修改传递给它的参数的值。 ( )
对
错
3.
指针的大小与其所指向的变量的数据类型的大小相同。 ( )
对
错
4.
二维数组的行的大小的必须在定义时确定,列的大小可以动态变化。 ( )
对
错
5.
递推算法通过逐步求解当前状态和前一个或几个状态之间的关系来解决问题。 ( )
对
错
6.
选择排序是稳定的排序算法。 ( )
对
错
7.
插入排序的时间复杂度总是比冒泡排序低。 ( )
对
错
8.
在 C++ 中,如果没有捕获到异常(没有匹配的 catch 块),程序会继续执行而不会终止。 ( )
对
错
9.
以下代码用递推法求斐波那契数列的第n项,时间复杂度为指数级。 ```cpp int fibonacci(int n) { if (n == 0) return 0; if (n == 1) return 1; int f0 = 0; // F(0) int f1 = 1; // F(1) int current; for (int i = 2; i <= n; i++) { current = f0 + f1; // F(n) = F(n-1) + F(n-2) f0 = f1; f1 = current; } return current; } ``` ( )
对
错
10.
执行下面C++代码后,输出的是20。 ```cpp int point(int* p){ return *p * 2; } int main() { int a = 10; int* p = &a; *p = point(p); cout << *p << endl; } ``` ( )
对
错