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

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

  1. 下列关于类的说法,错误的是( )。 {{ select(1) }}
  • 构造函数不能声明为虚函数,但析构函数可以。
  • 函数参数如声明为类的引用类型,调用时不会调用该类的复制构造函数。
  • 静态方法属于类而不是某个具体对象,因此推荐用 类名::方法(...) 调用。
  • 不管基类的析构函数是否是虚函数,都可以通过基类指针/引用正确删除派生类对象。
  1. 假设变量 veh 是类 Car 的一个实例,我们可以调用 veh.move() ,是因为面向对象编程有( )性质。
class Vehicle {
private:
    string brand;
public:
    Vehicle(string b) : brand(b) {}
    void setBrand(const string& b) { brand = b; }
    string getBrand() const { return brand; }
    void move() const {
        cout << brand << " is moving..." << endl;
    }
};

class Car : public Vehicle {
private:
    int seatCount;
public:
    Car(string b, int seats) : Vehicle(b), seatCount(seats) {}
    void showInfo() const {
        cout << "This car is a " << getBrand()
             << " with " << seatCount << " seats." << endl;
    }
};

{{ select(2) }}

  • 继承 (Inheritance)
  • 封装 (Encapsulation)
  • 多态 (Polymorphism)
  • 链接 (Linking)
  1. 下面代码中 v1v2 调用了相同接口 move() ,但输出结果不同,这体现了面向对象编程的( )特性。
class Vehicle {
private:
    string brand;
public:
    Vehicle(string b) : brand(b) {}
    void setBrand(const string& b) { brand = b; }
    string getBrand() const { return brand; }
    virtual void move() const {
        cout << brand << " is moving..." << endl;
    }
};

class Car : public Vehicle {
private:
    int seatCount;
public:
    Car(string b, int seats) : Vehicle(b), seatCount(seats) {}
    void showInfo() const {
        cout << "This car is a " << getBrand()
             << " with " << seatCount << " seats." << endl;
    }
    void move() const override {
        cout << getBrand() << " car is driving on the road!" << endl;
    }
};

class Bike : public Vehicle {
public:
    Bike(string b) : Vehicle(b) {}
    void move() const override {
        cout << getBrand() << " bike is cycling on the path!" << endl;
    }
};

int main() {
    Vehicle* v1 = new Car("Toyota", 5);
    Vehicle* v2 = new Bike("Giant");
    v1->move();
    v2->move();
    delete v1;
    delete v2;
    return 0;
}

{{ select(3) }}

  • 继承 (Inheritance)
  • 封装 (Encapsulation)
  • 多态 (Polymorphism)
  • 链接 (Linking)
  1. 栈的操作特点是( )。 {{ select(4) }}
  • 先进先出
  • 先进后出
  • 随机访问
  • 双端进出
  1. 循环队列常用于实现数据缓冲。假设一个循环队列容量为 5 (即最多存放 4 个元素,留一个位置区分空与满),依次进行操作:入队数据1,2,3,出队1个数据,再入队数据4和5,此时队首到队尾的元素顺序是( )。 {{ select(5) }}
  • [2, 3, 4, 5]
  • [1, 2, 3, 4]
  • [3, 4, 5, 2]
  • [2, 3, 5, 4]
  1. 以下函数 createTree() 构造的树是什么类型?
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

TreeNode* createTree() {
    TreeNode* root = new TreeNode(1);
    root->left = new TreeNode(2);
    root->right = new TreeNode(3);
    root->left->left = new TreeNode(4);
    root->left->right = new TreeNode(5);
    return root;
}

{{ select(6) }}

  • 满二叉树
  • 完全二叉树
  • 二叉排序树
  • 其他都不对
  1. 已知二叉树的 中序遍历 是 [D, B, E, A, F, C],先序遍历 是 [A, B, D, E, C, F]。请问该二叉树的后序遍历结果是( )。 {{ select(7) }}
  • [D, E, B, F, C, A]
  • [D, B, E, F, C, A]
  • [D, E, B, C, F, A]
  • [B, D, E, F, C, A]
  1. 完全二叉树可以用数组连续高效存储,如果节点从 1 开始编号,则对有两个孩子节点的节点 ii ,( )。 {{ select(8) }}
  • 左孩子位于 2i2i ,右孩子位于 2i+12i+1
  • 完全二叉树的叶子节点可以出现在最后一层的任意位置
  • 所有节点都有两个孩子
  • 左孩子位于 2i+12i+1 ,右孩子位于 2i+22i+2
  1. 设有字符集 {a, b, c, d, e, f} ,其出现频率分别为 {5, 9, 12, 13, 16, 45} 。哈夫曼算法构造最优前缀编码,以下哪一组可能是对应的哈夫曼编码?(非叶子节点左边分支记作 0,右边分支记作 1,左右互换不影响正确性)。 {{ select(9) }}
  • a: 00b: 01c: 10d: 110e: 111f: 0
  • a: 1100b: 1101c: 100d: 101e: 111f: 0
  • a: 000b: 001c: 01d: 10e: 110f: 111
  • a: 10b: 01c: 100d: 101e: 111f: 0
  1. 下面代码生成格雷编码,则横线上应填写( )。
vector<string> grayCode(int n) {
    if (n == 0) return {"0"};
    if (n == 1) return {"0", "1"};
    vector<string> prev = grayCode(n-1);
    vector<string> result;
    for (string s : prev) {
        result.push_back("0" + s);
    }
    for (_______________) { // 在此处填写代码
        result.push_back("1" + prev[i]);
    }
    return result;
}

{{ select(10) }}

  • int i = 0; i < prev.size(); i++
  • int i = prev.size()-1; i >= 0; i--
  • auto s : prev
  • int i = prev.size()/2; i < prev.size(); i++
  1. 请将下列树的深度优先遍历代码补充完整,横线处应填入( )。
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x): val(x), left(nullptr), right(nullptr) {}
};

void dfs(TreeNode* root) {
    if (!root) return;
    ______<TreeNode*> temp; // 在此处填写代码
    temp.push(root);
    while (!temp.empty()) {
        TreeNode* node = temp.top();
        temp.pop();
        cout << node->val << " ";
        if (node->right) temp.push(node->right);
        if (node->left) temp.push(node->left);
    }
}

{{ select(11) }}

  • vector
  • list
  • queue
  • stack
  1. nn 是树的节点数目,下列代码实现了树的广度优先遍历,其时间复杂度是( )。
void bfs(TreeNode* root) {
    if (!root) return;
    queue<TreeNode*> q;
    q.push(root);
    while (!q.empty()) {
        TreeNode* node = q.front();
        q.pop();
        cout << node->val << " ";
        if (node->left) q.push(node->left);
        if (node->right) q.push(node->right);
    }
}

{{ select(12) }}

  • O(n)O(n)
  • O(logn)O(\log n)
  • O(n2)O(n^2)
  • O(2n)O(2^ n)
  1. 在二叉排序树(Binary Search Tree, BST)中查找元素 50 ,从根节点开始:若根值为 60 ,则下一步应去搜索:

{{ select(13) }}

  • 左子树
  • 右子树
  • 随机
  • 根节点
  1. 删除二叉排序树中的节点时,如果节点有两个孩子,则横线处应填入( ),其中 findMaxfindMin 分别为寻找树的最大值和最小值的函数。
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x): val(x), left(nullptr), right(nullptr) {}
};

TreeNode* deleteNode(TreeNode* root, int key) {
    if (!root) return nullptr;
    if (key < root->val) {
        root->left = deleteNode(root->left, key);
    }
    else if (key > root->val) {
        root->right = deleteNode(root->right, key);
    }
    else {
        if (!root->left) return root->right;
        if (!root->right) return root->left;
        TreeNode* temp = ____________; // 在此处填写代码
        root->val = temp->val;
        root->right = deleteNode(root->right, temp->val);
    }
    return root;
}

{{ select(14) }}

  • root->left
  • root->right
  • findMin(root->right)
  • findMax(root->left)
  1. 给定 nn 个物品和一个最大承重为 WW 的背包,每个物品有一个重量 wt[i]wt[i] 和价值 val[i]val[i] ,每个物品只能选择放或不放。目标是选择若干个物品放入背包,使得总价值最大,且总重量不超过 WW ,则横线上应填写( )。
int knapsack(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) {
            ________________________ // 在此处填写代码
        }
    }
    return dp[W];
}

{{ select(15) }}

  • dp[w] = max(dp[w], dp[w] + val[i]);
  • dp[w] = dp[w - wt[i]] + val[i];
  • dp[w] = max(dp[w - 1], dp[w - wt[i]] + val[i]);
  • dp[w] = max(dp[w], dp[w - wt[i]] + val[i]);