理论卷2024年9月GESP等级认证(C++) · 六级
GESP C++ 6级 2024.09
满分 100 · 及格 60 · 时长 60 分钟 · 共 27 题 (单选15 / 判断10 / 编程题2)
本卷含 2 道编程题——可以先到「编程」专栏单独练习: 不计时、不关联卷面分,提交后按测例判分。
试卷阅览 · 免费预览前 10 题 · 交卷后可查看答案与解析
1
单选题号 #12105
分值 2以下( )没有涉及 C++ 语言的面向对象特性支持。
2
单选题号 #12106
分值 2关于以下 C++代码,( )行代码会引起编译错误。
#include <iostream>
using namespace std;
class Base {
private:
int a;
protected:
int b;
public:
int c;
Base() : a(1), b(2), c(3) {}
};
class Derived : public Base {
public:
void show() {
cout << a << endl; // Line 1
cout << b << endl; // Line 2
cout << c << endl; // Line 3
}
};
3
单选题号 #12107
分值 2有 个元素,按照 的顺序进入栈 ,下列( )的出栈序列是不能出现的
4
单选题号 #12108
分值 2采用如下代码实现检查输入的字符串括号是否匹配,横线上应填入的代码为
#include <iostream>
#include <stack>
#include <string>
using namespace std;
bool is_valid(string s) {
stack<char> st;
char top;
for (char& ch : s) {
if (ch == '(' || ch == '{' || ch == '[') {
st.push(ch); // 左括号入栈
}
else
{
if (st.empty())
return false;
———————————————————————— // 在此处填入代码
if ((ch == ')' && top != '(') ||
(ch == '}' && top != '{') ||
(ch == ']' && top != '[')) {
return false;
}
}
}
return st.empty(); // 栈为空则说明所有括号匹配成功
}
5
单选题号 #12109
分值 2下面代码判断队列的第一个元素是否等于 ,并删除该元素,横向上应填写
#include <iostream>
#include <queue>
using namespace std;
bool is_front_equal(std::queue<int>& q, int a) {
bool is_equal = false;
if (!q.empty()) {
———————————————————————— // 在此处填入代码
}
return is_equal;
}
6
单选题号 #12110
分值 2假设字母表 在字符串出现的频率分别为 ,,,,。若使用哈夫曼编码方式对字母进行二进制编码,则字符 分别对应的一组哈夫曼编码的长度分别为( )。
7
单选题号 #12111
分值 2以下C++代码实现 位的格雷码,则横线上应填写
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// 生成 n 位的格雷码
vector<string> generate_graycode(int n) {
vector<string> graycode_list;
if (n <= 0) {
return graycode_list;
}
// 初始1位格雷码
graycode_list.push_back("0");
graycode_list.push_back("1");
// 迭代生成 n 位的格雷码
for (int i = 2; i <= n; i++) {
int current_size = graycode_list.size();
for (int j = current_size - 1; j >= 0; j--) {
graycode_list.push_back("1" + graycode_list[j]);
}
for (int j = 0; j < current_size; j++) {
———————————————————————— // 在此处填入代码
}
}
return graycode_list;
}
8
单选题号 #12112
分值 2给定一棵二叉树,其前序遍历结果为:,中序遍历结果为:,则这棵树的正确后序遍历结果是( )。
9
单选题号 #12113
分值 2一棵有 个结点的完全二叉树用数组进行存储与表示,已知根结点存储在数组的第 个位置。若存储在数组第 个位置的结点存在兄弟结点和两个子结点,则它的兄弟结点和右子结点的位置分别是( )。
10
单选题号 #12114
分值 2二叉树的深度定义为从根结点到叶结点的最长路径上的结点数,则以下基于二叉树的深度优先搜索实现的深度计算函数中横线上应填写
// 定义二叉树的结点结构
struct tree_node {
int val;
tree_node* left;
tree_node* right;
tree_node(int x) : val(x), left(nullptr), right(nullptr) {}
};
// 计算二叉树的深度
int max_depth(tree_node* root) {
if (root == nullptr) {
return 0; // 如果根结点为空,则深度为 0
}
int left_depth = max_depth(root->left);
int right_depth = max_depth(root->right);
———————————————————————— // 在此处填入代码
}
🔒
已解锁前 10 题
第 11~27 题(共 17 题)注册用户即可作答(每题 1 积分)
本卷为普通试卷:注册用户每题扣 1 积分(每日登录送 30 体验积分),交卷后查看答案与解析
