首页 › 题库 › GESP编程能力等级认证 › GESP-C++六级

GESP-C++六级 题库

共 270 道题 ,第 3 / 14 页 · 按题型筛选,点「查看答案」核对解析
41 单选题 适中 GESP编程能力等级认证 ·GESP-C++六级 · 3.1 深度优先搜索DFS

请将下列 C++ 实现的深度优先搜索(DFS)代码补充完整,横线处应填入?( )

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x): val(x), left(nullptr), right(nullptr) {}
};
void dfs(TreeNode* root, vector<int>& result) {
    if (root == nullptr) return;
    __________________________
}
A
result.push_back(root->val);
dfs(root->left);
dfs(root->right);
B
result.push_back(root->left->val);
dfs(root->right);
dfs(root->left);
C
result.push_back(root->left->val);
dfs(root->left);
dfs(root->right);
D
result.push_back(root->right->val);
dfs(root->right);
dfs(root->left);
答案与解析
正确答案:A
这道题暂未补解析。
题目详情
42 单选题 适中 GESP编程能力等级认证 ·GESP-C++六级 · 1.1 树的基本概念

给定一个二叉树,返回每一层中最大的节点值,结果以数组形式返回,横线处应填入?( )

#include <vector>
#include <queue>
#include <algorithm>
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x): val(x), left(nullptr), right(nullptr) {}
};
vector<int> largestValues(TreeNode* root) {
    vector<int> result;
    if (!root) return result;
    queue<TreeNode*> q;
    q.push(root);
    while (!q.empty()) {
        int sz = q.size();
        int maxVal = INT_MIN;
        for (int i = 0; i < sz; ++i) {
            TreeNode* node;
            _______________________________
            maxVal = max(maxVal, node->val);
            if (node->left) q.push(node->left);
            if (node->right) q.push(node->right);
        }
        result.push_back(maxVal);
    }
    return result;
}
A
node = q.end();
B
node = q.front();
C
q.pop();node = q.front();
D
node = q.front();q.pop();
答案与解析
正确答案:D
这道题暂未补解析。
题目详情
43 单选题 适中 GESP编程能力等级认证 ·GESP-C++六级 · 1.1 树的基本概念

下面代码实现一个二叉排序树的插入函数(没有相同的数值),横线处应填入?( )

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x): val(x), left(nullptr), right(nullptr) {}
};
void insert(TreeNode*& root, int key) {
    if (!root) {
        root = new TreeNode(key);
        return;
    }
    _______________________________
}
A
if (key < root->val)
    insert(root->left, key);
else if (key > root->val)
    insert(root->right, key);
B
if (key < root->val)
    insert(root->right, key);
else if (key > root->val)
    insert(root->left, key);
C
insert(root->left, key);
insert(root->right, key);
D
insert(root->right, key);
insert(root->left, key);
答案与解析
正确答案:A
这道题暂未补解析。
题目详情
44 单选题 适中 GESP编程能力等级认证 ·GESP-C++六级 · 4.1 一维动态规划

以下关于动态规划算法特性的描述,正确的是?( )

A
子问题相互独立,不重叠
B
问题包含重叠子问题和最优子结构
C
只能从底至顶迭代求解
D
必须使用递归实现,不能使用迭代
答案与解析
正确答案:B
这道题暂未补解析。
题目详情
45 单选题 适中 GESP编程能力等级认证 ·GESP-C++六级 · 4.1 一维动态规划

给定 个物品和一个最大承重为 W 的背包,每个物品有一个重量 wt[i] 和价值 val[i],每个物品只能选择放或不放。

目标是选择若干个物品放入背包,使得总价值最大,且总重量不超过 W。关于下面代码,说法正确的是?( )

int knapsack1D(int W, vector<int>& wt, vector<int>& val, int n) {
    vector<int> dp(W+1, 0);
    for (int i = 0; i < n; ++i) {
        for (int w = W; w >= wt[i]; --w) {
            dp[w] = max(dp[w], dp[w - wt[i]] + val[i]);
        }
    }
    return dp[W];
}
A
该算法不能处理背包容量为 0 的情况
B
外层循环 i 遍历背包容量,内层遍历物品
C
从大到小遍历 w 是为了避免重复使用同一物品
D
这段代码计算的是最小重量而非最大价值
答案与解析
正确答案:C
这道题暂未补解析。
题目详情
46 单选题 适中 GESP编程能力等级认证 ·GESP-C++六级 · 5.1 面向对象思想

在面向对象编程中,类是一种重要的概念。下面关于类的描述中,【不正确】的是?( )

A
类是一个抽象的概念,用于描述具有相同属性和行为的对象集合。
B
类可以包含属性和方法,属性用于描述对象的状态,方法用于描述对象的行为。
C
类可以被实例化,生成具体的对象。
D
类一旦定义后,其属性和方法不能被修改或扩展。
答案与解析
正确答案:D

选项D错误,类的属性和方法可以修改或扩展(如通过继承、补丁等)。

题目详情
47 单选题 适中 GESP编程能力等级认证 ·GESP-C++六级 · 1.2 哈夫曼树

哈夫曼编码是一种数据压缩算法。以下关于哈夫曼编码的描述中,【不正确】的是?( )

A
哈夫曼编码是一种变长编码,频率高的字符使用较短的编码,频率低的字符使用较长的编码。
B
在构造哈夫曼树时,频率越低的字符离根节点越近,频率越高的字符离根节点越远。
C
哈夫曼编码的生成过程基于贪心算法,每次选择频率最低的两个节点进行合并。
D
哈夫曼编码是一种前缀编码,任何一个字符的编码都不会是另一个字符编码的前缀,因此可以实现唯一解码。
答案与解析
正确答案:B

哈夫曼编码是一种基于字符出现频率的无损数据压缩算法,其核心思想是:

变长编码:频率高的字符用较短的编码,频率低的字符用较长的编码。

前缀编码:任何字符的编码都不是其他字符编码的前缀,确保解码唯一性。

贪心算法:通过合并频率最低的节点构建哈夫曼树。

选项B错误,哈夫曼树的构造规则是:频率越低的字符离根节点越远(路径更长),频率越高的字符离根节点越近(路径更短)。

题目详情
48 单选题 适中 GESP编程能力等级认证 ·GESP-C++六级 · 1.1 树的基本概念

以下代码实现了树的哪种遍历方式?( )

void traverse(TreeNode* root) {
    if (root == nullptr) return;
    cout << root->val << " ";
    traverse(root->left);
    traverse(root->right);
}
A
前序遍历
B
中序遍历
C
后序遍历
D
层次遍历
答案与解析
正确答案:A

树的遍历主要方式:

前序遍历:访问顺序:根节点 → 左子树 → 右子树。

中序遍历:访问顺序:左子树 → 根节点 → 右子树。

后序遍历:访问顺序:左子树 → 右子树 → 根节点。

这段代码实现了树的 前序遍历(根→左→右)。

题目详情
49 单选题 适中 GESP编程能力等级认证 ·GESP-C++六级 · 1.1 树的基本概念

以下关于完全二叉树的代码描述,正确的是?( )

bool isCompleteTree(TreeNode* root) {
    if (root == nullptr) return true;
    queue<TreeNode*> q;
    q.push(root);
    bool hasNull = false;
    while (!q.empty()) {
        TreeNode* node = q.front();
        q.pop();
        if (node == nullptr) {
            hasNull = true;
        } else {
            if (hasNull) return false;
            q.push(node->left);
            q.push(node->right);
        }
    }
    return true;
}
A
该代码用于判断一棵树是否为满二叉树
B
该代码用于判断一棵树是否为完全二叉树
C
该代码用于判断一棵树是否为二叉搜索树
D
该代码用于计算树的高度
答案与解析
正确答案:B

使用队列 q 进行层序遍历,hasNull 标记是否遇到过空节点(初始为 false)。

遍历过程:

每次从队列中取出一个节点 node。

如果 node 是 nullptr:设置 hasNull = true。

如果 node 非空:若之前已遇到空节点(hasNull == true),则直接返回 false(违反完全二叉树的定义)。

否则,将 node 的左右子节点(无论是否为空)加入队列。

代码实现了 完全二叉树的判断逻辑。

题目详情
50 单选题 适中 GESP编程能力等级认证 ·GESP-C++六级 · 1.1 树的基本概念

以下代码实现了二叉排序树的哪种操作?( )

TreeNode* op(TreeNode* root, int val) {
    if (root == nullptr) return new TreeNode(val);
    if (val < root->val) {
        root->left = op(root->left, val);
    } else {
        root->right = op(root->right, val);
    }
    return root;
}
A
查找
B
插入
C
删除
D
遍历
答案与解析
正确答案:B

程序中:

if (root == nullptr) return new TreeNode(val)

若目标值不存在,则在叶子节点位置创建新节点。

若目标值更小,递归查找左子树;若目标值更大,递归查找右子树。

这段代码通过递归查找插入位置并动态创建新节点,实现了 二叉排序树的插入操作。

题目详情
51 单选题 适中 GESP编程能力等级认证 ·GESP-C++六级 · 1.2 哈夫曼树

给定字符集 {A, B, C, D} 的出现频率分别为 {5, 1, 6, 2} ,则正确的哈夫曼编码是?( )

A
A: 0, B: 100, C: 11, D: 101
B
A: 11, B: 100, C: 0, D: 101
C
A: 0, B: 101, C: 11, D: 100
D
A: 10, B: 101, C: 0, D: 100
答案与解析
正确答案:B
这道题暂未补解析。
题目详情
52 单选题 适中 GESP编程能力等级认证 ·GESP-C++六级 · 4.1 一维动态规划

关于动态规划的描述,正确的是?( )

A
动态规划算法的时间复杂度总是低于贪心算法。
B
动态规划要求问题必须具有最优子结构和重叠子问题两个性质。
C
动态规划通过递归实现时不需要存储中间结果。
D
动态规划的核心思想是将问题分解为互不重叠的子问题。
答案与解析
正确答案:B

选项B正确,动态规划问题的两个必要条件:

最优子结构确保问题可分解,重叠子问题使得记忆化有意义。

题目详情
53 单选题 适中 GESP编程能力等级认证 ·GESP-C++六级 · 5.1 面向对象思想

以下代码中,类的构造函数被调用了( )次?

class MyClass {
    public:
        MyClass() {
            cout << "Constructor called!" << endl;
        }
};
int main() {
    MyClass obj1;
    MyClass obj2 = obj1;
    return 0;
}
A
1
B
2
C
3
D
0
答案与解析
正确答案:A

MyClass obj1:调用 默认构造函数,输出 "Constructor called!"。

MyClass obj2 = obj1,调用的是隐式拷贝构造函数,不会触发默认构造函数。

因此 构造函数仅被调用 1 次。

题目详情
54 单选题 适中 GESP编程能力等级认证 ·GESP-C++六级 · 5.1 面向对象思想

以下代码实现了循环队列的哪种操作?( )

class CircularQueue {
        int* arr;
        int front, rear, size;
    public:
        CircularQueue(int k) {
            size = k;
            arr = new int[k];
            front = rear = -1;
        }
        bool enQueue(int value) {
            if (isFull()) return false;
            if (isEmpty()) front = 0;
            rear = (rear + 1) % size;
            arr[rear] = value;
            return true;
        }
};
A
入队
B
出队
C
查看队首元素
D
判断队列是否为空
答案与解析
正确答案:A

enQueue()函数(入队):

首先检查队列是否已满,如果已满则返回 false,表示入队失败。

如果队列为空(isEmpty()),初始化 front 为 0。

更新 rear 指针:rear = (rear + 1) % size,循环队列利用取模实现环形移动。

将新值 value 存入 arr[rear]。

返回 true 表示入队成功。

题目详情
55 单选题 适中 GESP编程能力等级认证 ·GESP-C++六级 · 1.1 树的基本概念

以下代码实现了二叉树的深度优先搜索(DFS),并统计叶子结点的数量,则横线上应填写?( )

int countLeafNodes(TreeNode* root) {
    if (root == nullptr) return 0;
    stack<TreeNode*> s;
    s.push(root);
    int count = 0;
    while (!s.empty()) {
        TreeNode* node = s.top();
        s.pop();
        if (node->left == nullptr && node->right == nullptr) {
            count++;
        }
        if (node->right) s.push(node->right);
        _____________________________________ // 在此处填入代码
    }
    return count;
}
A
if (node->left) s.push(node->left);
B
if (node->left) s.pop(node->left);
C
if (node->left) s.front(node->left);
D
if (node->left) s.push(node->right);
答案与解析
正确答案:A

空缺的位置在 while 循环的末尾,当前代码已经处理了右子树的压栈操作,需要补充左子树的处理逻辑。

程序使用栈(stack<TreeNode*>)来实现 DFS,空缺部分应该是一个类似的逻辑:如果 node 有左子节点,则将其压入栈。

本题答案为选项A。

题目详情
56 单选题 适中 GESP编程能力等级认证 ·GESP-C++六级 · 1.1 树的基本概念

以下代码实现了二叉树的广度优先搜索(BFS),并查找特定值的节点,则横线上应填写?( )

TreeNode* findNode(TreeNode* root, int target) {
    if (root == nullptr) return nullptr;
    queue<TreeNode*> q;
    q.push(root);
    while (!q.empty()) {
        TreeNode* current = q.front();
        q.pop();
        if (current->val == target) {
            return current; // 找到目标节点
        }
        ___________________________________ // 在此处填入代码
    }
    return nullptr; // 未找到目标节点
}
A
if (current->left) q.push(current->left);if (current->right) q.push(current->right);
B
if (current->left) q.pop(current->left);if (current->right) q.pop(current->right);
C
if (current->left) q.front(current->left);if (current->right) q.front(current->right);
D
if (current->left) q.push(current->right);if (current->right) q.push(current->left);
答案与解析
正确答案:A

BFS 遍历:使用队列(queue)来实现 BFS。

初始时,将根节点 root 入队。

在 while 循环中,取出队首节点 current:

如果 current->val == target,则返回 current(找到目标节点)。

空缺部分需要将 current 的子节点(左子节点和右子节点)入队。

本题答案为选项A。

题目详情
57 单选题 适中 GESP编程能力等级认证 ·GESP-C++六级 · 2.1 格雷编码

以下代码用于生成 n 位格雷编码。横线上应填写?( )

vector<string> generateGrayCode(int n) {
    if (n == 0) return {"0"};
    if (n == 1) return {"0", "1"};
    vector<string> prev = generateGrayCode(n - 1);
    vector<string> result;
    for (string s : prev) {
        result.push_back("0" + s); // 在前缀添加 0
    }
    for (int i = prev.size() - 1; i >= 0; i--) {
        __________________________________ // 在此处填入代码
    }
    return result;
}
A
result.push_back("1" + prev[i]);
B
result.push_back("0" + prev[i]);
C
result.push_back(prev[i] + "1");
D
result.push_back(prev[i] + "0");
答案与解析
正确答案:A

格雷码的特点是:

相邻两个编码只有 1 位不同。

可以通过递归方式生成:n 位格雷码 = "0" + (n-1 位格雷码) + "1" + (n-1 位格雷码的逆序)。

前半部分:在 prev 的每个编码前加 "0"("0" + s)。

后半部分:在 prev 的 逆序 每个编码前加 "1"("1" + prev[i])。

本题答案为选项A。

题目详情
58 单选题 适中 GESP编程能力等级认证 ·GESP-C++六级 · 4.1 一维动态规划

以下代码实现了0/1背包问题的动态规划解法。假设物品重量为 weights[] ,价值为 values[] ,背包容量为 W ,横线上应填写?( )

int knapsack(int W, vector<int>& weights, vector<int>& values) {
    int n = weights.size();
    vector<vector<int>> dp(n + 1, vector<int>(W + 1, 0));
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= W; j++) {
            if (weights[i-1] > j) {
                dp[i][j] = dp[i-1][j]; // 当前物品装不下
            } else {
                dp[i][j] = max(_________________________); // 在此处填入代码
            }
        }
    }
    return dp[n][W];
}
A
dp[i-1][j], values[i-1]
B
dp[i-1][j], dp[i-1][j - weights[i-1]] + values[i-1]
C
dp[i][j-1], values[i-1]
D
dp[i-1][j - weights[i-1]] + values[i-1], dp[i][j-1]
答案与解析
正确答案:B
这道题暂未补解析。
题目详情
59 单选题 适中 GESP编程能力等级认证 ·GESP-C++六级 · 6.1 栈

以下代码用于检查字符串中的括号是否匹配,横线上应填写?( )

bool isBalanced(string s) {
    stack<char> st;
    for (char c : s) {
        if (c == '(' || c == '[' || c == '{') {
            st.push(c);
        } else {
            if (st.empty()) return false; // 无左括号匹配
            char top = st.top();
            st.pop();
            if ((c == ')' && top != '(') ||
                    (c == ']' && top != '[') ||
                    (c == '}' && top != '{')) {
                return false;
            }
        }
    }
    return ________________; //在此处填入代码
}
A
true
B
false
C
st.empty()
D
!st.empty()
答案与解析
正确答案:C
这道题暂未补解析。
题目详情
60 单选题 适中 GESP编程能力等级认证 ·GESP-C++六级 · 5.1 面向对象思想

关于下面代码,说法【错误】的是?( )

class Shape {
    protected:
        string name;
    public:
        Shape(const string& n) : name(n) {}
        virtual double area() const {
            return 0.0;
        }
};
class Circle : public Shape {
    private:
        double radius; // 半径
    public:
        Circle(const string& n, double r) : Shape(n), radius(r) {}
        double area() const override {
            return 3.14159 * radius * radius;
        }
};
class Rectangle : public Shape {
    private:
        double width; // 宽度
        double height; // 高度
    public:
        Rectangle(const string& n, double w, double h) : Shape(n), width(w), height(h) {
        }
        double area() const override {
            return width * height;
        }
};
int main() {
    Circle circle("MyCircle", 5.0);
    Rectangle rectangle("MyRectangle", 4.0, 6.0);
    Shape* shapePtr = &circle;
    cout << "Area: " << shapePtr->area() << endl;
    shapePtr = &rectangle;
    cout << "Area: " << shapePtr->area() << endl;
    return 0;
}
A
语句 Shape* shapePtr = &circle; 和 shapePtr = &rectangle; 出现编译错误
B
Shape 为基类, Circle 和 Rectangle 是派生类
C
通过继承, Circle 和 Rectangle 复用了 Shape 的属性和方法,并扩展了新的功能
D
Circle 和 Rectangle 通过重写(override)基类的虚函数 area 和基类指针,实现了运行时多态
答案与解析
正确答案:A
这道题暂未补解析。
题目详情