#1055. GESP C++ 四 级 2025年3月真题 · 单选题

GESP C++ 四 级 2025年3月真题 · 单选题

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

单选题(共 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;
}

{{ select(1) }}

  • 函数multiply的定义应该放到函数main之前。
  • 函数声明int multiply(int x, int y);中明确指定了函数multiply()的返回值为整数类型。
  • 在main函数中,函数multiply通过multiply(a, b)被调用,其中a和b是定义在main函数中的变量,它们作为实参传递给了multiply函数的形参x和y。
  • 运行上述代码,将输出The result is: 20。

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

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

{{ select(2) }}

  • 2020
  • 2010
  • 1010
  • 编译错误

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

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

{{ select(3) }}

  • 10
  • 20
  • 随机值
  • 编译错误

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

{{ select(4) }}

  • 只能用值传递
  • 只能用引用传递
  • 只能用指针传递
  • 引用传递和指针传递均可

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

{{ select(5) }}

  • 12
  • 21
  • 22
  • 11

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

{{ select(6) }}

  • struct Person {
        string name;
        int age;
    };
    Person p("Yang", 10);
  • struct Person {
        string name,
        int age;
    };
    Person p;
    p.name = "Yang";
    p.age = 10;
  • struct Person
    {
        string name;
        int age;
    };
    Person p = {"Yang", 10};
  • 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;
};

下面描述错误的是(  )。

{{ select(7) }}

  • 结构Person内嵌套结构Address
  • Person 有一个Address 类型的 address 成员
  • 一个Person类型的变量p的address的初始化可以写成:p.address.street = "123 Main St"; p.address.city = "Anytown";
  • 结构的嵌套可以减少命名冲突,因此可以不必控制嵌套层次

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

{{ select(8) }}

  • 2
  • 3
  • 5
  • 6

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

{{ select(9) }}

  • int arr[3,4];
  • int arr[3][4];
  • int arr(3,4);
  • 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;
}

{{ select(10) }}

  • res += f1 + f2;
    f1 = f2;
    f2 = res;
  • res = f1 + f2;
    f1 = f2;
    f2 = res;
  • res += f1 + f2;
    f2 = res;
    f1 = f2;
  • 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;
}

{{ select(11) }}

  • O(n2)
  • O(nx2n)
  • O(1)
  • O(n3)

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

{{ select(12) }}

  • 稳定性指算法的时间复杂度恒定
  • 稳定排序保证相同元素的相对顺序不变
  • 选择排序是稳定排序
  • 插入排序不是稳定排序

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

{{ select(13) }}

  • 3, 5, 1, 8
  • 3, 1, 5, 8
  • 3, 5, 8, 1
  • 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;
}

{{ select(14) }}

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

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

{{ select(15) }}

  • freopen("log.txt", "w", stdout);
    cout << "Happy Spring!" << endl;
    fclose(stdout);
  • std::ofstream outFile("log.txt");
    outFile << "Happy Spring!" << endl;
    outFile.close();
  • std::ofstream outFile("log.txt");
    cout << "Happy Spring!" << endl;
    outFile.close();
  • ofstream log_file("log.txt");
    streambuf* org_cout = cout.rdbuf();
    cout.rdbuf(log_file.rdbuf());
    cout << "Happy Spring!" << endl;
    cout.rdbuf(org_cout);