#7428. [GESP202412四级] 客观题
[GESP202412四级] 客观题
题目描述
[GESP202412四级] 客观题。本题为客观题,请在页面中按题号依次作答。题目正文、选项、图片或代码片段以页面显示为准。
题目内容
一.单选题(每题2分,共30分)
- 下⾯的语句中,( )正确定义了⼀个计算浮点数x的平⽅(x2 = x * x)的函数,并成功调⽤该函数。
{{ select(1) }}
-
float square(float x) { return x * x; } float area = square(2); ```cpp -
square(float x) { return x * x; } float area = square(2); ```cpp -
void square(float x) { return x * x; } area = square(2.0); ```cpp -
void square(float x) { x * x; return; } area = square(2); ```cpp
- 下⾯代码的描述中,正确的是 ( )。
void n_chars(char c, int n) {
while (n-- > 0)
cout << c;
}
char my_char = 'w';
int times = 5;
n_chars(my_char, times);
```cpp
{{ select(2) }}
- 代码执⾏结束后, times 的值为0
- n 是形参, times 是实参
- n 是形参, times 是实参
- 代码最后⼀⾏换成 n_chars(times, my_char); 也可以
3. 给定以下代码
```cpp
void func(int& x) {
x = x * 2;
}
int a = 5;
func(a);
```cpp
执⾏上述代码后,变量a的值为( )。
{{ select(3) }}
- $5$
- $10$
- $15$
- $20$
4. 运⾏下⾯代码,屏幕上输出是 ( )。
```cpp
double* p_arr = new double [3];
p_arr[0] = 0.2;
p_arr[1] = 0.5;
p_arr[2] = 0.8;
p_arr += 1;
cout << p_arr[0] << endl;
p_arr -= 1;
delete p_arr;
```cpp
{{ select(4) }}
- $0.2$
- $0.5$
- $1.2$
- $1.5$
5. 运⾏下⾯代码⽚段后, x 和*p的结果分别是 ( )。
```cpp
int x = 20;
int* p = &x;
*p = *p + 2;
```cpp
{{ select(5) }}
- 20 20
- 20 22
- 22 20
- 22 22
6. 下⾯的描述中,( )不能正确定义⼀个名为Student 的结构体以及⼀个包含20个元素的结构数组。
{{ select(6) }}
- ```cpp
struct Student {
string name;
int age;
float score;
};
struct Student students[20];
```cpp
- ```cpp
struct Student {
string name;
int age;
float score;
};
Student students[20];
```cpp
- ```cpp
struct Student {
string name;
int age;
float score;
};
Student* students = new Student[20];
```cpp
- ```cpp
struct Student {
string name;
int age;
float score;
};
Student students = new Student[20];
```cpp
7. 假定整型是32位,对⼀个 ⾏ 列的⼆维整数数组 array ,假设数组第⼀个元素在内存中的地址为0x7ffee4065820 ,则第2⾏第2个元素的地址 `&array[1][1]`为
```cpp
int array[2][3] = {
{0, 1, 2},
{3, 4, 5}
};
```cpp
{{ 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. 下⾯代码采⽤递推算法来计算斐波那契数列`f(n)=f(n-1)+f(n-2)`,则横线上应填写( )。
```cpp
int fib(int n) {
if (n == 0 || n == 1)
return n;
int f1 = 0;
int f2 = 1;
int result = 0;
for (int i = 2; i <= n; i++) {
________________________________
}
return result;
}
```cpp
{{ select(9) }}
- ```cpp
result = f1 + f2;
f1 = f2;
f2 = result;
```cpp
- ```cpp
result += f1 + f2;
f1 = f2;
f2 = result;
```cpp
- ```cpp
result += f1 + f2;
f2 = result;
f1 = f2;
```cpp
- ```cpp
result = f1 + f2;
f2 = result;
f1 = f2;
```cpp
10. 下⾯关于排序算法(冒泡排序、插⼊排序和选择排序)的描述中,不正确的是
{{ select(10) }}
- 冒泡排序基于元素交换实现,需借助临时变量,共涉及3个单元操作;⽽插⼊排序基于元素赋值实现,仅需1个单元操作。因此冒泡排序的计算开销通常⽐插⼊排序更⾼。
- 选择排序在任何情况下的时间复杂度都为 O(n<sup>2</sup>)。
- 冒泡排序在任何情况下的时间复杂度都为 O(n<sup>2</sup>)。
- 如果给定数据部分有序,插⼊排序通常⽐选择排序效率更⾼。
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. 给定如下代码,其时间复杂度为
```cpp
int cellRecur(int n) {
if (n == 1)
return 1;
return cellRecur(n - 1) + cellRecur(n - 1) + 1;
}
```cpp
{{ select(12) }}
- $O(n^2)$
- $O(2^n)$
- $O(1)$
- $O(n)$
13. 下⾯代码实现了插⼊排序函数,则横线上应填写
```cpp
void insertion_sort(vector<int> &nums) {
for (int i = 1; i < nums.size(); i++) {
________________________________ { // 在此处填入代码
while (j >= 0 && nums[j] > base)
nums[j + 1] = nums[j];
j--;
}
nums[j + 1] = base;
}
}
```cpp
{{ 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
```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;
}
```cpp
{{ select(14) }}
- ```cpp
freopen("log.txt", "w", stdout);
cout << "Welcome to GESP!" << endl;
fclose(stdout);
```cpp
- ```cpp
std::ofstream outFile("log.txt");
outFile << "Welcome to GESP!" << endl;
outFile.close();
```cpp
- ```cpp
std::ofstream outFile("log.txt");
cout << "Welcome to GESP!" << endl;
outFile.close();
```cpp
- ```cpp
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(oorg_cout);
```cpp
15. 运⾏下⾯的代码,将出现什么情况?
```cpp
double hmean(double a, double b) {
if (a == -b )
throw runtime_error("Runtime error occurred");
return 2.0*a*b/(a + b);
}
int main() {
double x = 10;
double y = -10;
try {
int result = hmean(x, y);
cout << "hmean: " << result << endl;
}
catch (const runtime_error& e) {
cout << "Caught: " << e.what() << endl;
} catch (...) {
cout << "Caught an unknown exception." << endl;
}
return 0;
}
```cpp
{{ select(15) }}
- 屏幕上输出Caught: Runtime error occurred
- 屏幕上输出Caught an unknown exception
- 程序调⽤ std::terminate()
- 编译错误
**二.判断题(每题2分,共20分)**
16. 在 C++ 中,下⾯代码可以正确定义指针和初始化指针。
int* ptr;
*ptr = 10;
{{ select(16) }}
- 正确
- 错误
17. ⼀个函数必须在调⽤之前既声明⼜定义。
{{ select(17) }}
- 正确
- 错误
18. 函数参数可以通过值传递、引⽤传递和指针传递,这样函数内对参数的修改可以直接修改传⼊变量的值。
{{ select(18) }}
- 正确
- 错误
19. `int arr[3][]`是⼀个正确的⼆维数组的声明。
{{ select(19) }}
- 正确
- 错误
20. 递推是⼀种通过已知的初始值和递推公式,逐步求解⽬标值的算法。
{{ select(20) }}
- 正确
- 错误
21. 某算法的递推关系式为T(n)=T(n-1)+n (n为正整数)及 T(0)=1 ,则该算法的时间复杂度为$O(n^2)$ 。
{{ select(21) }}
- 正确
- 错误
22. 冒泡排序的平均时间复杂度为$O(n^2)$ ,但最优情况下为$O(n)$ 。
{{ select(22) }}
- 正确
- 错误
23. 冒泡排序和插⼊排序都是稳定的排序算法。
{{ select(23) }}
- 正确
- 错误
24. 选择排序是稳定的排序算法。
{{ select(24) }}
- 正确
- 错误
25. 在 C++语⾔中,如果⼀个函数可能抛出异常,那么⼀定要在try ⼦句⾥调⽤这个函数。
{{ select(25) }}
- 正确
- 错误
## 输入格式
本题没有标准输入。
## 输出格式
本题没有标准输出,请在交互式作答区域选择或填写答案。
```input1
数据范围与提示
- 本题为 GESP 客观题,包含单选题、判断题等页面作答题型。
- 请按页面中的
select作答区域提交答案;无需编写读取标准输入、输出标准输出的程序。 - 题面中的图片、代码片段和选项以页面显示为准。
来源
GESP202412四级