#2324. GESP 202606 C++ 四级

GESP 202606 C++ 四级

1、小杨正在编写一个“数字交换器”程序,他希望通过函数交换两个变量的值。请问运行以下代码后,屏幕上输出的是( )。

void exchange(int *a, int &b) {
    int t = *a;
    *a = b;
    b = t;
}
int main() {
    int x = 100, y = 200;
    exchange(&x, y);
    cout << x << " " << y;
    return 0;
}

{{ select(1) }}

  • 100 200
  • 200 100
  • 200 200
  • 编译错误

2、下面程序想通过函数计算三门课总分,横线处应填入的是( )。

int sumScore(int a, int b, int c) {
    return a + b + c;
}
int main() {
    int chinese = 88, math = 95, english = 90;
    int total = __________;
    cout << total;
    return 0;
}

{{ select(2) }}

  • sumScore
  • sumScore(chinese, math, english)
  • sumScore(int chinese, int math, int english)
  • sumScore(a, b, c)

3、下面程序输出结果是( )。

int addOne(int x) {
    return x + 1;
}
int main() {
    int a = 6;
    cout << addOne(a) + addOne(3);
    return 0;
}

{{ select(3) }}

  • 9
  • 10
  • 11
  • 12

4、关于下面程序,说法正确的是( )。

void show() {
    int stars = 5;
}
int main() {
    cout << stars;
    return 0;
}

{{ select(4) }}

  • 程序输出 5
  • 程序可以通过编译,但输出随机值
  • 程序不能通过编译,因为 stars 只在 show 函数中有效
  • 程序不能通过编译,因为 cout 不能输出变量

5、小杨在调试一个“等级提升”系统,代码逻辑如下,执行后 *p 的值是( )。

int lv = 5, next_lv = 6;
int *p = &lv;
*p = *p + 1;
p = &next_lv;

{{ select(5) }}

  • 5
  • 6
  • lv 的地址
  • next_lv 的地址

6、小杨正在开发一款名为“星际网格”的游戏,他用二维数组 int map[5][4]; 来表示地图。已知 int 占 4 字节,如果 map 的内存地址是 0x2000,则表达式 &map + 1 的地址值是( )。 {{ select(6) }}

  • 0x204c
  • 0x205c
  • 0x2050
  • 0x2058

7、执行完下面代码后,变量 val 的值是( )。

int data[] = {10, 20, 30, 40, 50};
int *ptr = data + 2;
int val = *(ptr - 1) + *(ptr + 1);

{{ select(7) }}

  • 50
  • 60
  • 70
  • 80

8、某班 3 个小组、每组 4 名同学的分数存入下面的二维数组 score,则 score[1][2] 的值是( )。

int score[3][4] = {
    {80, 81, 82, 83},
    {90, 91, 92, 93},
    {70, 71, 72, 73}
};

{{ select(8) }}

  • 81
  • 90
  • 92
  • 72

9、小杨定义了一个结构体 Hero 来表示游戏角色,下面哪种初始化方式会由于语法错误导致编译失败?( )。

struct Hero {
    string name;
    int hp;
};

{{ select(9) }}

  • Hero h = {"Arthur", 100};
  • Hero h; h.name = "Arthur"; h.hp = 100;
  • Hero h = new Hero{"Arthur", 100};
  • Hero *p = new Hero{"Arthur", 100};

10、下面程序输出结果是( )。

struct Book {
    string title;
    int pages;
};
int main() {
    Book books[2] = {{"Math", 120}, {"Science", 150}};
    cout << books[1].title;
    return 0;
}

{{ select(10) }}

  • Math
  • Science
  • 120
  • 150

11、小杨在对“能量晶石”按亮度进行排序。如果两块晶石亮度相同,他希望保持它们在原始序列中的相对顺序。下列关于排序算法稳定性的说法,错误的是( )。 {{ select(11) }}

  • 冒泡排序是稳定的,因为只有在左边比右边大时才交换。
  • 插入排序是稳定的,因为它将元素插入到相等元素的右侧。
  • 选择排序是稳定的,因为它每次选出最小元素放在前面。
  • 稳定性是指排序后相等元素的相对位置不发生改变。

12、小杨的机器人正在能量踏板上跳跃,踏板编号为 1,2,\ldots,n。跳到第 n 块踏板的方案数满足递推式 f(n)=f(n-1)+f(n-2)。若 f(1)=1,f(2)=2,则运行以下代码计算 jump(5) 的结果是( )。

int jump(int n) {
    if (n <= 2)
        return n;
    int a = 1, b = 2, c = 0;
    for (int i = 3; i <= n; i++) {
        c = a + b;
        a = b;
        b = c;
    }
    return c;
}

{{ select(12) }}

  • 5
  • 8
  • 13
  • 21

13、在“模拟实验室”程序中,为了防止除以 0 导致崩溃,小杨使用了异常处理机制。执行以下代码将输出( )。

try {
    int x = 10, y = 0;
    if (y == 0) throw "Zero Error";
    cout << x / y;
} catch (int e) {
    cout << "Error Code: " << e;
} catch (const char* msg) {
    cout << "Caught: " << msg;
}

{{ select(13) }}

  • 0
  • Error Code: 0
  • Caught: Zero Error
  • 程序直接崩溃

14、下面代码使用某种排序算法,将数组中的元素按从小到大排序。这段代码使用的排序算法是( )。

void mystery_sort(double arr[], int n) {
    for (int i = 0; i < n - 1; i++) {
        int minPos = i;
        for (int j = i + 1; j < n; j++) {
            if (arr[j] < arr[minPos]) {
                minPos = j;
            }
        }
        double temp = arr[i];
        arr[i] = arr[minPos];
        arr[minPos] = temp;
    }
}

{{ select(14) }}

  • 冒泡排序
  • 插入排序
  • 选择排序
  • 非典型排序

15、小杨正在读取“冒险日志”文件 quest.txt。若文件内容为 Level 10,执行以下程序后输出为( )。

ifstream fin("quest.txt");
string s;
int v;
fin >> s >> v;
cout << s.length() * v;

{{ select(15) }}

  • 50
  • 15
  • 70
  • 5

16、运行以下程序后,变量 a 的值最终会变为 20。

void modify(int *p) {
    *p = *p + 10;
}
int main() {
    int a = 10;
    modify(&a);
    return 0;
}

{{ select(16) }}

  • 正确
  • 错误

17、在 C++ 中,引用一旦初始化并绑定到某个变量后,可以通过赋值语句将其重新绑定到另一个变量。 {{ select(17) }}

  • 正确
  • 错误

18、下面程序可以正确计算并输出 3 名学生的平均成绩。

struct Student {
    int id;
    int score;
};
int main() {
    Student students[3] = {
        {1, 90},
        {2, 80},
        {3, 100}
    };
    int sum = 0;
    for (int i = 0; i < 3; i++) {
        sum += students[i].score;
    }
    double average = sum / 3.0;
    cout << average << endl;
    return 0;
}

{{ select(18) }}

  • 正确
  • 错误

19、选择排序算法在寻找每一轮最小值时,如果遇到相等的元素不进行交换,则选择排序是一种稳定的排序算法。 {{ select(19) }}

  • 正确
  • 错误

20、如果使用带 flag 的冒泡排序,且待排序数组一开始就是有序的,那么算法只需一轮扫描即可结束,时间复杂度为 O(n)。 {{ select(20) }}

  • 正确
  • 错误

21、在 C++ 中定义二维数组并初始化时,可以省略第一维,但不能省略第二维。因此 int a[][2] = {{1, 2}, {3, 4}}; 是合法的,而 int a[][] = {{1, 2}, {3, 4}}; 是不合法的。 {{ select(21) }}

  • 正确
  • 错误

22、下面代码的时间复杂度是 O(n^2)。

int cnt = 0;
for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
        cnt++;
    }
}

{{ select(22) }}

  • 正确
  • 错误

23、假设文件 output.txt 能正常打开,下面代码通过 rdbufcout 的输出重定向到了文件中。

ofstream fout("output.txt");
streambuf* old_buf = cout.rdbuf();
cout.rdbuf(fout.rdbuf());
cout << "GESP Exam";
cout.rdbuf(old_buf);

{{ select(23) }}

  • 正确
  • 错误

24、小杨想通过下面程序给饭卡充值,程序会输出 70。

void recharge(int money) {
    money += 20;
}
int main() {
    int card = 50;
    recharge(card);
    cout << card;
    return 0;
}

{{ select(24) }}

  • 正确
  • 错误

25、下面代码可以通过编译。

int a[5];
a++;

{{ select(25) }}

  • 正确
  • 错误