Skip to content

选择题

C++ 概述

Which programming paradigm below is not well supported in C++?

A. Object-oriented programming

B. Procedural programming

C. Declarative programming

D. Generic programming

答案

C

解析

C++ 是一门多范式语言,但它主要支持以下范式:

  • 面向对象编程(A):通过类、继承、多态、封装等全面支持。
  • 过程式编程(B):完全兼容 C 风格,函数、指针、结构体等。
  • 泛型编程(D):通过模板(函数模板、类模板)、概念(C++20)提供强大的泛型支持。

声明式编程(如逻辑编程 Prolog、纯函数式 Haskell、SQL 查询)的核心是描述“做什么”而非“怎么做”。C++ 本质上是一门命令式语言,控制流由显式的语句管理,没有内建的逻辑推导、惰性求值、纯函数组合等声明式机制,因此声明式范式不是它原生支持的。

继承

Which of the following sentences is not correct?

A. Derived class object can be assigned to Base class object.

B. Base class object can be assigned to Derived class object.

C. Derived class object pointer can be assigned to Base class object pointer.

D. Base class object pointer can be assigned to Derived class object pointer.

答案

D

解析
  • A 正确:允许对象切片(slicing),派生类对象中的基类部分会被复制给基类对象。
  • B 正确:why?
  • C 正确:派生类指针可以隐式转换为基类指针(向上转型),这是安全的。
  • D 错误:基类指针不能隐式转换为派生类指针(向下转型),直接赋值会导致编译错误,只能使用显式强制转换。
纯虚函数和抽象类

关于纯虚函数和抽象类的描述中,哪个是错误的?

A. 纯虚函数是一种特殊的虚函数,它没有具体的实现

B. 抽象类是指具有纯虚函数的类

C. 一个基类中说明有纯虚函数,该基类的派生类一定不再是抽象类

D. 抽象类只能作为基类来使用,其纯虚函数的实现由派生类给出

答案

C

解析
  • A 正确:纯虚函数在基类中通过 = 0 声明,通常没有实现(虽然可以给出默认实现,但调用必须通过派生类,且抽象类本身不提供完整实现)。
  • B 正确:抽象类的定义就是包含至少一个纯虚函数的类(无论是自己声明的还是继承的未覆盖的纯虚函数)。
  • C 错误:如果派生类没有实现基类的所有纯虚函数,那么它依然包含纯虚函数,从而仍然是抽象类,不能实例化。
  • D 正确:抽象类不能实例化对象,只能作为基类,其纯虚函数的实现由派生类负责给出。
mallocnew

Among the following statements about new and malloc, which one is correct?

A. new returns a void * pointer, which needs to be typecasted to the appropriate type.

B. Both new and malloc call an appropriate constructor for object allocation.

C. None of the other options is correct.

D. new and malloc are both operators.

答案

C

解析
  • A 错误:new 返回对应类型指针,无需强制转换;malloc 才返回 void* 需要强转。
  • B 错误:只有 new 会调用构造函数,malloc 仅分配原始内存,不会调用构造函数。
  • D 错误:new 是运算符,malloc 是库函数。
拷贝构造函数

When a copy constructor is called?

A. When an object of the class is returned by value

B. When an object of the class is passed by value to a function

C. When an object is constructed based on another object of the same class

D. All of the mentioned

答案

D

解析

拷贝构造函数在以下情况都会被调用:

  • 按值传递对象给函数时:形参通过拷贝构造函数从实参初始化。
  • 从函数按值返回对象时:返回的对象通过拷贝构造函数初始化(即使编译器可能会做返回值优化)。
  • 基于同类的另一个对象构造新对象时:直接调用拷贝构造函数。

How many times is the destructor of class A called during execution?

#include <iostream>
using namespace std;

class A {
public:
    A() { cout << "A()" << endl; }
    ~A() { cout << "~A()" << endl; }
};

void foo(A a) {
    A arr[5];
    A *p = new A[3];
    throw p;
}

int main() {
    try {
        A a;
        foo(a);
    } catch (A *p) {
        delete[] p;
        cout << "catched" << endl;
    }
}

A. 5

B. 7

C. 10

D. 3

答案

C

解析
  • foo 中参数 a(按值传递的副本)在栈展开时销毁:1 次
  • foo 中局部数组 arr[5] 在栈展开时销毁:5 次
  • catch 块中 delete[] p 释放堆上的 3 个对象:3 次
  • main 中的 a 对象在程序结束时销毁:1 次

因此,程序运行期间会输出 10 次 "~A()"

内联函数

About inline function, which statement is correct?

A. When the program is executed, inline function will insert the object code to every place where this function is called.

B. When the program is compiled, inline function will insert the object code to every place where this function is called.

C. Inline function must be defined inside a class.

D. Inline function must be defined outside a class with keyword "inline".

答案

B

解析
  • A 错误:内联函数的展开(将函数体代码插入调用处)发生在编译时,而不是程序执行时。执行时只是运行已经展开的代码。
  • B 正确:内联函数的本质就是在编译阶段,由编译器将函数的对象代码(函数体)插入到每一个调用点,从而避免函数调用的额外开销(如参数压栈、跳转等)。当然,inline 关键字只是向编译器提出请求,最终是否内联由编译器决定,但从语义上讲,这就是编译时替换。
  • C 错误:内联函数不一定要定义在类内部。虽然在类定义内部直接实现的成员函数会自动被视为内联函数,但普通函数也可以定义为内联函数,且成员函数也可以在类外部使用 inline 关键字定义。
  • D 错误:内联函数并非必须在类外部用 inline 定义。对于类内部的成员函数定义,可以省略 inline 关键字;另外,全局函数也可以在声明或定义时加 inline。不存在这种“必须”的限制。
运算符重载

Which of the following operator cannot be used to overload when that function is declared as friend function?

A. ||

B. ==

C. -=

D. []

答案

D

解析

在 C++ 中,有四个运算符只能作为非静态成员函数重载,不能重载为非成员函数(包括友元函数):

  • = (赋值)
  • () (函数调用)
  • [] (下标)
  • -> (成员访问)
delete

About delete operator, which statement below is NOT correct?

A. Only pointers as the result of a new operation can be used to be deleted.

B. Destructor will be called automatically during the delete operation.

C. It is safe to delete the same pointer multiple times.

D. There's only one pair of [] followed to delete a multi-dimension array.

答案

C

解析
  • A 正确:只有 new 操作返回的指针才能用 delete 释放,栈上普通指针不能用 delete
  • B 正确:执行 delete 时,会自动调用对应对象的析构函数,再释放内存。
  • C 错误:对同一个指针多次 delete 会引发未定义行为(内存重复释放、程序崩溃等),是不安全的。
  • D 正确:C++ 释放多维数组只需要写一次 delete[],不需要逐层写多组 [],例如 int a[2][3]; int (*p)[3] = new int[2][3]; delete[] p;