#GESP202603C4T1. 单选题(每题 2 分,共 30 分)

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

  1.  \ 执行下面程序后,输出为( )。
int f(int x = 2){
    return x * 3;
}

int main(){
    cout << f() << " " << f(4);
}

{{ select(1) }}

  • 2 122 \ 12
  • 6 126 \ 12
  • 6 46 \ 4
  • 12 612 \ 6

  1.  \ 执行下面代码后,输出为( )。
int main() {
    int a = 5;
    int* p = &a;
    int** q = &p;
    **q += 7;
    cout << a << " " << *p;
}

{{ select(2) }}

  • 5 55 \ 5
  • 12 1212 \ 12
  • 12 512 \ 5
  • 5 125 \ 12

  1.  \ 已知:
int a[3][4] = {
    {1,2,3,4},
    {5,6,7,8},
    {9,10,11,12}
};
int (*p)[4] = a;

则表达式 *(*(p + 2) + 1) 的值为( )。

{{ select(3) }}

  • 66
  • 1010
  • 99
  • 1111

  1.  \ 执行下面程序后,输出为( )。
void fun(int a, int &b, int *c){
    a += 1;
    b += 2;
    *c += 3;
}

int main(){
    int x = 1, y = 1, z = 1;
    fun(x, y, &z);
    cout << x << " " << y << " " << z;
}

{{ select(4) }}

  • 2 3 42 \ 3 \ 4
  • 1 3 41 \ 3 \ 4
  • 2 1 42 \ 1 \ 4
  • 1 1 11 \ 1 \ 1

  1.  \ 执行下面程序后输出为( )。
int x = 3;
void f(int& x){
    x += 2;
}
int main(){
    int x = 10;
    f(x);
    cout << x << " " << ::x;
}

{{ select(5) }}

  • 12 312 \ 3
  • 10 510 \ 5
  • 12 512 \ 5
  • 10 310 \ 3

  1.  \ 下列关于结构体初始化的写法,正确的是( )。

{{ select(6) }}

  • struct Point { int x, y; }; Point p = (1,2);
  • struct Point { int x, y; }; Point p = {1,2};
  • struct Point { int x, y; }; Point p = new Point(1,2);
  • struct Point { int x, y; }; Point p = <1,2>;

  1.  \ 执行下面代码后输出为( )。
struct S { int a; int b; };

void g(S s){ s.a += 10; }
void h(S& s){ s.b += 10; }

int main(){
    S s{1,2};
    g(s);
    h(s);
    cout << s.a << " " << s.b;
}

{{ select(7) }}

  • 1111 1212
  • 11 1212
  • 1111 22
  • 11 22

  1.  \ 关于递推算法的描述,正确的是( )。

{{ select(8) }}

  • 递推表现为函数自己调用自己
  • 递推从已知初值出发,利用递推关系逐步推出后续结果
  • 递推只能用于指数复杂度问题
  • 递推一定需要回溯

  1.  \ 执行 climb(6) 的返回值为( )。
int climb(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(9) }}

  • 88
  • 1313
  • 55
  • 1010

  1.  \ 某排序算法对如下数据排序(按 score 升序),则下面关于该排序算法稳定性的描述中,说法正确的是( )。
  • 初始:(90,A),(90,B),(80,C),(90,D)(90, `A'), (90, `B'), (80, `C'), (90, `D')
  • 排序后:(80,C),(90,A),(90,B),(90,D)(80, `C'), (90, `A'), (90, `B'), (90, `D')

{{ select(10) }}

  • 不稳定,因为出现了相同分数
  • 稳定,因为相同 score 的相对顺序保持为 A 在 B 前、B 在 D 前
  • 不稳定,因为 C 跑到前面了
  • 无法判断

  1.  \ 下面代码试图把数组按升序进行“插入排序”,横线处应填写( )。
void ins(int a[], int n){
    for(int i = 1; i < n; i++){
        int key = a[i];
        int j = i-1;
        while(j >= 0 && __________){
            a[j+1] = a[j];
            j--;
        }
        a[j+1] = key;
    }
}

{{ select(11) }}

  • a[j] < key
  • a[j] > key
  • a[j+1] > key
  • a[j] == key

  1.  \ 下列代码段的时间复杂度为( )。
int cnt=0;
for(int i=0; i<n; i++){
    for(int j=0; j<n; j++){
        if( (i+j) % 3 == 0) cnt++;
    }
}

{{ select(12) }}

  • O(n)O(n)
  • O(nlogn)O(n \log n)
  • O(n2)O(n^2)
  • O(2n)O(2^n)

  1.  \ 下面哪种方式不能实现将字符串 Welcome to 2026! 输出重定向到文件 log.txt( )。

A.

freopen("log.txt", "w", stdout);
cout << "Welcome to 2026!" << endl;
fclose(stdout);

B.

std::ofstream outFile("log.txt");
cout << "Welcome to 2026!" << endl;
outFile.close();

C.

ofstream log_file("log.txt");
streambuf* org_cout = cout.rdbuf();
cout.rdbuf(log_file.rdbuf());
cout << "Welcome to 2026!" << endl;
cout.rdbuf(org_cout);

D.

std::ofstream outFile("log.txt");
outFile << "Welcome to 2026!" << endl;
outFile.close();

{{ select(13) }}

  • A
  • B
  • C
  • D

  1.  \ 执行下面程序,输出结果是( )。
int divi(int a,int b){
    if(b==0) throw 0;
    return a/b;
}

int main(){
    try{
        cout << divi(10,0);
    }catch(const char* msg){
        cout << "A";
    }catch(int){
        cout << "B";
    }
}

{{ select(14) }}

- A
- B
- 程序崩溃
- 无输出

---

15. $\ $ 下列函数实现排行榜中单个元素的位置调整(类似插入排序的相邻搬移)。当某玩家分数增加,需将其向前移动时, `while` 循环的条件应为( )。

```cpp
struct Player{ int score; };
void up(Player players[], int n, int idx){
    Player cur = players[idx];
    int i = idx;
    while( _________________ ){
        players[i] = players[i-1];
        i--;
    }
    players[i] = cur;
}

{{ select(15) }}

  • i > 0 && cur.score > players[i-1].score
  • i > 0 && cur.score < players[i-1].score
  • i < n-1 && cur.score > players[i+1].score
  • i < n-1 && cur.score < players[i+1].score