#1166. GESP C++ 四 级 2025年6月真题 · 判断题

GESP C++ 四 级 2025年6月真题 · 判断题

GESP C++ 四 级 2025年6月真题(120 分钟,总分 100 分)

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

1. 下面C++代码正确声明了一个返回int类型、接受两个int参数的函数。

int add(int, int);

{{ select(1) }}

  • 正确
  • 错误

2. 下面C++代码的输出是15。

void foo(int x) {
    x += 5;
}
int main() {
    int a = 10;
    foo(a);
    cout << a << endl;
}

{{ select(2) }}

  • 正确
  • 错误

3. 下面c++代码在一个结构体中又定义了别的结构体。这种结构嵌套定义的方式语法不正确。

#include <string>
#include <vector>
using namespace std;
struct Library
{
    struct Book
    {
        struct Author
        {
            string name;
            int birthYear;
        };
        string title;
        int year;
        Author author;
    };
    string name;
    vector<Book> books;
};

{{ select(3) }}

  • 正确
  • 错误

4. 在C++中,相比于值传递,使用引用传递作的优点可以直接操作和修改原始变量,避免数据拷贝,提高效率。

{{ select(4) }}

  • 正确
  • 错误

5. 下面这段代码不合法,因为每一行都必须显式初始化个元素。

int arr[2][3] = {{1, 2}, {3}};

{{ select(5) }}

  • 正确
  • 错误

6. 以下程序中使用了递推方式计算阶乘(),计算结果正确。

int factorial(int n)
{
    int res = 1;
    for (int i = 0; i < n; ++i)
    {
        res *= i;
    }
    return res;
}

{{ select(6) }}

  • 正确
  • 错误

7. 无论初始数组是否有序,选择排序都执行O(n2)次比较

{{ select(7) }}

  • 正确
  • 错误

8. 以下C++代码,尝试对有n个整数的数组arr进行排序。这个代码实现了选择排序算法。

for (int i = 0; i < n - 1; ++i)
{
    int minIndex = i;
    for (int j = i + 1; j < n; ++j)
    {
        if (arr[j] < arr[minIndex])
            minIndex = j;
    }
    if (minIndex != i)
        swap(arr[i], arr[minIndex]);
}

{{ select(8) }}

  • 正确
  • 错误

9. 如果一个异常在 try 块中抛出但没有任何 catch 匹配,它将在编译时报错。

{{ select(9) }}

  • 正确
  • 错误

10. 下面C++代码实现将 Hello 写入 data.txt。

ofstream out("data.txt");
out << "Hello";
out.close();

{{ select(10) }}

  • 正确
  • 错误