CCF GESP 2025年3月认证 C++ 4级

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

关于下述代码,说法错误的是( )。

int multiply(int x, int y);

int main() {
    int a = 4;
    int b = 5;
    int result = multiply(a, b);
    std::cout << "The result is: " << result << std::endl;
    return 0;
}

int multiply(int x, int y) {
    return x * y;
}
A

函数 multiply 的定义应该放到函数 main 之前。

B

函数声明 int multiply(int x, int y); 中明确指定了函数 multiply() 的返回值为整数类型。

C

main 函数中,函数 multiply 通过 multiply(a, b) 被调用,其中 ab 是定义在 main 函数中的变量,它们作为实参传递给了 multiply 函数的形参 xy

D

运行上述代码,将输出 The result is: 20

第 2 题

执行下述代码将输出( )。

int x = 10;
void func() { int x = 20; std::cout << x; }
int main() {
    func();
    std::cout << x;
    return 0;
}
A

2020

B

2010

C

1010

D

编译错误

第 3 题

执行下述代码后,变量 a 的值为( )。

int a = 10;
int* p = &a;
*p = 20;
A

10

B

20

C

随机值

D

编译错误

第 4 题

以下哪种参数传递方式可以避免拷贝大型对象?

A

只能用值传递

B

只能用引用传递

C

只能用指针传递

D

引用传递和指针传递均可

第 5 题

执行下述代码,将输出( )。

void swap(int a, int &b) {
    int temp = a;
    a = b;
    b = temp;
}
int main() {
    int x = 1, y = 2;
    swap(x, y);
    std::cout << x << y;
    return 0;
}
A

12

B

21

C

22

D

11

第 6 题

下面的描述中,( )正确定义一个名为 Person 的结构体并正确初始化了一个 Person 结构体的变量 p。

A
struct Person {
    string name;
    int age;
};
Person p("Yang", 10);
B
struct Person {
    string name,
    int age;
};
Person p;
p.name = "Yang";
p.age = 10;
C
struct Person {
    string name;
    int age;
};
Person p = { "Yang", 10 };
D
struct Person {
    string name;
    int age;
};
Person p = new Person("Yang", 10);
第 7 题

给定如下代码,

struct Person {
    std::string name;
    int age;
    struct Address {
        std::string street;
        std::string city;
    };
    Address address;
};

下面描述错误的是( )。

A

结构 Person 内嵌套结构 Address

B

Person 有一个 Address 类型的 address 成员

C

一个 Person 类型的变量 paddress 的初始化可以写成:p.address.street = "123 Main St"; p.address.city = "Anytown";

D

结构的嵌套可以减少命名冲突,因此可以不必控制嵌套层次

第 8 题

假设 int arr[2][3] = {{1,2,3},{4,5,6}};,则 arr[1][2] 的值是( )。

A

2

B

3

C

5

D

6

第 9 题

下面( )正确定义了二维数组。

A

int arr[3,4];

B

int arr[3][4];

C

int arr(3,4);

D

int a[3-4];

第 10 题

小杨正在爬楼梯,需要爬 n 阶才能到达楼顶。如果每次可以爬 1 个或 2 个台阶,下面代码采用递推算法来计算一共有多少种不同的方法可以爬到楼顶,则横线上应填写( )。

int f(int n) {
    if (n == 1 || n == 2)
        return n;

    int f1 = 1;
    int f2 = 2;
    int res = 0;
    for (int i = 3; i <= n; i++) {
        __________________________    // 在此处填入代码
    }
    return res;
}
A
res += f1 + f2;
f1 = f2;
f2 = res;
B
res = f1 + f2;
f1 = f2;
f2 = res;
C
res += f1 + f2;
f2 = res;
f1 = f2;
D
res = f1 + f2;
f2 = res;
f1 = f2;
第 11 题

给定如下算法,其时间复杂度为( )。

bool f(int arr[], int n, int target) {
    for (int i = 0; i < (1 << n); i++) {
        int sum = 0;
        for (int j = 0; j < n; j++) {
            if (i & (1 << j)) {
                sum += arr[j];
            }
        }
        if (sum == target) return true;
    }
    return false;
}
A

$O(n^2)$

B

$O(n × 2^n)$

C

$O(1)$

D

$O(n^3)$

第 12 题

下面关于排序稳定性的描述,正确的是( )。

A

稳定性指算法的时间复杂度恒定

B

稳定排序保证相同元素的相对顺序不变

C

选择排序是稳定排序

D

插入排序不是稳定排序

第 13 题

对数组 arr[]={5, 3, 8, 1} 进行升序排序,执行第一轮冒泡排序后数组 arr 中的内容为( )。

A

3, 5, 1, 8

B

3, 1, 5, 8

C

3, 5, 8, 1

D

5, 3, 8, 1

第 14 题

运行下面的代码,将出现( )。

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

屏幕上输出 Caught: Runtime error occurred.

B

屏幕上输出 Caught an unknown exception.

C

程序调用 std::terminate()

D

编译错误

第 15 题

下面哪种方式不能实现将字符串 "Happy Spring!" 输出重定向到文件 log.txt ( )。

A
freopen("log.txt", "w", stdout);
cout << "Happy Spring!" << endl;
fclose(stdout);
B
std::ofstream outFile("log.txt");
outFile << "Happy Spring!" << endl;
outFile.close();
C
std::ofstream outFile("log.txt");
cout << "Happy Spring!" << endl;
outFile.close();
D
ofstream log_file("log.txt");
streambuf* org_cout = cout.rdbuf();
cout.rdbuf(log_file.rdbuf());
cout << "Happy Spring!" << endl;
cout.rdbuf(org_cout);
单选题部分已到底了。