CCF GESP 2024年9月认证 C++ 4级

单选题
共 15 道 每题 2 分 共计 30 分
第 1 题

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

A

int add(int a, int b) { return a + b; }

B

void add(int a, int b) { return a + b; }

C

int add(a, b) { return a + b; }

D

void add(int a, int b) { return a - b; }

第 2 题

在 C++ 中,形参与实参的关系描述正确的是( )。

A

形参在函数调用时指定,实参在函数定义时传递

B

形参在函数定义时指定,实参在函数调用时传递

C

形参和实参可以互换

D

形参和实参必须是完全相同的类型,不能有任何差异。

第 3 题

运行以下代码,屏幕上将输出( )。

#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;
}
A

100 200 100 200

B

100 200 100 300

C

100 200 200 200

D

100 200 200 300

第 4 题

运行下面代码,屏幕上输出是( )。

int arr[3] = {24, 9, 7};
int* p = arr;
p++;
cout << *p << endl;
A

24

B

9

C

7

D

不确定

第 5 题

运行下面代码片段的结果是( )。

int x = 20;
int y = 24;
int* p = &x;
int* q = &y;
p = q;
A

将 x 赋值为 24

B

将 y 赋值为 20

C

将 q 指向 x 的地址

D

将 p 指向 y 的地址

第 6 题

在 C++ 中,( )正确定义一个名为 student 的结构体,其中包含一个 name 字符数组和一个 age 整数?

A

struct student { char name[20]; int age; };

B

student struct { char name[20]; int age; };

C

student struct { string name; int age; };

D

struct student { char[20] name; int age; };

第 7 题

在 C++ 中,( )正确声明了一个 3 行 4 列的二维数组。

A

int arr[3, 4];

B

int arr[3][4];

C

int arr[4][3];

D

int arr(3, 4);

第 8 题

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

A

3

B

4

C

12

D

16

第 9 题

下面代码采用递推算法来实现整数 n 的阶乘($n! = n × (n - 1) × ... × 2 × 1$),则横线上应填写( )。

int factorial(int n) {
    int result = 1;
    for (int i = 2; i <= n; i++) {
        __________________________    // 在此处填入代码
    }
    return result;
}
A

result *= i;

B

result += i;

C

result *= result;

D

result += result;

第 10 题

在排序算法中,稳定性指的是( )。

A

排序后数据不会丢失

B

排序后相同元素的相对顺序保持不变

C

排序后数据不会被修改

D

排序后数据的时间复杂度不变

第 11 题

下面代码实现了冒泡排序函数,则横线上应填写( )。

//交换数组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, i, j);
                flag = true;
            }
        }
        if (!flag)
            break; // 此轮“冒泡”未交换任何元素
    }
}
A

for (int j = 0; j < arr.size() - 1; j++)

B

for (int j = arr.size() - 1; j > 0; j--)

C

for (int j = 0; j < i; j++)

D

for (int j = i-1; j <=0; j--)

第 12 题

上一题算法的时间复杂度为( )。

A

$O(n^2)$

B

$O(2^n)$

C

$O(1)$

D

$O(n)$

第 13 题

下面代码实现了插入排序函数(升序),则横线上应填写( )。

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;
    }
}
A

while (j >= 0 && nums[j] > base)

B

while (j > 0 && nums[j] > base)

C

while (j >= 0 && nums[j] < base)

D

while (j > 0 && nums[j] < base)

第 14 题

小杨用文件重定向实现在 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;
}
A

cout << "This output will go to the log file." << endl;

B

log_file << "This output will go to the log file." << endl;

C

cout >> "This output will go to the log file." >> endl;

D

log_file >> "This output will go to the log file." >> endl;

第 15 题

运行下面的代码,屏幕上将输出( )。

#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;
}
A

division by zero error result: caught an exception:

B

result: caught an exception: division by zero error

C

caught an exception: division by zero error

D

division by zero error caught an exception: division by zero error

单选题部分已到底了。