#G24S3C4XZ. [GESP 2024] S3 C++ 四级 选择题

[GESP 2024] S3 C++ 四级 选择题

单选题(共 15 题)

  1. 下列语句中,( )正确定义了一个计算浮点数平方的函数,并成功调用该函数,是 {{ select(1) }}
  • float square(float x) {
        return x * x;
    }
    float area = square(2);
    
  • square(float x) {
        return x * x;
    }
    float area = square(2);
    
  • void square(float x) {
        return x * x;
    }
    area = square(2.0);
    
  • void square(float x) {
        x * x;
        return;
    }
    area = square(2);
    

  1. 已知如下函数和调用方式:
void n_chars(char c, int n) {
    while (n-- > 0)
        cout << c;
}

char my_char = 'w';
int times = 5;
n_chars(my_char, times);

下面代码的描述中,正确的是 {{ select(2) }}

  • 代码执行结束后,times 的值为 0
  • n 是形参,times 是实参
  • n 是实参,times 是形参
  • 代码最后一行换成 n_chars(times, my_char); 也可以
void func(int& x) {
    x = x * 2;
}

int a = 5;
func(a);

给定以上代码,执行后,变量 a 的值为 {{ select(3) }}

  • 5
  • 10
  • 15
  • 20
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;  // 注意此处的 delete p_arr 会产生问题,本题仅考察输出结果

运行上面代码,屏幕上输出是 {{ select(4) }}

  • 0.2
  • 0.5
  • 1.2
  • 1.5
int x = 20;
int* p = &x;
*p = *p + 2;

运行上面代码片段后,x*p 的结果分别是 {{ select(5) }}

  • 20 20
  • 20 22
  • 22 20
  • 22 22
  1. 下面的描述中,( )不能正确定义一个名为 Student 的结构体以及一个包含 20 个元素的结构数组,是 {{ select(6) }}
  • struct Student {
        string name;
        int age;
        float score;
    };
    struct Student  students[20];
    
  • struct Student {
        string name;
        int age;
        float score;
    };
    Student  students[20];
    
  • struct Student {
        string name;
        int age;
        float score;
    };
    Student*  students = new Student[20];
    
  • struct Student {
        string name;
        int age;
        float score;
    };
    Student  students = new Student[20];
    
  1. 假定整型是 3232 位,对一个 2×N2\times N 的二维整数数组 arrayarray,假设数组第一个元素在内存中的地址为 0x7ffee4065820,则第 22 行第 22 个元素的地址 &array[1][1] 为 {{ select(7) }}
  • 0x7ffee4065824
  • 0x7ffee4065828
  • 0x7ffee406582c
  • 0x7ffee4065830
  1. 下面( )正确定义二维数组,是 {{ select(8) }}
  • int a[3][];
  • int a[][];
  • int a[][4];
  • int a[][2] = {
        {1,2},
        {1,2},
        {3,4}
    };
    
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;
}

上面代码采用递推算法来计算斐波那契数列,则横线上应填写 {{ select(9) }}

  • result = f1 + f2;
    f1 = f2;
    f2 = result;
    
  • result += f1 + f2;
    f1 = f2;
    f2 = result;
    
  • result += f1 + f2;
    f2 = result;
    f1 = f2;
    
  • result = f1 + f2;
    f2 = result;
    f1 = f2;
    
  1. 下面关于排序算法(冒泡排序、插入排序和选择排序)的描述中,不正确的是 {{ select(10) }}
  • 冒泡排序基于元素交换实现,需借助临时变量,共涉及较多单元操作;插入排序基于元素赋值实现,仅需较少单元操作。因此冒泡排序的计算开销通常比插入排序更高。
  • 选择排序在任何情况下的时间复杂度都为 O(n2)O(n^2)
  • 冒泡排序在任何情况下的时间复杂度都为 O(n2)O(n^2)
  • 如果给定数据部分有序,插入排序通常比选择排序效率更高。
  1. 冒泡排序的第一轮操作是从左到右遍历数组,通过两两比较相邻元素,将当前最大的元素移动到末尾。给定数组 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
int cellRecur(int n) {
    if (n == 1)
        return 1;
    return cellRecur(n - 1) + cellRecur(n - 1) + 1;
}

给定如上代码,其时间复杂度为 {{ select(12) }}

  • O(1)O(1)
  • O(n)O(n)
  • O(2n)O(2^n)
  • O(n2)O(n^2)
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;
    }
}

上面代码实现了插入排序函数,则横线上应填写 {{ 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;
  1. 下面哪种方式不能实现将字符串 "Welcome to GESP!" 输出重定向到文件 log.txt 是 {{ select(14) }}
  • freopen("log.txt", "w", stdout);
    cout << "Welcome to GESP!" << endl;
    fclose(stdout);
    
  • std::ofstream outFile("log.txt");
    outFile << "Welcome to GESP!" << endl;
    outFile.close();
    
  • std::ofstream outFile("log.txt");
    cout << "Welcome to GESP!" << endl;
    outFile.close();
    
  • 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(org_cout);
    
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;
}

运行上面的代码,将出现什么情况?{{ select(15) }}

  • 屏幕上输出 Caught: Runtime error occurred
  • 屏幕上输出 Caught an unknown exception
  • 程序调用 std::terminate()
  • 编译错误

判断题(共 10 题)

下面判断题仅有“对”或“错”两个选项,请根据题意在 {{ select(n) }} 中进行选择。

  1. 在 C++ 中,下面代码可以正确定义指针和初始化指针。是 {{ select(16) }}
  1. 一个函数必须在调用之前既声明又定义。是 {{ select(17) }}
  1. 函数参数可以通过值传递、引用传递和指针传递,这样函数内对参数的修改可以直接修改传入变量的值。是 {{ select(18) }}
  1. int arr[3][] 是一个正确的二维数组的声明。是 {{ select(19) }}
  1. 递推是一种通过已知的初始值和递推公式,逐步求解目标值的算法。是 {{ select(20) }}
  1. 某算法的递推关系式为 T(n)=2×T(n1)+1T(n) = 2 \times T(n - 1) + 1(n 为正整数)及 T(1)=1T(1) = 1,则该算法的时间复杂度为 O(2n)O(2^n)。是 {{ select(21) }}
  1. 冒泡排序的平均时间复杂度为 O(n2)O(n^2),但最优情况下为 O(n)O(n)。是 {{ select(22) }}
  1. 冒泡排序和插入排序都是稳定的排序算法。是 {{ select(23) }}
  1. 选择排序是稳定的排序算法。是 {{ select(24) }}
  1. 在 C++ 语言中,如果一个函数可能抛出异常,那么一定要在 try 子句里调用这个函数。是 {{ select(25) }}