海小星AI平台海小星AI平台
理论卷2025年12月GESP等级认证(C++) · 六级

GESP C++ 6级 2025.12

满分 100 · 及格 60 · 时长 60 分钟 · 共 27 题 (单选15 / 判断10 / 编程题2)
本卷含 2 道编程题——可以先到「编程」专栏单独练习: 不计时、不关联卷面分,提交后按测例判分。
试卷阅览 · 免费预览前 10 题 · 交卷后可查看答案与解析
1
单选题号 #10998
分值 2

在⾯向对象编程中,下列关于虚函数的描述中,错误的是( )。

A

虚函数⽤于⽀持运⾏时多态

B

通过基类指针调⽤虚函数时,会根据对象实际类型决定调⽤版本

C

构造函数可以声明为虚函数以⽀持多态

D

基类析构函数常声明为虚函数以避免资源泄漏

2
单选题号 #10999
分值 2

执⾏如下代码,会输出钢琴:叮咚叮咚和吉他:咚咚当当。这体现了⾯向对象编程的( )特性。

class Instrument {
public:
    virtual void play() {
        cout << "乐器在演奏声音" << endl;
    }

    virtual ~Instrument() {}
};

class Piano : public Instrument {
public:
    void play() override {
        cout << "钢琴:叮咚叮咚" << endl;
    }
};

class Guitar : public Instrument {
public:
    void play() override {
        cout << "吉他:咚咚当当" << endl;
    }
};

int main() {
    Instrument* instruments[2];
    instruments[0] = new Piano();
    instruments[1] = new Guitar();

    for (int i = 0; i < 2; ++i) {
        instruments[i]->play();
    }

    for (int i = 0; i < 3; ++i) {
        delete instruments[i];
    }
    return 0;
}
A

继承

B

封装

C

多态

D

链接

3
单选题号 #11000
分值 2

关于以下代码,说法正确的是( )。

class Instrument {
public:
    void play() {
        cout << "乐器在演奏声音" << endl;
    }

    virtual ~Instrument() {}
};

class Piano : public Instrument {
public:
    void play() override {
        cout << "钢琴:叮咚叮咚" << endl;
    }
};

class Guitar : public Instrument {
public:
    void play() override {
        cout << "吉他:咚咚当当" << endl;
    }
};

int main() {
    Instrument* instruments[2];
    instruments[0] = new Piano();
    instruments[1] = new Guitar();

    for (int i = 0; i < 2; ++i) {
        instruments[i]->play();   
    }

    for (int i = 0; i < 3; ++i) {
        delete instruments[i];    
    }
    return 0;
}
A

执⾏代码会输出两⾏,内容分别为:钢琴:叮咚叮咚 和 吉他:咚咚当当

B

执⾏代码会输出两⾏,内容分别为:乐器在演奏声音 和 乐器在演奏声音

C

代码编译出现错误

D

代码运⾏出现错误

4
单选题号 #11001
分值 2

某⽂本编辑器把⽤户输⼊的字符依次压⼊栈 S。⽤户依次输⼊ A、B、C、D 后,⽤户按了两次撤销(每次撤销,弹出栈顶⼀个字符)。此时栈从栈底到栈顶的内容是:( )。

A

A B

B

A B C

C

A B D

D

B C

5
单选题号 #11002
分值 2

假设循环队列数组长度为 NN,其中队空判断条件为:front == rear,队满判断条件为:(rear + 1) % N == front,出队对应的操作为:front = (front + 1) % N,⼊队对应的操作为:rear = (rear + 1) % N。循环队列长度 N = 6,初始 front = 1,rear = 1,执⾏操作序列为:⼊队,⼊队,⼊队,出队,⼊队,⼊队,则最终 (front, rear) 的值是( )。

A

(2, 5)

B

(2, 0)

C

(3, 5)

D

(3, 0)

6
单选题号 #11003
分值 2

以下函数 check() ⽤于判断⼀棵⼆叉树是否为( )。

bool check(TreeNode* root) {
    if (!root) return true;

    queue<TreeNode*> q;
    q.push(root);
    bool hasNull = false;
    while (!q.empty()) {
        TreeNode* cur = q.front(); q.pop();
        if (!cur) {
            hasNull = true;
        } else {
            if (hasNull) return false;
            q.push(cur->left);
            q.push(cur->right);
        }
    }
    return true;
}
A

满⼆叉树

B

完全⼆叉树

C

⼆叉搜索树

D

平衡⼆叉树

7
单选题号 #11004
分值 2

以下代码实现了⼆叉树的( )。

void traverse(TreeNode* root) {
    if (!root) return;
    traverse(root->left);
    traverse(root->right);
    cout << root->val << " ";
}
A

前序遍历

B

中序遍历

C

后序遍历

D

层序遍历

8
单选题号 #11005
分值 2

下⾯代码实现了哈夫曼编码,则横线处应填写的代码是( )。

struct Symbol {
    char ch;            // 字符
    long long freq;     // 频率
    string code;        // 哈夫曼编码
};

struct Node {
    long long w;        // 权值
    int l, r;           // 左右孩子(节点下标),-1 表示空
    int sym;            // 叶子对应符号下标;内部节点为 -1
    Node(long long _w = 0, int _l = -1, int _r = -1, int _sym = -1)
        : w(_w), l(_l), r(_r), sym(_sym) {}
};

// 从 A(leafIdx) 和 B(internalIdx) 的队首取最小的一个节点下标
static int PopMinNode(const vector<Node>& nodes,
                      const vector<int>& leafIdx, int n, int& pA,
                      const vector<int>& internalIdx, int& pB) {
    if (pA < n && (pB >= (int)internalIdx.size() ||
                   nodes[leafIdx[pA]].w <= nodes[internalIdx[pB]].w)) {
        return leafIdx[pA++];
    }
    else {
        return internalIdx[pB++];
    }
}

// DFS 生成编码(左 0,右 1)
static void DFSBuildCodes(int u, const vector<Node>& nodes, Symbol sym[], string& path) {
    if (u == -1) return;

    if (nodes[u].sym != -1) {               // 叶子
        sym[nodes[u].sym].code = path;
        return;
    }

    path.push_back('0');
    DFSBuildCodes(nodes[u].l, nodes, sym, path);
    path.pop_back();

    path.push_back('1');
    DFSBuildCodes(nodes[u].r, nodes, sym, path);
    path.pop_back();
}

int BuildHuffmanCodes(Symbol sym[], int n) {
    for (int i = 0; i < n; i++) sym[i].code.clear();
    if (n <= 0) return -1;

    // 只有一个字符:约定编码为 "0"
    if (n == 1) {
        sym[0].code = "0";
        return 0;
    }

    vector<Node> nodes;
    nodes.reserve(2 * n);

    // 1)建立叶子节点
    vector<int> leafIdx(n);
    for (int i = 0; i < n; i++) {
        leafIdx[i] = (int)nodes.size();
        nodes.push_back(Node(sym[i].freq, -1, -1, i));
    }

    // 2)叶子按权值排序(A 队列)
    sort(leafIdx.begin(), leafIdx.end(),
        [&](int a, int b) {
            if (nodes[a].w != nodes[b].w) return nodes[a].w < nodes[b].w;
            return nodes[a].sym < nodes[b].sym; // 稳定一下
        });

    // B 队列(内部节点下标队列)
    vector<int> internalIdx;
    internalIdx.reserve(n);

    int pA = 0, pB = 0;

    // 3)合并 $n-1$ 次
    for (int k = 1; k < n; k++) {
        int x = PopMinNode(nodes, leafIdx, n, pA, internalIdx, pB);
        int y = PopMinNode(nodes, leafIdx, n, pA, internalIdx, pB);

        int z = (int)nodes.size();
        ________________________     // 在此处填写代码      
    }

    int root = internalIdx.back();

     // 4) DFS 生成编码
    string path;
    DFSBuildCodes(root, nodes, sym, path);
    return root;  
}
A
nodes.push_back(Node(nodes[x].w + nodes[y].w, x, y, -1));
internalIdx.push_back(z);
B
nodes.push_back(Node(nodes[x].w + nodes[y].w, x, y, -1));
leafIdx.push_back(z);
C
internalIdx.push_back(z);
nodes.push_back(Node(nodes[x].w + nodes[y].w, x, y, x+y));
D
nodes.push_back(Node(nodes[x].w + nodes[y].w, x, y, x+y));
leafIdx.push_back(z);
9
单选题号 #11006
分值 2

以下关于哈夫曼编码的说法,正确的是( )。

A

哈夫曼编码是定长编码

B

哈夫曼编码中,没有任何⼀个字符的编码是另⼀个字符编码的前缀

C

哈夫曼编码⼀定唯⼀

D

哈夫曼编码不能⽤于数据压缩

10
单选题号 #11007
分值 2

以下函数实现了⼆叉排序树(BST)的( )操作。

TreeNode* op(TreeNode* root, int x) {
    if (!root) return new TreeNode(x);
    if (x < root->val)
        root->left = op(root->left, x);
    else
        root->right = op(root->right, x);
    return root;
}
A

查找

B

插⼊

C

删除

D

遍历

🔒

已解锁前 10 题

第 11~27 题(共 17 题)需开通题库会员或使用积分包作答
本卷为 VIP 试卷:需开通题库会员(无限刷)或购买积分包(每题 1 积分);已购长期正课学员自动享有权益