#P5597. [GESP202409四级] 客观题

[GESP202409四级] 客观题

Description

**一.单选题(每题2分,共30分)** 1. 在 C++ 中, ( ) 正确定义了⼀个返回整数值并接受两个整数参数的函数。 - `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; } ` 2. 在C++中, 形参与实参的关系描述正确的是 ( )。 - 形参在函数调⽤时指定, 实参在函数定义时传递 - 形参在函数定义时指定, 实参在函数调⽤时传递 - 形参和实参可以互换 - 形参和实参必须是完全相同的类型, 不能有任何差异 3. 运⾏以下代码, 屏幕上将输出 ```cpp #include 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; } ``` - `100 200 100 200 ` - `100 200 100 300 ` - `100 200 200 200 ` - `100 200 200 300 ` 4. 运⾏下⾯代码, 屏幕上输出是 ( )。 ```cpp int arr[3] = {24, 9, 7}; int* p = arr; p++; cout << *p << endl; ``` - $24$ - $9$ - $7$ - 不确定 5. 运⾏下⾯代码⽚段的结果是 ( )。 ```cpp int x = 20; int y = 24; int* p = &x; int* q = &y; p = q; ``` - 将x赋值为24 - 将y赋值为20 - 将q指向x的地址 - 将p指向y的地址 6. 在 C++ 中, ( ) 正确定义⼀个名为 student 的结构体, 其中包含⼀个 name 字符数组和⼀个 age 整数 ( )。 - `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; }; ` 7. 在 C++ 中, ( ) 正确声明了⼀个 3 ⾏ 4 列的⼆维数组。 - `int arr[3, 4]; ` - `int arr[3][4]; ` - `int arr[4][3]; ` - `int arr(3, 4); ` 8. ⼀个⼆维数组定义为 `int arr[3][4]; `(假设⼀个int变量占4个字节) , 则 int arr[0] 占⽤( ) 个字节 的内存( ) - $3$ - $4$ - $12$ - $16$ 9. 下⾯代码采⽤递推算法来实现整数 的阶乘( ) , 则横线上应填写 ```cpp int factorial(int n) { int result = 1; for (int i = 2; i <= n; i++) { ________________________________ // 在此处填入代码 } return result; } ``` - `result *= i; ` - `result += i; ` - `result *= result; ` - `result += result; ` 10. 在排序算法中, 稳定性指的是 - 排序后数据不会丢失 - 排序后相同元素的相对顺序保持不变 - 排序后数据不会被修改 - 排序后数据的时间复杂度不变 11. 下⾯代码实现了冒泡排序函数, 则横线上应填写 ( )。 ```cpp //交换数组arr的第i个元素和第j个元素 void swap(vector &arr, int i, int j) { int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } int bubble_sort(vector &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; // 此轮“冒泡”未交换任何元素 } } ``` - `for (int j = 0; j 0; j--) ` - `for (int j = 0; j < i; j++) ` - `for (int j = i-1; j <=0; j--) ` 12. 上⼀题算法的时间复杂度为 - $O(n^2)$ - $O(2^n)$ - $O(1)$ - $O(n)$ 13. 下⾯代码实现了插⼊排序函数(升序) , 则横线上应填写 ```cpp void insertion_sort(vector &nums) { for (int i = 1; i = 0 && nums[j] > base) ` - `while (j > 0 && nums[j] > base) ` - `while (j >= 0 && nums[j] 0 && nums[j] < base) ` 14. ⼩杨⽤⽂件重定向实现在 log.txt ⽂件中输出⽇ 志, 则下⾯横线上应填写 ```cpp #include #include #include 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; } ``` - `cout << "This output will go to the log file." << endl; ` - `log_file << "This output will go to the log file." <> "This output will go to the log file." >> endl; ` - `log_file >> "This output will go to the log file." >> endl; ` 15. 运⾏下⾯的代码, 屏幕上将输出 ```cpp #include 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; } ``` - `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 ` **二.判断题(每题2分,共20分)** 16. 代码 int a = 10; int* p = &a; 可以正确定义指针和初始化指针。 - 正确 - 错误 17. 在 C++ 中, 引⽤传递允许函数修改传递给它的参数的值。 - 正确 - 错误 18. 指针的⼤⼩与其所指向的变量的数据类型的⼤⼩相同。 - 正确 - 错误 19. ⼆维数组的⾏的⼤⼩的必须在定义时确定, 列的⼤⼩可以动态变化。 - 正确 - 错误 20. 递推算法通过逐步求解当前状态和前⼀个或⼏个状态之间的关系来解决问题。 - 正确 - 错误 21. 选择排序是稳定的排序算法。 - 正确 - 错误 22. 插⼊排序的时间复杂度总是⽐冒泡排序低 - 正确 - 错误 23. 在 C++ 中, 如果没有捕获到异常(没有匹配的 catch 块) , 程序会继续执⾏⽽不会终⽌。 - 正确 - 错误 24. 以下代码⽤递推法求斐波那契数列的第 项, 时间复杂度为指数级。 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 = f1 + f2; // F(n) = F(n-1) + F(n-2) f0 = f1; f1 = current; } return current; } - 正确 - 错误 25. 执⾏下⾯C++代码后, 输出的是20。 int point(int* p){ return *p * 2; } int main() { int a = 10; int* p = &a; *p = point(p); cout << *p << endl; } - 正确 - 错误

Source

GESP真题