#1722. 四级(2509)

四级(2509)

C++ 四级

2025 年 09 月

1 单选题(每题 2 分,共 30 分)

第 1 题 运行下面程序后变量 a 的值是( )。

int a = 42;
int* p = &a;
*p = *p + 1;

{{ select(1) }}

  • 42
  • 43
  • 编译错误
  • 不确定

第 2 题 以下关于数组的描述中,( )是错误的。

{{ select(2) }}

  • 数组名是一个指针常量
  • 随机访问数组的元素方便快捷
  • 数组可以像指针一样进行自增操作
  • sizeof(arr) 返回的是整个数组 arr 占用的字节数

第 3 题 给定如下定义的数组 arr ,则 *(*(arr + 1) + 2) 的值是( )。

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

{{ select(3) }}

  • 2
  • 5
  • 4
  • 6

第 4 题 下面这段代码会输出( )。

int add(int a, int b = 1); // 函数声明

int main() {
    cout << add(2) << " " << add(2, 3);
    return 0;
}

int add(int a, int b) { // 函数定义
    return a + b;
}

{{ select(4) }}

  • 3 5
  • 编译失败:定义处少了默认参数
  • 运行错误
  • 链接失败:未定义引用

第 5 题 下面这段代码会输出( )。

int x = 5;

void foo() {
    int x = 10;
    cout << x << " ";
}

void bar() {
    cout << x << " ";
}

int main() {
    foo();
    bar();
}

{{ select(5) }}

  • 5 5
  • 10 10
  • 5 10
  • 10 5

第 6 题 下面程序运行的结果是( )。

void increaseA(int x) {
    x++;
}
void increaseB(int* p) {
    (*p)++;
}
int main() {
    int a = 5;
    increaseA(a);
    cout << a << " ";
    increaseB(&a);
    cout << a;
}

{{ select(6) }}

  • 6 7
  • 6 6
  • 5 6
  • 5 5

第 7 题 关于结构体初始化,以下哪个选项中正确的是( )。

struct Point {int x,y;}; 

{{ select(7) }}

  • Point p = (1,2);
  • Point p = {1,2};
  • Point p = new {1,2};
  • Point p = <1,2>;

第 8 题 运行如下代码会输出( )。

struct Cat {
    string name;
    int age;
};

void birthday(Cat& c) {
    c.age++;
}

int main() {
    Cat kitty{ "Mimi", 2 };
    birthday(kitty);
    cout << kitty.name << " " << kitty.age;
}

{{ select(8) }}

  • Mimi 2
  • Mimi 3
  • kitty 3
  • kitty 2

第 9 题 关于排序算法的稳定性,以下说法错误的是( )。

{{ select(9) }}

  • 稳定的排序算法不改变相等元素的相对位置
  • 冒泡排序是稳定的排序算法
  • 选择排序是稳定的排序算法
  • 插入排序是稳定的排序算法

第 10 题 下面代码试图实现选择排序,使其能对数组 nums 排序为升序,则横线上应分别填写( )。

void selectionSort(vector<int>& nums) {
    int n = nums.size();
    for (int i = 0; i < n - 1; ++i) {
        int minIndex = i;
        for (int j = i + 1; j < n; ++j) {
            if (__________) { // 在此处填入代码
                minIndex = j;
            }
        }
        ____________________; // 在此处填入代码
    }
}

//A:
nums[j] < nums[minIndex]
swap(nums[i], nums[minIndex])

//B:
nums[j] > nums[minIndex]
swap(nums[i], nums[minIndex])

//C:
nums[j] <= nums[minIndex]
swap(nums[j], nums[minIndex])

//D:
nums[j] <= nums[minIndex]
swap(nums[i], nums[j])

{{ select(10) }}

  • A
  • B
  • C
  • C

第 11 题 下面程序实现插入排序(升序排序),则横线上应分别填写( )。

void insertionSort(int arr[], int n) {
    for (int i = 1; i < n; i++) {
        int key = arr[i];
        int j = i - 1;
        while (j >= 0 && ____________________) { // 在此处填入代码
            arr[j + 1] = arr[j];
            j--;
        }
        ____________________; // 在此处填入代码
    }
}

//A:
arr[j] > key
arr[j + 1] = key

//B:
arr[j] < key
arr[j + 1] = key

//C:
arr[j] > key
arr[j] = key

//D:
arr[j] < key
arr[j] = key

{{ select(11) }}

  • A
  • B
  • C
  • C

第 12 题 关于插入排序的时间复杂度,下列说法正确的是( )。

{{ select(12) }}

  • 最好情况和最坏情况的时间复杂度都是O(n2)O(n^2)
  • 最好情况是 O(n)O(n),最坏情况是O(n2)O(n^2)
  • 最好情况是 O(n)O(n),最坏情况是O(2n)O(2^n)
  • 最好情况是 O(n2)O(n^2),最坏情况是O(2n)O(2^n)

第 13 题 小杨正在爬楼梯,需要 nn 阶才能到达楼顶,每次可以爬 1 阶或 2 阶,求小杨有多少种不同的方法可以爬到楼顶,横线上应填写( )。

int climbStairs(int n) {
    if (n <= 2) return n;
    int prev2 = 1;
    int prev1 = 2;
    int current = 0;
    for (int i = 3; i <= n; ++i) {
        ________________ // 在此处填入代码
    }
    return current;
}

//A:
prev2 = prev1;
prev1 = current;
current = prev1 + prev2;

//B:
current = prev1 + prev2;
prev2 = prev1;
prev1 = current;

//C:
current = prev1 + prev2;
prev1 = current;
prev2 = prev1;

//D:
prev1 = current;
prev2 = prev1;
current = prev1 + prev2;

{{ select(13) }}

  • A
  • B
  • C
  • D

第 14 题 假设有一个班级的成绩单,存储在一个长度为 n 的数组 scores 中,每个元素是一个学生的分数。老师想要找出 所有满足 scores[i] + scores[j] + scores[k] == 300 的三元组,其中 i < j < k。下面代码实现该功能,请问其时间复杂度是( )。

int cnt = 0;
for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
        for (int k = j + 1; k < n; k++) {
            if (scores[i] + scores[j] + scores[k] == 300) {
                cnt++;
            }
        }
    }
}

{{ select(14) }}

  • O(n)O(n)
  • O(n2)O(n^2)
  • O(n3)O(n^3)
  • O(2n)O(2^n)

第 15 题 关于异常处理,以下说法错误的是( )。

{{ select(15) }}

  • try 块中的代码可能会抛出异常
  • catch 块可以有多个,处理不同类型的异常
  • throw 语句用于抛出异常
  • 所有异常都必须被捕获,否则程序会崩溃

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

第 1 题 以下代码能正确初始化指针。

int a = 5;
int *p = a;

{{ select(16) }}

第 2 题 执行下面C++代码将输出 11 。

int x = 10;
void f() {
    int x = x + 1;
    cout << x << endl;
}

int main() {
    f();
}

{{ select(17) }}

第 3 题 以下C++代码合法。

struct Student {
    string name;
    int age;
    float score;
};
Student* students = new Student[20];

{{ select(18) }}

第 4 题 执行下面C++代码将输出 10 。

void func(int* p) {
    *p = 10;
}

int main() {
    int a = 5;
    func(&a);
    cout << a << endl;
    return 0;
}

{{ select(19) }}

第 5 题 下面代码将二维数组 arr 传递给函数 f ,函数内部用 arr[i][j] 访问元素,函数参数声明为 int arr[][4] 是错误的。

void f(int arr[][4], int rows) {
    // 访问 arr[i][j]
}

int main() {
    int arr[3][4] = { /* 初始化 */ };
    f(arr, 3);
}

{{ select(20) }}

第 6 题 递推是在给定初始条件下,已知前一项(或前几项)求后一项的过程。

{{ select(21) }}

第 7 题 虽然插入排序的时间复杂度为 O(n2)O(n^2),但由于单元操作相对较少,因此在小数据量的排序任务中非常受欢迎。

{{ select(22) }}

第 8 题 对整数数组 {4, 1, 3, 1, 5, 2} 进行冒泡排序(将最大元素放到最后),执行一轮之后是 {4, 1, 3, 1, 2, 5}

{{ select(23) }}

第 9 题 以下代码只能捕获 int 类型异常。

int main() {
    try {
        throw 42;
    }
    catch (...) {
        cout << "Caught" << endl;
    }
    return 0;
}

{{ select(24) }}

第 10 题 以下代码将 Hello 写入文件 data.txt 。

ofstream file("data.txt");
cout<<"Hello"<< endl;
file.close();

{{ select(25) }}