题目编号
语言
全部语言
等级
全部等级
知识点
选择知识点 (0)
找到 1260 道单选题
EXY-SC-1230
第 271 题

关于下面代码,说法错误的是( )。

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 为基类,CircleRectangle 是派生类

C

通过继承,CircleRectangle 复用了 Shape 的属性和方法,并扩展了新的功能

D

CircleRectangle 通过重写(override)基类的虚函数 area 和基类指针,实现了运行时多态

语言: C++
GESP真题 六级
2025.3
单选题号: 15
EXY-SC-1229
第 272 题

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

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++
GESP真题 六级
2025.3
单选题号: 14
EXY-SC-1228
第 273 题

以下代码实现了 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]

语言: C++
GESP真题 六级
2025.3
单选题号: 13
EXY-SC-1227
第 274 题

以下代码用于生成 $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")

语言: C++
GESP真题 六级
2025.3
单选题号: 12
EXY-SC-1226
第 275 题

以下代码实现了二叉树的广度优先搜索(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);
语言: C++
GESP真题 六级
2025.3
单选题号: 11
当前页显示 271 - 275 ,共 1260 道单选题