#59. GESP C++ 四级 2024年09月 客观题

GESP C++ 四级 2024年09月 客观题

1 单选题(每题2分,共30分)

  1. 在C++中,()正确定义了一个返回整数值并接受两个整数参数的函数

{{ 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; }
  1. 在C++中,形参与实参的关系描述正确的是()

{{ select(2) }}

  • 形参在函数调用时指定,实参在函数定义时传递
  • 形参在函数定义时指定,实参在函数调用时传递
  • 形参和实参可以互换
  • 形参和实参必须是完全相同的类型,不能有任何差异
  1. 运行以下代码,屏幕上将输出()
#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;
}

{{ select(3) }}

  • 100 200 100 200
  • 100 200 100 300
  • 100 200 200 200
  • 100 200 200 300
  1. 运行下面代码,屏幕上输出是()
int arr[3] = {24, 9, 7};
int* p = arr;
p++;
cout << *p << endl;

{{ select(4) }}

  • 24
  • 9
  • 7
  • 不确定
  1. 运行下面代码片段的结果是()
int y = 24;
int x = 20;
int* p = &x;
int* q = &y;
p = q;

{{ select(5) }}

  • x将赋值为24
  • y将赋值为20
  • p将指向y的地址
  • q将指向x的地址
  1. 在C++中,()正确定义一个名为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; };
  1. 在C++中,()正确声明了一个3行4列的二维数组

{{ select(7) }}

  • int arr[3, 4];
  • int arr[3][4];
  • int arr[4][3];
  • int arr(3, 4);
  1. 一个二维数组定义为int arr[3][4];(假设一个int变量占4个字节),则int arr[0]占用()个字节的内存

{{ select(8) }}

  • 3
  • 4
  • 12
  • 16
  1. 下面代码采用递推算法来实现整数的阶乘(n!=n×(n1)×...×2×1n! = n \times (n-1) \times ... \times 2 \times 1),则横线上应填写()
int factorial(int n) {
    int result = 1;
    for (int i = 2; i <= n; i++) {
        // 在此处填入代码
    }
    return result;
}

{{ select(9) }}

  • result *= i;
  • result += i;
  • result *= result;
  • result += result;
  1. 在排序算法中,稳定性指的是()

{{ select(10) }}

  • 排序后数据不会丢失
  • 排序后相同元素的相对顺序保持不变
  • 排序后数据不会被修改
  • 排序后数据的时间复杂度不变
  1. 下面代码实现了冒泡排序函数,则横线上应填写()
// 交换数组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, j, j + 1);
                flag = true;
            }
        }
        if (!flag) break; // 此轮“冒泡”未交换任何元素
    }
}

{{ 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--)
  1. 上一题算法的时间复杂度为()

{{ select(12) }}

  • O(n)O(n)
  • O(nlogn)O(n \log n)
  • O(n2)O(n^2)
  • O(2n)O(2^n)
  1. 下面代码实现了插入排序函数(升序),则横线上应填写()
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;
    }
}

{{ 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)
  1. 小杨用文件重定向实现在log.txt文件中输出日志,则下面横线上应填写()
#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;
}

{{ 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;
  1. 运行下面的代码,屏幕上将输出()
#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;
}

{{ 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

2 判断题(每题2分,共20分)

  1. 代码int a = 10; int* p = &a;可以正确定义指针和初始化指针。()

{{ select(16) }}

  • 正确
  • 错误
  1. 在C++中,引用传递允许函数修改传递给它的参数的值。()

{{ select(17) }}

  • 正确
  • 错误
  1. 指针的大小与其所指向的变量的数据类型的大小相同。()

{{ select(18) }}

  • 正确
  • 错误
  1. 二维数组的行的大小必须在定义时确定,列的大小可以动态变化。()

{{ select(19) }}

  • 正确
  • 错误
  1. 递推算法通过逐步求解当前状态和前一个或几个状态之间的关系来解决问题。()

{{ select(20) }}

  • 正确
  • 错误
  1. 选择排序是稳定的排序算法。()

{{ select(21) }}

  • 正确
  • 错误
  1. 插入排序的时间复杂度总是比冒泡排序低。()

{{ select(22) }}

  • 正确
  • 错误
  1. 在C++中,如果没有捕获到异常(没有匹配的catch块),程序会继续执行而不会终止。()

{{ select(23) }}

  • 正确
  • 错误
  1. 以下代码用递推法求斐波那契数列的第n项,时间复杂度为指数级。()
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;
}

{{ select(24) }}

  • 正确
  • 错误
  1. 执行下面C++代码后,输出的是20。()
int point(int* p) {
    return *p * 2;
}
int main() {
    int a = 10;
    int* p = &a;
    *p = point(p);
    cout << *p << endl;
}

{{ select(25) }}

  • 正确
  • 错误