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