全部来自 AI 原创生成并经逐题审校的题目,带 精品 标记的为 AI 复审通过。整卷生成请去组卷。
语句 cout << setw(6) << 42; 执行后(数值按右对齐输出),输出的结果中 42 前面共有几个空格? A. 2 个 B. 3 个 C. 4 个 D. 5 个
登录后查看答案
有如下C++代码: vector<int> v; for(int i=1;i<=5;i++) v.push_back(i); vector<int>::iterator it = v.begin(); v.insert(v.begin(), 0); cout << *it << endl; 若向量为空时 capacity 为 0,插入已存在元素时容量不足则重新分配存储,原迭代器全部失效。执行后输出结果是什么? A. 1 B. 0 C. 程序行为未定义 D. 编译错误
设类 V 的成员函数为 `V& operator=(const V& rhs) { value = rhs.value + 1; return *this; }`,且构造函数 `V(int x)` 将 x 存入成员 value。执行 `V a(0), b(0), c(4); a = b = c;` 后,按 (a.value, b.value, c.value) 排列的结果是: A. (5, 4, 4) B. (6, 5, 4) C. (4, 5, 6) D. (6, 6, 4)
阅读以下声明: class Base { public: virtual void render() const = 0; }; class Mid : public Base { public: void render() {} }; class Leaf : public Mid { public: void render() const override {} }; 下列判断正确的是: A. Base 与 Mid 是抽象类,Leaf 不是抽象类 B. Base 是抽象类,Mid 与 Leaf 都不是抽象类 C. Base、Mid 与 Leaf 都是抽象类 D. Base 不是抽象类,Mid 与 Leaf 是抽象类
有如下类模板定义:template<typename T> class Box { T data; public: Box(T d):data(d){} T get(){return data;} }; 下列关于该类模板使用的语句中,正确的是( ) A. Box b(10); B. Box<int> b(10); b.get() 返回 int 型数据 C. class Box<int> b(10); D. Box<double> b(10); b.get() 返回 int 型数据
在 C++ 中,若要将加法运算符重载为类 Complex 的友元函数,使其支持 c1 + c2(c1、c2 均为 Complex 对象),下列函数声明正确的是 A. friend Complex operator+(Complex &a, Complex &b); B. Complex operator+(Complex &a); C. Complex operator+(Complex a, Complex b); D. friend Complex add(Complex &a, Complex &b);
已知 `struct Base { int value; }; struct Derived : Base { int value; };`,并有对象 `Derived d;`。在 `Derived` 中同名成员隐藏了 `Base` 的 `value`。若要通过 `d` 明确访问被隐藏的基类成员,应选哪一项? A. `d.value` B. `d.Base::value` C. `d.Derived::value` D. `Base::value`
已知类 Base 中定义了成员函数 void show(),派生类 Derived 中又定义了同名成员函数 void show()(参数相同)。在 Derived 的成员函数中直接写 show(),调用的是哪一个函数? A. Base 中的 show() B. Derived 中的 show() C. 两个都会被调用 D. 编译报错,必须写 Base::show()
阅读代码,判断在类外函数中,哪些语句可以通过对象 v 合法执行。 class Vault { int code; public: void set(int x) { code = x; } int read() const { return code; } private: void reset() { code = 0; } public: void clear() { reset(); } }; Vault v; ① v.set(7); ② v.read(); ③ v.code; ④ v.reset(); ⑤ v.clear(); A. ①②⑤ B. ①②④⑤ C. ①③⑤ D. ②③④
按标准 C++ 规则,执行下列语句后,变量 d 的值是多少? int i = -2.8; double d = i + 4.25; A. 1.25 B. 2.25 C. 2.45 D. 3.25
某日志分析程序执行以下语句序列:cin >> key; cout << key; cerr << "错误"; clog << "日志"。已知 cerr 不使用缓冲区(内容立即送达标准错误设备),clog 使用缓冲区。下列关于这四个流对象的说法,唯一正确的是( ) A. cin 和 cout 均默认关联标准输出设备 B. cerr 与 clog 都关联标准错误设备,区别仅在是否带缓冲 C. clog 因无缓冲而最适于即时报错 D. cout 与 cerr 输出内容必然按语句先后顺序显示在同一设备上
下列 4 条 C++ 变量定义与初始化语句中,编译无法通过(即存在错误)的共有几条? ① int a = 3.9; ② const int b; ③ int c{3.9}; ④ double d{3}; A. 1 条 B. 2 条 C. 3 条 D. 4 条
设有如下定义: struct Tick { int n; Tick(int x) : n(x) {} Tick& operator++() { ++n; return *this; } Tick operator++(int) { Tick old = *this; ++n; return old; } int value() const { return n; } }; 执行 Tick t(6); int a=(t++).value(); int b=(++t).value(); 后,(a,b,t.value()) 的值是: A. (6,7,7) B. (6,8,8) C. (7,8,8) D. (6,7,8)
阅读程序: ```cpp #include <iostream> using namespace std; class Item { char name; public: Item(char n) : name(n) { cout << '+' << name; } ~Item() { cout << '-' << name; } }; int main() { Item a('a'); { Item b('b'); Item c('c'); } Item d('d'); } ``` 程序运行时输出的字符序列是什么? A. +a+b+c-c-b+d-d-a B. +a+b+c-b-c+d-d-a C. +a+b+c-c-b+d-a-d D. +a+b+c-c-b-d+d-a
阅读代码,a.value() 在内层作用域结束后返回什么? class Item { int n; static int alive; public: Item(int x) : n(x) { ++alive; } ~Item() { --alive; } int value() const { return this->n + alive; } }; int Item::alive = 0; int main() { Item a(6); { Item b(9); } // 此处调用 a.value() } A. 6 B. 7 C. 8 D. 9
有如下 C++ 代码段: int a = 5, b = 8; int *p = &a, *q = &b; *p = *q; q = p; *q = 12; 执行完该代码段后,变量 a 和 b 的值分别为( ) A. a=12,b=8 B. a=12,b=12 C. a=5,b=12 D. a=8,b=12
阅读下列程序,判断正确说法。(假设已包含 <iostream>,并使用 std 命名空间) inline int token(); int main() { std::cout << token(); } inline int token() { return 7; } A. 程序不能通过编译,因为调用前只有函数原型 B. 程序能通过编译并输出 7;函数原型允许先调用后定义,inline 不保证调用处一定展开 C. 程序能通过编译并输出 7;函数原型本身已定义了 token D. 程序不能通过编译,因为 inline 函数的定义必须出现在函数原型之前
已有变量 `int x = 8;`。若要定义 `r`,使其成为 `x` 的引用,并且之后可通过给 `r` 赋值来修改 `x`,应选择哪项? A. `int& r = x;` B. `int r = x;` C. `const int& r = x;` D. `int& r;`
下列关于指针与 const 的声明,哪一项表示“指针所指对象不能通过该指针修改,但指针本身可以改为指向其他对象”? A. const int* p B. int* const p C. const int* const p D. int& p
有如下 C++ 递归函数:int f(int n){ if(n<=0) return 1; return n*f(n-2); } 则调用 f(7) 的返回值是:A. 5040 B. 105 C. 945 D. 16