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

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

  1.  \ 小杨想让指针 p 指向整数变量 x,正确写法是( )。

{{ select(1) }}

  • int p = &x;
  • int *p = x;
  • int *p = &x;
  • p = *x;

  1.  \ 小杨写了如下的指针接力程序,程序执行完后变量 a*p1*p2 的值分别是( )。
int a = 5;
int* p1 = &a;
int* p2 = p1;
*p2 = 10;

{{ select(2) }}

  • 5 10 105\ 10\ 10
  • 5 10 155\ 10\ 15
  • 10 10 1010\ 10\ 10
  • 5 5 105\ 5\ 10

  1.  \ 小杨用一个二维数组表示棋盘,其中 11 表示有棋子,00 表示没有棋子。他想知道第 22 行 第 33 列有没有棋子,可采用的代码是:( )。
int a[3][4] = {
    {1, 0, 1, 0},
    {0, 1, 0, 1},
    {1, 1, 0, 0}
};

{{ select(3) }}

  • cout << a[1, 2] << endl;
  • cout << a[1][2] << endl;
  • cout << a(1, 2) << endl;
  • cout << a{1}{2} << endl;

  1.  \ 执行完下面的代码后,*(p + 5)arr[1][1] 的值分别是( )。
int arr[3][4] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}};
int* p = &arr[0][0];

{{ select(4) }}

  • 5 65\ 6
  • 6 56\ 5
  • 5 55\ 5
  • 6 66\ 6

  1.  \ 执行完下面的代码后,sum 的值是( )。
int arr[2][3][2] = {
    {{1,2}, {3,4}, {5,6}},
    {{7,8}, {9,10}, {11,12}}
};
int sum = 0;
for(int i = 0; i < 2; i++)
    for(int j = 0; j < 3; j++)
        for(int k = 0; k < 2; k++)
            if((i+j+k) % 2 == 0)
                sum += arr[i][j][k];

{{ select(5) }}

  • 3636
  • 3939
  • 7878
  • 3030

  1.  \ 执行完下面的代码后,输出是( )。
int a = 1;

void test() {
    int a = 2;
    {
        int a = 3;
        a++;
    }
    a++;
    cout << a << " ";
}

int main() {
    test();
    cout << a;
    return 0;
}

{{ select(6) }}

  • 3 13\ 1
  • 4 14\ 1
  • 3 23\ 2
  • 4 24\ 2

  1.  \ 执行完下面的代码后,abc 的值分别是( )。
void byValue(int x) { x = 100; }
void byRef(int& x) { x = 200; }
void byPointer(int* x) { *x = 300; }

int main() {
    int a = 1, b = 2, c = 3;
    byValue(a);
    byRef(b);
    byPointer(&c);
    return 0;
}

{{ select(7) }}

  • 100 200 300100\ 200\ 300
  • 1 2 31\ 2\ 3
  • 1 200 3001\ 200\ 300
  • 1 2 3001\ 2\ 300

  1.  \ 运行如下代码会输出( )。
struct Point {
    int x, y;
};

struct Rectangle {
    Point topLeft;
    Point bottomRight;
};

int main() {
    Rectangle rect = {{10, 10}, {20, 20}};
    rect.topLeft.x = 5;
    Point* p = &rect.bottomRight;
    p->y = 5;
    cout << rect.topLeft.x + rect.bottomRight.y;
    return 0;
}

{{ select(8) }}

  • 1010
  • 3030
  • 1515
  • 2020

  1.  \ 给定函数 climbStairs(int n) 的定义如下,则 climbStairs(5) 的返回的值是( )。
int climbStairs(int n) {
    if(n <= 2) return n;
    int a = 1, b = 2;
    for(int i = 3; i <= n; i++) {
        int temp = a + b;
        a = b;
        b = temp;
    }
    return b;
}

{{ select(9) }}

  • 55
  • 88
  • 1313
  • 1010

  1.  \ 对如下 44 个扑克牌进行排序,
struct Card {
    int value;
    char suit; // 花色
};

Card cards[4] = {{5,'A'}, {3,'B'}, {5,'C'}, {3,'D'}};

使用某排序算法按value排序后,结果为:{3,'D'}, {3,'B'}, {5,'A'}, {5,'C'},则这个排序算法是稳定的吗?

{{ select(10) }}

  • 稳定,因为相同 value 的元素相对顺序保持不变
  • 不稳定,因为 {3,'D'} 出现在 {3,'B'} 之前
  • 无法判断
  • 稳定,因为结果是有序的

  1.  \ 下面的函数 selectTopK() 实现从 nn 个学生中选出前 kk 名成绩最好的学生颁发奖学金(不需要对所有学生完全排序,只需要找出前 kk 名),则横线上应填写( )。
struct Student {
    string name;
    int score;
};

void selectTopK(Student students[], int n, int k) {
    for (int i = 0; i < k; i++) {
        int maxIdx = i;
        for (__________________) {  // 在此处填入代码
            if (students[j].score > students[maxIdx].score) {
                maxIdx = j;
            }
        }

        if (maxIdx != i) {
            Student temp = students[i];
            students[i] = students[maxIdx];
            students[maxIdx] = temp;
        }
    }
}

{{ select(11) }}

  • int j = 0; j < n; j++
  • int j = i + 1; j < n; j++
  • int j = i; j < n; j++
  • int j = 1; j <= n; j++

  1.  \ 某游戏的排行榜系统需要实时更新玩家分数。每次只有一个玩家的分数发生变化,排行榜已经是按分数降序排列的。现在需要将更新后的玩家调整到正确位置。下面的函数 updateRanking() 要实现上述功能,则两处横线上应分别填写( )。
struct Player {
    string name;
    int score;
};

// 玩家索引 playerIdx 的分数刚刚更新,需要调整位置
void updateRanking(Player players[], int size, int playerIdx) {
    Player updatedPlayer = players[playerIdx];

    if (playerIdx > 0 && updatedPlayer.score > players[playerIdx - 1].score) {
        int i = playerIdx;
        while (____________________) {  // 在此处填入代码
            players[i] = players[i - 1];
            i--;
        }
        players[i] = updatedPlayer;
    }
    else if (playerIdx < size - 1 && updatedPlayer.score < players[playerIdx + 1].score) {
        int i = playerIdx;
        while (____________________) {  // 在此处填入代码
            players[i] = players[i + 1];
            i++;
        }
        players[i] = updatedPlayer;
    }
}

A.

i > 0 && updatedPlayer.score > players[i - 1].score
i < size - 1 && updatedPlayer.score < players[i + 1].score

B.

i < size - 1 && updatedPlayer.score < players[i + 1].score
i > 0 && updatedPlayer.score > players[i - 1].score

C.

i > 0 && updatedPlayer.score < players[i - 1].score
i < size - 1 && updatedPlayer.score < players[i + 1].score

D.

i > 0 && updatedPlayer.score < players[i - 1].score
i < size - 1 && updatedPlayer.score > players[i + 1].score

{{ select(12) }}

  • A
  • B
  • C
  • D

  1.  \ 给定如下算法,其时间复杂度为( )。
bool f(int arr[], int n, int target) {
    for (int i = 0; i < 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(13) }}

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

  1.  \ 执行下面 C++ 程序,会输出( )。
int main() {
    ofstream fout("test.txt");
    fout << "Happy" << endl;
    fout << "New Year";
    fout.close();

    ifstream fin("test.txt");
    string s1, s2;
    fin >> s1;
    getline(fin, s2);
    fin.close();

    cout << s1 << "|" << s2;
    return 0;
}

{{ select(14) }}

  • Happy|New Year
  • Happy| New Year
  • HappyNew Year|
  • Happy|

  1.  \ 执行下面 C++ 代码,会输出( )。
int divide(int a, int b) {
    if(b == 0) throw "Division by zero";
    return a / b;
}

int main() {
    int result = 0;
    try {
        result = divide(10, 0);
        cout << "A";
    }
    catch(const char* msg) {
        cout << "B";
        result = -1;
    }
    cout << result;
    return 0;
}

{{ select(15) }}

  • A0
  • B-1
  • A10
  • 程序崩溃