本文由 OOP 课程 HW1–HW6 的个人 TeX 错题笔记整理而成。每道题均保留原题、选项和代码;答案与解析以原笔记为基础,并保留其中对题库表述不严谨之处的辨析。

题干、选项和代码默认展示,答案与解析折叠在题目下方。部分题目带有 Java/C# 或旧教材语境,阅读时应以题目所指定的语言和标准为准。

校勘说明

  • HW1 Question 8 的原题不是严格的单选题:按照 C++ 语言规则,B、C、D 都可以判为错误陈述;题库预期答案应是 B。
  • “C++ 完全向下兼容 C”是常见但不严谨的说法。两者具有很高的源码兼容性,但 C++ 不是 C 的严格超集。
  • 涉及拷贝构造的题目默认采用课程基础语境;现代 C++ 还需要考虑移动构造和复制消除。
  • 原笔记中的题库答案予以保留;遇到题干或选项存在歧义时,解析会优先说明标准 C++ 下的严格结论。

阅读索引

  • HW1:OOP 基础、抽象、封装与命名空间
  • HW2:对象、构造/析构、成员、友元与运算符
  • HW3:继承、访问控制与类型转换
  • HW4:多态、虚函数与抽象类
  • HW5:综合代码题与进阶专题
  • HW6:模板、STL、字符串与异常处理

HW1

C++ OOP and Namespaces Questions

Question 8

Which concept of OOP is false for C++?

  • A. Code can be written without using classes

  • B. Code must contain at least one class

  • C. A class must have member functions

  • D. At least one object should be declared in code

查看答案与解析
Question 18

Which feature allows open recursion, among the following?

  • A. Use of this pointer

  • B. Use of pointers

  • C. Use of pass by value

  • D. Use of parameterized constructor

查看答案与解析
Question 21

Which among the following is false, for a member function of a class?

  • A. All member functions must be defined

  • B. Member functions can be defined inside or outside the class body

  • C. Member functions need not be declared inside the class definition

  • D. Member functions can be made friend to another class using the friend keyword

查看答案与解析
Question 32

Abstraction principle includes____

  • A. Use abstraction at its minimum

  • B. Use abstraction to avoid longer codes

  • C. Use abstraction whenever possible to avoid duplication

  • D. Use abstraction whenever possible to achieve OOP

查看答案与解析
Question 33

Encapsulation and abstraction differ as ____

  • A. Binding and Hiding respectively

  • B. Hiding and Binding respectively

  • C. Can be used any way

  • D. Hiding and hiding respectively

查看答案与解析
Question 34

In terms of stream and files ____

  • A. Abstraction is called a stream and device is called a file

  • B. Abstraction is called a file and device is called a stream

  • C. Abstraction can be called both file and stream

  • D. Abstraction can’t be defined in terms of files and stream

查看答案与解析
Question 37

Which among the following is not a level of abstraction?

  • A. Logical level

  • B. Physical level

  • C. View level

  • D. External level

查看答案与解析
Question 40

Identify the correct statement.

  • A. Namespace is used to group class, objects and functions

  • B. Namespace is used to mark the beginning of the program

  • C. A namespace is used to separate the class, objects

  • D. Namespace is used to mark the beginning & end of the program

查看答案与解析
Question 41

What is the use of Namespace?

  • A. To encapsulate the data

  • B. To structure a program into logical units

  • C. Encapsulate the data & structure a program into logical units

  • D. It is used to mark the beginning of the program

查看答案与解析
Question 47

What will be the output of the following C++ code?

#include <iostream>
using namespace std;
namespace extra {
    int i;
}
void i() {
    using namespace extra;
    int i;
    i = 9;
    cout << i;
}
int main() {
    enum letter { i, j };
    class i { letter j; };
    ::i();
    return 0;
}
  • A. 9

  • B. 10

  • C. compile time error

  • D. 11

查看答案与解析
Question 54

What will be the output of the following C++ code?

#include <iostream>
using namespace std;
namespace {
    int var = 10;
}
int main() {
    cout << var;
}
  • A. 10

  • B. Error

  • C. Some garbage value

  • D. Nothing but program runs perfectly

查看答案与解析

HW2

PTA2

Question 1

The copy constructors can be used to ____

  • A. Copy an object so that it can be passed to another primitive type variable

  • B. Copy an object for type casting

  • C. Copy an object so that it can be passed to a function

  • D. Copy an object so that it can be passed to a class

查看答案与解析
Question 2

Which constructor will be called from the object obj2 in the following C++ program?

A obj2(10,20);

  • A. A(int y, int x)

  • B. A(int y; int x)

  • C. A(int y)

  • D. A(int x)

查看答案与解析
Question 5

Which of the following is not a property of an object?

  • A. Properties

  • B. Names

  • C. Identity

  • D. Attributes

查看答案与解析
Question 10

If data members are private, what can we do to access them from the class object?

  • A. Private data members can never be accessed from outside the class

  • B. Create public member functions to access those data members

  • C. Create private member functions to access those data members

  • D. Create protected member functions to access those data members

查看答案与解析
Question 15

Copy constructor will be called whenever the compiler ____

  • A. Generates implicit code

  • B. Generates member function calls

  • C. Generates temporary object

  • D. Generates object operations

查看答案与解析
Question 17

Can a copy constructor be made private?

  • A. Yes, always

  • B. Yes, if no other constructor is defined

  • C. No, never

  • D. No, private members can’t be accessed

查看答案与解析
Question 20

Does constructor overloading include different return types for constructors to be overloaded?

  • A. Yes, if return types are different, signature becomes different

  • B. Yes, because return types can differentiate two functions

  • C. No, return type can’t differentiate two functions

  • D. No, constructors doesn’t have any return type

查看答案与解析
Question 21

Destructor calls ____ (C++)

  • A. Are only implicit

  • B. Are only explicit

  • C. Can be implicit or explicit

  • D. Are made at end of program only

查看答案与解析
Question 26

When a destructor is called?

  • A. After the end of object life

  • B. Anytime in between object’s lifespan

  • C. At end of whole program

  • D. Just before the end of object life

查看答案与解析
Question 29

Global destructors execute in ____ order after main function is terminated.

  • A. Sequential

  • B. Random

  • C. Reverse

  • D. Depending on priority

查看答案与解析
Question 31

Which among the following is true for public class?

  • A. There can be more than one public class in a single program

  • B. Public class members can be used without using instance of class

  • C. Public class is available only within the package

  • D. Public classes can be accessed from any other class using instance

查看答案与解析
Question 38

What is the output of following code?

int n=10; // global
class A {
private :
    int n;
public :
    int m;
    A() { n=100; m=50; }
    void disp() { cout<<"n"<<m<<n; }
};
  • A. 1050100

  • B. 1005010

  • C. n5010

  • D. n50100

查看答案与解析
Question 44

Can a function, other than the enclosing function of local class, access the class members?

  • A. Yes, using object

  • B. Yes, using direct call

  • C. Yes, using pointer

  • D. No, can’t access

查看答案与解析
Question 45

Which among the following is the main advantage of using local classes?

  • A. Make program more efficient

  • B. Makes program execution faster

  • C. Helps to add extra functionality to a function

  • D. Helps to add more members to a function

查看答案与解析
Question 48

Non-static nested classes have access to ____ from enclosing class.

  • A. Private members

  • B. Protected members

  • C. Public members

  • D. All the members

查看答案与解析
Question 56

Which among the following is correct?

  • A. Friend function of derived class can access non-private members of base class

  • B. Friend function of base class can access derived class members

  • C. Friend function of derived class can access members of only derived class

  • D. Friend function can access private members of base class of a derived class

查看答案与解析
Question 1

The copy constructors can be used to ____

  • A. Copy an object so that it can be passed to another primitive type variable

  • B. Copy an object for type casting

  • C. Copy an object so that it can be passed to a function

  • D. Copy an object so that it can be passed to a class

查看答案与解析
Question 2

Which constructor will be called from the object obj2 in the following C++ program?

A obj2(10,20);

  • A. A(int y, int x)

  • B. A(int y; int x)

  • C. A(int y)

  • D. A(int x)

查看答案与解析
Question 5

Which of the following is not a property of an object?

  • A. Properties

  • B. Names

  • C. Identity

  • D. Attributes

查看答案与解析
Question 10

If data members are private, what can we do to access them from the class object?

  • A. Private data members can never be accessed from outside the class

  • B. Create public member functions to access those data members

  • C. Create private member functions to access those data members

  • D. Create protected member functions to access those data members

查看答案与解析
Question 15

Copy constructor will be called whenever the compiler ____

  • A. Generates implicit code

  • B. Generates member function calls

  • C. Generates temporary object

  • D. Generates object operations

查看答案与解析
Question 17

Can a copy constructor be made private?

  • A. Yes, always

  • B. Yes, if no other constructor is defined

  • C. No, never

  • D. No, private members can’t be accessed

查看答案与解析
Question 20

Does constructor overloading include different return types for constructors to be overloaded?

  • A. Yes, if return types are different, signature becomes different

  • B. Yes, because return types can differentiate two functions

  • C. No, return type can’t differentiate two functions

  • D. No, constructors doesn’t have any return type

查看答案与解析
Question 21

Destructor calls ____ (C++)

  • A. Are only implicit

  • B. Are only explicit

  • C. Can be implicit or explicit

  • D. Are made at end of program only

查看答案与解析
Question 26

When a destructor is called?

  • A. After the end of object life

  • B. Anytime in between object’s lifespan

  • C. At end of whole program

  • D. Just before the end of object life

查看答案与解析
Question 29

Global destructors execute in ____ order after main function is terminated.

  • A. Sequential

  • B. Random

  • C. Reverse

  • D. Depending on priority

查看答案与解析
Question 31

Which among the following is true for public class?

  • A. There can be more than one public class in a single program

  • B. Public class members can be used without using instance of class

  • C. Public class is available only within the package

  • D. Public classes can be accessed from any other class using instance

查看答案与解析
Question 38

What is the output of following code?

int n=10; // global
class A {
private :
    int n;
public :
    int m;
    A() { n=100; m=50; }
    void disp() { cout<<"n"<<m<<n; }
};
  • A. 1050100

  • B. 1005010

  • C. n5010

  • D. n50100

查看答案与解析
Question 44

Can a function, other than the enclosing function of local class, access the class members?

  • A. Yes, using object

  • B. Yes, using direct call

  • C. Yes, using pointer

  • D. No, can’t access

查看答案与解析
Question 45

Which among the following is the main advantage of using local classes?

  • A. Make program more efficient

  • B. Makes program execution faster

  • C. Helps to add extra functionality to a function

  • D. Helps to add more members to a function

查看答案与解析
Question 48

Non-static nested classes have access to ____ from enclosing class.

  • A. Private members

  • B. Protected members

  • C. Public members

  • D. All the members

查看答案与解析
Question 56

Which among the following is correct?

  • A. Friend function of derived class can access non-private members of base class

  • B. Friend function of base class can access derived class members

  • C. Friend function of derived class can access members of only derived class

  • D. Friend function can access private members of base class of a derived class

查看答案与解析
Question 61

What are the constant member functions?

  • A. Functions which doesn’t change value of calling object

  • B. Functions which doesn’t change value of any object inside definition

  • C. Functions which doesn’t allow modification of any object of class

  • D. Functions which doesn’t allow modification of argument objects

查看答案与解析
Question 65

Which type of member functions get inherited in the same specifier in which the inheritance is done?

  • A. Private member functions

  • B. Protected member functions

  • C. Public member functions

  • D. All member functions

查看答案与解析
Question 73

If static data members have to be used inside a class, those member functions ____ (when no object exists)

  • A. Must not be static member functions

  • B. Must not be member functions

  • C. Must be static member functions

  • D. Must not be member function of corresponding class

查看答案与解析
Question 78

If object of class are created, then the static data members can be accessed ____

  • A. Using dot operator

  • B. Using arrow operator

  • C. Using colon

  • D. Using dot or arrow operator

查看答案与解析
Question 80

Which among the following is wrong syntax related to static data members?

  • A. className :: staticDataMember;

  • B. dataType className :: memberName =value;

  • C. static dataType memberName;

  • D. className : dataType -> memberName;

查看答案与解析
Question 81

Which among the following is correct definition for static member functions?

  • A. Functions created to allocate constant values to each object

  • B. Functions made to maintain single copy of member functions for all objects

  • C. Functions created to define the static members

  • D. Functions made to manipulate static programs

查看答案与解析
Question 90

The keyword static is used ____

  • A. With declaration inside class and with definition outside the class

  • B. With declaration inside class and not with definition outside the class

  • C. With declaration and definition wherever done

  • D. With each call to the member function

查看答案与解析
Question 93

What will be the output if all necessary code is included?

void test (Object &y) { y = "It is a string"; }
void main() {
    Object x ;
    test (x);
    cout<<x;
}
  • A. Run time error

  • B. Compile time error

  • C. Null

  • D. It is a string

查看答案与解析
Question 100

Which error will be produced if a local object is returned by reference outside a function?

  • A. Out of memory error

  • B. Run time error

  • C. Compile time error

  • D. No error

查看答案与解析
Question 102

Can we return an array of objects?

  • A. Yes, always

  • B. Yes, only if objects are having same values

  • C. No, because objects contain many other values

  • D. No, because objects are single entity (arrays are not a single entity)

查看答案与解析
Question 103

Which among the following is true?

  • A. Two objects can point to the same memory location

  • B. Two objects can never point to the same memory location

  • C. Objects not allowed to point at a location already occupied

  • D. Objects can’t point to any address

查看答案与解析
Question 108

How the argument passed to a function get initialized?

  • A. Assigned using copy constructor at time of passing

  • B. Copied directly

  • C. Uses addresses always

  • D. Doesn’t get initialized

查看答案与解析
Question 111

In copy constructor definition, if non const values are accepted only ____

  • A. Only const objects will be accepted

  • B. Only non – const objects are accepted

  • C. Only const members will not get copied

  • D. Compiler generates an error

查看答案与解析
Question 112

Use of assignment operator ____

  • A. Changes its use, when used at declaration and in normal assignment

  • B. Doesn’t changes its use, whatever the syntax might be

  • C. Assignment takes place in declaration and assignment syntax

  • D. Doesn’t work in normal syntax, but only with declaration

查看答案与解析
Question 117

Which among the following is true?

  • A. We can use direct assignment for any object

  • B. We can use direct assignment only for different class objects

  • C. We must not use direct assignment

  • D. We can use direct assignment to same class objects

查看答案与解析
Question 119

What is the size of an object pointer?

  • A. Equal to size of any usual pointer

  • B. Equal to size of sum of all the members of object

  • C. Equal to size of maximum sized member of object

  • D. Equal to size of void

查看答案与解析
Question 127

An object’s this pointer ____

  • A. Isn’t part of class

  • B. Isn’t part of program

  • C. Isn’t part of compiler

  • D. Isn’t part of object itself

查看答案与解析
Question 130

Which is the correct interpretation of the member function call from an object, object.function(parameter);

  • A. object.function(&this, parameter)

  • B. object(&function,parameter)

  • C. function(&object,&parameter)

  • D. function(&object,parameter)

查看答案与解析
Question 132

Which among the following is true? (About this pointer)

  • A. This pointer can be used to guard against any kind of reference

  • B. This pointer can be used to guard against self-reference

  • C. This pointer can be used to guard from other pointers

  • D. This pointer can be used to guard from parameter referencing

查看答案与解析
Question 136

This pointer can be used directly to ____

  • A. To manipulate self-referential data structures

  • B. To manipulate any reference to pointers to member functions

  • C. To manipulate class references

  • D. To manipulate and disable any use of pointers

查看答案与解析
Question 137

Which is the correct syntax for declaring the type of this in a member function?

  • A. classType [cv-qualifier-list] *const this;

  • B. classType const[cv-qualifier-list] *this;

  • C.[cv-qualifier-list]*const classType this;

  • D.[cv-qualifier-list] classType *const this;

查看答案与解析
Question 139

If a constructors should be capable of creating objects without argument and with arguments, which is a good alternative for this purpose?

  • A. Use zero argument constructor

  • B. Use constructor with one parameter

  • C. Use constructor with all default arguments

  • D. Use default constructor

查看答案与解析
Question 142

If the constructors are overloaded by using the default arguments, which problem may arise?

  • A. The constructors might have all the same arguments except the default arguments

  • B. The constructors might have same return type

  • C. The constructors might have same number of arguments

  • D. The constructors can’t be overloaded with respect to default arguments

查看答案与解析
Question 145

Which constructor definition will produce a compile time error?

  • A. className(int x=0);

  • B. className(char c);

  • C. className(int x=0, char c);

  • D. className(char c, int x=0);

查看答案与解析
Question 147

Which is the correct statement for default constructors?

  • A. The constructors with all the default arguments

  • B. The constructors with all the null and zero values

  • C. The constructors which can’t be defined by programmer

  • D. The constructors with zero arguments

查看答案与解析
Question 150

What happens when new fails?

  • A. Returns zero always

  • B. Throws an exception always

  • C. Either throws an exception or returns zero

  • D. Terminates the program

查看答案与解析
Question 160

If delete is used to delete an object which was not allocated using new ____

  • A. Then out of memory error arises

  • B. Then unreachable code error arises

  • C. Then unpredictable errors may arise

  • D. Then undefined variable error arises

查看答案与解析
Question 162

When delete operator is used ____ (If object has a destructor)

  • A. Object destructor is called after deallocation

  • B. Object destructor is called before deallocation

  • C. Object destructor is not used

  • D. Object destructor can be called anytime during destruction

查看答案与解析
Question 163

If delete is applied to an object whose l-value is modifiable, then ____ after the object is deleted.

  • A. Its value is defined as null

  • B. Its value is defined as void

  • C. Its value is defined as 0

  • D. Its value is undefined

查看答案与解析
Question 164

Which is the correct syntax to delete an array of objects?

  • A. delete[] objectName;

  • B. delete * objectName;

  • C. objectName[] delete;

  • D. delete objectName[];

查看答案与解析
Question 165

The delete operator ____

  • A. Invokes function operator delete

  • B. Invokes function defined by user to delete

  • C. Invokes function defined in global scope to delete object

  • D. Doesn’t invoke any function

查看答案与解析
Question 167

What are inbuilt classes?

  • A. The predefined classes in a language

  • B. The classes that are defined by the user

  • C. The classes which are meant to be modified by the user

  • D. The classes which can’t be used by the user

查看答案与解析
Question 169

What doesn’t inbuilt classes contain?

  • A. Function prototype

  • B. Function declaration

  • C. Function definitions

  • D. Objects

查看答案与解析
Question 171

What is an array of objects?

  • A. An array of instances of class represented by single name

  • B. An array of instances of class represented by more than one name

  • C. An array of instances which have more than 2 instances

  • D. An array of instances which have different types

查看答案与解析

HW3

Inheritance, Access Specifiers and Object Lifecycle

Question 6

If class B inherits class A privately. And class B has a friend function. Will the friend function be able to access the private member of class A?

  • A. Yes, because friend function can access all the members

  • B. Yes, because friend function is of class B

  • C. No, because friend function can only access private members of friend class

  • D. No, because friend function can access private member of class A also

查看答案与解析
Question 10

Which among the following is true for the given code below?

class A {
protected: int marks;
public:
    A() { marks=100; }
    void disp() { cout<<"marks="<<marks; }
};
class B: protected A { };
B b;
b.disp();
  • A. Object b can’t access disp() function

  • B. Object b can access disp() function inside its body

  • C. Object b can’t access members of class A

  • D. Program runs fine

查看答案与解析
Question 14

If a class have default constructor defined in private access, and one parameter constructor in protected mode, how will it be possible to create instance of object?

  • A. Define a constructor in public access with different signature

  • B. Directly create the object in the subclass

  • C. Directly create the object in main() function

  • D. Not possible

查看答案与解析
Question 19

Which specifier allows to secure the public members of base class in inherited classes?

  • A. Private

  • B. Protected

  • C. Public

  • D. Private and Protected

查看答案与解析
Question 25

Which type of inheritance is illustrated by the following code?

class student { public: int marks; };
class topper: public student { public: char grade; };

class average { public: int makrs_needed; };
class section: public average { public: char name[10]; };
class overall: public average { public: int students; };
  • A. Single level

  • B. Multilevel and single level

  • C. Hierarchical

  • D. Hierarchical and single level

查看答案与解析
Question 27

Which type of inheritance results in the diamond problem?

  • A. Single level

  • B. Hybrid (部分题库为 Hierarchical & Multiple 的组合)

  • C. Hierarchical

  • D. Multilevel

查看答案与解析
Question 31

If class A and class B are derived from class C and class D, then ____

  • A. Those are 2 pairs of single inheritance

  • B. That is multilevel inheritance

  • C. Those is enclosing class

  • D. Those are all independent classes

查看答案与解析
Question 32

Single level inheritance supports ____ inheritance.

  • A. Runtime

  • B. Compile time

  • C. Multiple inheritance

  • D. Language independency

查看答案与解析
Question 34

Which among the following is false for single level inheritance?

  • A. There can be more than 2 classes in program to implement single inheritance

  • B. There can be exactly 2 classes to implement single inheritance in a program

  • C. There can be more than 2 independent classes involved in single inheritance

  • D. The derived class must implement all the abstract method if single inheritance is used

查看答案与解析
Question 35

Which among the following best defines multilevel inheritance?

  • A. A class derived from another derived class

  • B. Classes being derived from other derived classes

  • C. Continuing single level inheritance

  • D. Class which have more than one parent

查看答案与解析
Question 37

In multilevel inheritance one class inherits ____

  • A. Only one class

  • B. More than one class

  • C. At least one class

  • D. As many classes as required

查看答案与解析
Question 40

Why does diamond problem arise due to multiple inheritance?

  • A. Methods with same name creates ambiguity and conflict

  • B. Methods inherited from the superclass may conflict

  • C. Derived class gets overloaded with more than two class methods

  • D. Derived class can’t distinguish the owner class of any derived method

查看答案与解析
Question 44

Pointer to a base class can be initialized with the address of derived class, because of ____

  • A. derived-to-base implicit conversion for pointers

  • B. base-to-derived implicit conversion for pointers

  • C. base-to-base implicit conversion for pointers

  • D. derived-to-derived implicit conversion for pointers

查看答案与解析
Question 47

Can constructors be overloaded in derived class?

  • A. Yes, always

  • B. Yes, if derived class has no constructor

  • C. No, programmer can’t do it

  • D. No, never

查看答案与解析
Question 55

Is it compulsory for all the classes in multilevel inheritance to have constructors defined explicitly if only last derived class object is created?

  • A. Yes, always

  • B. Yes, to initialize the members

  • C. No, it not necessary

  • D. No, Constructor must not be define

查看答案与解析

附录:重点知识梳理

C++ 经典继承模式全景对比

在 C++ 面向对象编程与考试中,理清类的派生拓扑结构是解决复杂结构题和“菱形危机”的关键。请牢记以下四种核心继承模式的本质区别:

继承模式 (英文) 拓扑结构 核心描述与易错考点
Single level (单继承) 1 对 1 一个派生类只拥有唯一一个直接基类。(A \(\rightarrow\) B)
**易错点:**单继承约束的只是“认爹的规则”。哪怕程序里写了 1000 个独立的类,只要它们全都是两两一对一继承,整体就依然是单继承。
Multilevel (多级继承) 单向垂直链 派生类作为新的基类,继续派生下一个类,形成家族代代相传的垂直链条。(A \(\rightarrow\) B \(\rightarrow\) C)
**易错点:**构造顺序永远从“最顶层的老祖宗”开始往下,析构顺序严格反转(拆楼逻辑)。每层只能看见紧挨着自己的上一层非私有成员。
Hierarchical (层次继承) 1 对 多 一个基类同时派生出多个不同的子类,像大树发叉一样散开。(A \(\rightarrow\) B 且 A \(\rightarrow\) C)
**易错点:**兄弟子类之间是平行的,互不干扰。多用于同一基类在不同业务方向上的分支实现。
Hybrid (混合继承) 网状交织 两种或两种以上继承方式的结合体(通常是 Hierarchical 加上 Multiple 多重继承)。
**易错点:**这是导致大名鼎鼎的 菱形问题 (Diamond Problem) 的罪魁祸首!子类通过不同的路径继承了同一个顶层基类的两份数据拷贝,调用时会产生严重的“二义性(Ambiguity)”。

【特别补充:Multiple Inheritance (多重继承)】

很多时候题目会单独考察它。它指的是一个子类同时拥有多个直接基类(多对 1,例如 B 和 C 共同派生出 D)。它正是引发上述 Hybrid 混合继承“菱形危机”的核心机制。在 C++ 中,为了解决菱形二义性,必须在多重继承的中间层使用 virtual 关键字进行虚继承 (Virtual Inheritance)

HW4

Question 3

Which among the following is mandatory condition for operators overloading?

  • A. Overloaded operator must be member function of the left operand

  • B. Overloaded operator must be member function of the right operand

  • C. Overloaded operator must be member function of either left or right operand

  • D. Overloaded operator must not be dependent on the operands

查看答案与解析
Question 4

When the operator to be overloaded becomes the left operand member then ____

  • A. The right operand acts as implicit object represented by *this

  • B. The left operand acts as implicit object represented by *this

  • C. Either right or left operand acts as implicit object represented by *this

  • D. *this pointer is not applicable in that member function

查看答案与解析
Question 5

*If the left operand is pointed by this pointer, what happens to other operands?

  • A. Other operands are passed as function return type

  • B. Other operands are passed to compiler implicitly

  • C. Other operands must be passed using another member function

  • D. Other operands are passed as function arguments

查看答案与解析
Question 8

Which object’s members can be called directly while overloading operator function is used (In function definition)?

  • A. Left operand members

  • B. Right operand members

  • C. All operand members

  • D. None of the members

查看答案与解析

补充笔记:运算符的重载方式选择 (Member vs Friend)

在 C++ 中,运算符重载的选择并非随意,编译器对不同运算符有严格的规定。以下为四大类核心场景:

1. 必须且只能用“成员函数 (Member Function)”重载

这类运算符与对象的内存状态或生命周期高度绑定,左操作数必须是该类的对象。

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

  • 记忆口诀: “等号、中括号、小括号、箭头” 这四个是类的“私有财产”,绝不允许非成员函数插手。

2. 通常必须用“友元函数 / 非成员函数 (Friend Function)”重载

这类运算符的左操作数通常不是自定义类,而是标准库类(如 ostreamistream)。

  • 包含: 流插入 <<、流提取 >>

  • 核心逻辑: 为了保持 cout << obj; 这种符合直觉的写法,左操作数必须是 ostream 对象,因此只能写成全局非成员函数:operator<<(ostream& out, const MyClass& obj)。通常将其声明为 friend 以便访问类内的私有成员。

3. 两者皆可(Member 或 Friend 都可以)

绝大多数常规的二元运算符都属于这一类。

  • 包含: 算术 (+, -, *), 关系 (==, <), 复合赋值 (+=), 自增自减 (++, --) 等。

  • 最佳实践经验:

  • 改变对象自身状态的(如 +=, ++):强烈建议用 Member function

  • 不改变双方状态且具对称性的(如 +, ==):强烈建议用 Friend function。因为友元函数允许左右两边的操作数都进行隐式类型转换(例如 obj + 55 + obj 均可顺利编译)。

4. 绝对不能被重载的运算符

为了保证 C++ 基础语法的解析不崩溃,以下 5 个运算符绝对禁止重载:

  • 成员访问 .、成员指针访问 .*、作用域解析 ::、三目条件 ?:、获取大小 sizeof

终极总结速查表

运算符类别 示例 必须 Member? 必须 Friend? 两者皆可?
类的核心行为 =, [], (), -> \(✓\) \(\times\) \(\times\)
标准 IO 流操作 <<, >> (配合 cin/cout) \(\times\) \(✓\) \(\times\)
修改对象自身状态 +=, -=, ++, -- \(\times\) \(\times\) \(✓\) 可以 (推荐 Member)
对称计算与比较 +, -, ==, < \(\times\) \(\times\) \(✓\) 可以 (推荐 Friend)
底层语法解析 ., ::, ?:, sizeof - - 严禁重载

Operator Overloading & Polymorphism Concepts

Concept 1: Assignment Operator

赋值运算符可以重载,但是无论参数为何种类型,赋值运算符都必须重载为成员函数,并且因为返回的是左值,所以返回值的类型必须是该类的 。

答案: 引用(或 类名&

解析: 赋值运算符 = 为了支持连续赋值操作(如 a = b = c;),其返回值必须是可以被修改的左值。因此,它必须返回调用该运算符的对象自身的引用(即 return *this;),其标准函数签名通常为 ClassName& operator=(const ClassName& rhs);

Concept 2: Parameter Counts & Polymorphism Definition

运算符重载为类的成员函数时,函数参数个数比原来的运算符个数 ,当重载为类的友元函数时,参数个数与原来运算符个数 。多态指不同对象接收 时产生的 。

答案: 少一个相同相同的消息不同行为

解析:

  • 参数个数: 重载为成员函数时,左操作数充当了隐藏的 this 指针,所以显式参数少一个;重载为友元函数时无隐藏指针,所有操作数都要显式传入。

  • 多态本质: 面向对象中的多态,核心定义就是“不同的对象接收到相同的消息(即调用同名接口),由于内部具体实现不同,从而表现出不同的响应行为”。

Concept 3: Overloading Methods

运算符重载函数的两种主要方式是 、。

答案: 成员函数友元函数(或普通全局函数)

Concept 4: Types of Polymorphism (Execution)

从运行的角度多态可分为 和 。

答案: 静态多态(编译时多态)、动态多态(运行时多态)

解析: 静态多态主要依赖函数重载、运算符重载和模板(Template)在编译期决断;动态多态则主要依赖继承和虚函数(virtual)在运行期决断。

Concept 5: Function Overloading

函数重载就是一种 ,相同的函数名,对应多个不同的函数体。

答案: 静态多态(或编译时多态)

解析: 编译器在编译阶段,就能根据传入实参的“类型、数量、顺序”组成的函数签名,唯一确定需要绑定哪一个具体的函数版本,无需拖延到运行时。

Concept 6: Overloading Polymorphism

函数重载和 都属于重载多态。

答案: 运算符重载

解析: 广义的多态可分为四大类:重载多态(函数/运算符重载)、强制多态(类型转换)、包含多态(虚函数)、参数多态(模板)。

Concept 7: Coercion Polymorphism

强制类型转换是通过 来实现的。

答案: 类型转换函数(如 operator int()) 或 强制多态

解析: 在 C++ 类的语境下,实现隐式或强制类型转换靠的是自定义的类型转换重载函数。

Concept 8: Binding Phases

按照联编所进行的阶段不同,可分为两种不同的联编方法: 和 。

答案: 静态联编(早期联编/编译时联编)、动态联编(晚期联编/运行时联编)

解析: 联编(Binding)即将函数调用语句与具体的函数体内存代码绑定在一起的过程。

Concept 9: Dynamic Binding Core

动态联编对函数的选择不是基于指针或者引用,而是基于 ,在编译、链接过程中无法解决的绑定问题要等到程序开始运行之后再确定。

答案: 指针或引用所指向对象的实际类型

解析: 这是动态多态的灵魂。例如 Base* ptr = new Derived();ptr 的静态类型是基类指针,但它真正在内存中指向的实际类型是派生类。动态联编会通过查找虚函数表(vtable),在运行时精准调用派生类重写的虚函数。

Concept 10: Copy Constructor vs. Assignment Operator (拷贝构造与赋值重载的易错区分)

在 C++ 中,“=” 符号在不同的上下文中有两种完全不同的含义,这是考试和面试中最常见的陷阱。核心的判断标准只有一个:赋值号左边的对象是否已经存在?

1. 拷贝构造函数 (Copy Constructor): A(const A &)

  • 核心动作:初始化 (Initialization)

  • 触发条件: 正在创建一个全新的对象,并用一个同类型的、已存在的对象来初始化它。此时该对象在内存中刚刚被分配空间。

  • 典型代码: A d = a; (在 C++ 编译器眼中,这行代码与 A d(a); 完全等价,绝不会调用 operator=)。

  • 其他隐式调用场景 (极易考):

  • 将对象作为实参,按值传递给函数参数时(例如调用 void func(A obj);)。

  • 函数按值返回一个局部对象时(例如 A func() \{ return a; \})。

2. 赋值运算符重载 (Assignment Operator): A& operator=(const A &)

  • 核心动作:赋值 (Assignment)

  • 触发条件: 针对一个已经存在(在之前的代码中早就已经被构造函数创建好)的对象,修改或覆盖它的状态。

  • 典型代码: c = a; (前提是 c 在这行代码之前就已经通过诸如 A c; 声明过了)。

【一句话防坑口诀】

    **带有类型名声明的“`=`”**(例如 `A d = a;`)是在生孩子,**必定调用拷贝构造**;

    **没有类型名、单独使用的“`=`”**(例如 `c = a;`)是在换衣服,**必定调用赋值重载**。

HW5

总览

本份笔记整理以下错题:1,2,7,10,16,19,22,26–27,28,32,35,37,38,39,44,45,47,50,51,54,55,57,58,62,以及一道虚函数输出分析题。

核心主线:

  • virtual function:普通虚函数,不一定要被派生类重写。

  • pure virtual function / abstract method:纯虚函数,若派生类要成为具体类,则必须实现。

  • abstract class:含有至少一个纯虚函数的类,不能直接创建对象,但可以创建指针或引用。

  • runtime polymorphism:必须通过基类指针或引用调用虚函数,才体现运行时多态。

Question 1

以下说法正确的是?

  • A. 派生类可以和基类有同名成员函数,但是不能有同名成员变量

  • B. 派生类的成员函数中,可以调用基类的同名同参数表的成员函数

  • C. 派生类和基类的同名成员函数必须参数表不同,否则就是重复定义

  • D. 派生类和基类的同名成员变量存放在相同存储空间

查看答案与解析
Question 2

Which among the following can show polymorphism?

  • A. Overloading &&

  • B. Overloading <<

  • C. Overloading ||

  • D. Overloading +=

查看答案与解析
Question 7

**If a virtual member function is defined **

  • A. It should not contain any body and defined by subclasses

  • B. It must contain body and overridden by subclasses

  • C. It must contain body and be overloaded

  • D. It must not contain any body and should not be derived

查看答案与解析
Question 10

What does a virtual function ensure for an object, among the following?

  • A. Correct method is called, regardless of the class defining it

  • B. Correct method is called, regardless of the object being called

  • C. Correct method is called, regardless of the type of reference used for function call

  • D. Correct method is called, regardless of the type of function being called by objects

查看答案与解析
Question 16

Which is a must condition for virtual function to achieve runtime polymorphism?

  • A. Virtual function must be accessed with direct name

  • B. Virtual functions must be accessed using base class object

  • C. Virtual function must be accessed using pointer or reference

  • D. Virtual function must be accessed using derived class object only

查看答案与解析
Question 19

It is to redefine the virtual function in derived class.

  • A. Necessary

  • B. Not necessary

  • C. Not acceptable

  • D. Good practice

查看答案与解析
Question 22

Which among the following is true?

  • A. The abstract functions must be only declared in derived classes

  • B. The abstract functions must not be defined in derived classes

  • C. The abstract functions must be defined in base and derived class

  • D. The abstract functions must be defined either in base or derived class

查看答案与解析
Question 26--27

Given:

class A {
    A() {};
    virtual f() {};
    int i;
};

which statement is NOT true?

  • A. i is private

  • B. f() is an inline function

  • C. i is a member of class A

  • D. sizeof(A) == sizeof(int)

查看答案与解析
Question 28

Given:

class X {
    int i;
    virtual void f() {};
};

If sizeof(int*) == sizeof(int) == 4, then sizeof(X)==?

  • A. 4

  • B. 6

  • C. 8

  • D. Undetermined

查看答案与解析
Question 32

Which among the following is wrong?

  • A. class student\{ \}; student s;

  • B. abstract class student\{ \}; student s;

  • C. abstract class student\{ \}s[50000000];

  • D. abstract class student\{ \}; class toppers: public student\{ \}; topper t;

查看答案与解析
Question 35

Which among the following best describes abstract classes?

  • A. If a class has more than one virtual function, it’s abstract class

  • B. If a class have only one pure virtual function, it’s abstract class

  • C. If a class has at least one pure virtual function, it’s abstract class

  • D. If a class has all the pure virtual functions only, then it’s abstract class

查看答案与解析
Question 37

**If there is an abstract method in a class then, **

  • A. Class must be abstract class

  • B. Class may or may not be abstract class

  • C. Class is generic

  • D. Class must be public

查看答案与解析
Question 38

**If a class is extending/inheriting another abstract class having abstract method, then **

  • A. Either implementation of method or making class abstract is mandatory

  • B. Implementation of the method in derived class is mandatory

  • C. Making the derived class also abstract is mandatory

  • D. It’s not mandatory to implement the abstract method of parent class

查看答案与解析
Question 39

Abstract class A has 4 virtual functions. Abstract class B defines only 2 of those member functions as it extends class A. Class C extends class B and implements the other two member functions of class A. Choose the correct option below.

  • A. Program won’t run as all the methods are not defined by B

  • B. Program won’t run as C is not inheriting A directly

  • C. Program won’t run as multiple inheritance is used

  • D. Program runs correctly

查看答案与解析
Question 44

**The abstract classes in Java can **

  • A. Implement constructors

  • B. Can’t implement constructor

  • C. Can implement only unimplemented methods

  • D. Can’t implement any type of constructor

查看答案与解析
Question 45

It is to have an abstract method.

  • A. Not mandatory for an static class

  • B. Not mandatory for a derived class

  • C. Not mandatory for an abstract class

  • D. Not mandatory for parent class

查看答案与解析
Question 47

If single level inheritance is used and an abstract class is created with some undefined functions, can its derived class also skip some definitions?

  • A. Yes, always possible

  • B. Yes, possible if only one undefined function

  • C. No, at least 2 undefined functions must be there

  • D. No, the derived class must implement those methods

查看答案与解析
Question 50

Is it compulsory to have constructor for all the classes involved in multiple inheritance?

  • A. Yes, always

  • B. Yes, only if no abstract class is involved

  • C. No, only classes being used should have a constructor

  • D. No, they must not contain constructors

查看答案与解析
Question 51

Which among the following best defines the abstract methods?

  • A. Functions declared and defined in base class

  • B. Functions only declared in base class

  • C. Function which may or may not be defined in base class

  • D. Function which must be declared in derived class

查看答案与解析
Question 54

It is to define the abstract functions.

  • A. Mandatory for all the classes in program

  • B. Necessary for all the base classes

  • C. Necessary for all the derived classes

  • D. Not mandatory for all the derived classes

查看答案与解析
Question 55

**The abstract function definitions in derived classes is enforced at **

  • A. Runtime

  • B. Compile time

  • C. Writing code time

  • D. Interpreting time

查看答案与解析
Question 57

**If a function declared as abstract in base class doesn’t have to be defined in derived class then **

  • A. Derived class must define the function anyhow

  • B. Derived class should be made abstract class

  • C. Derived class should not derive from that base class

  • D. Derived class should not use that function

查看答案与解析
Question 58

Which among the following is true?

  • A. Abstract methods can be static

  • B. Abstract methods can be defined in derived class

  • C. Abstract methods must not be static

  • D. Abstract methods can be made static in derived class

查看答案与解析
Question 62

The abstract method definition can be made in derived class.

  • A. Private

  • B. Protected

  • C. Public

  • D. Private, public, or protected

查看答案与解析

补充题:虚函数、const 与隐藏

题目代码:

#include<iostream>
using namespace std;

class Base{
protected:
    int x;
public:
    Base(int b=0): x(b) { }
    virtual void display() const {cout << x << endl;}
};

class Derived: public Base{
    int y;
public:
    Derived(int d=0): y(d) { }
    void display() {cout << x << "," << y << endl;}
};

int main()
{
  Base b(1);
  Derived d(2);
  Base *p = &d;
  b.display();
  d.display();
  p->display();
  return 0;
}

输出:

1
0,2
0

解析 1:Base b(1)

Base b(1);

调用 Base(int b=0): x(b),所以 b.x = 1。因此:

b.display();

调用 Base::display() const,输出:

1

解析 2:Derived d(2)

Derived(int d=0): y(d) { }

这里只初始化了 y,没有显式调用基类构造函数,所以基类部分自动调用:

Base()

也就是 x = 0。同时 y = 2。因此:

d.display();

调用 Derived::display(),输出:

0,2

解析 3:为什么 p->display() 输出 0?

基类函数是:

virtual void display() const

派生类函数是:

void display()

注意:基类版本有 const,派生类版本没有 const。因此它们的函数签名不同,Derived::display() 并没有真正 override Base::display() const,而只是隐藏了基类同名函数。

所以:

Base *p = &d;
p->display();

调用的仍然是 Base::display() const。由于 d 的基类部分 x = 0,所以输出:

0

正确重写写法

如果想让它真正发生多态,应写成:

class Derived: public Base{
    int y;
public:
    Derived(int d=0): Base(d), y(d) { }

    void display() const override {
        cout << x << "," << y << endl;
    }
};

这样:

  • const 保持一致;

  • 使用 override 让编译器检查是否真正重写;

  • Base(d) 让基类部分的 x 也初始化为 d

此时输出会变为:

1
2,2
2,2

HW6

总览

本份笔记按照前面整理出的 1–100 题编号记录,不重新编号。整理范围包括:

[
1,\ 5,\ 17,\ 18,\ 25,\ 27,\ 30,\ 31,\ 32,\ 34,\ 41,\ 43,\ 44,\ 49,\ 53,\ 55,\ 66,\ 67,\ 71\text{–}72,\ 87
]

以及前面单独问过的异常题:

[
69,\ 73,\ 78,\ 86,\ 93,\ 94.
]

核心主线:

  • template:模板不是具体代码,实例化后才形成具体函数或具体类。

  • class template:使用类模板创建对象时,一般需要写模板实参,如 MyClass<int> a(10);

  • STL:容器、迭代器、算法相互配合,不是完全孤立的三部分。

  • string:注意 cin >> sgetline、下标越界和字符运算。

  • exception:多个 catch 从上到下匹配;派生类异常应写在基类异常之前;throw; 表示重抛当前异常。

一、模板与类模板

Question 1

**原题:**现有声明:

template <class T>
class Test { ... };

则以下哪一个声明不可能正确?

  • A. Test a;

  • B. Test<int> a;

  • C. Test<float> a;

  • D. Test<Test<int>> a;

查看答案与解析
Question 5

**原题:**关于函数模板,描述错误的是。

  • A. 函数模板必须由程序员实例化为可执行的函数模板

  • B. 函数模板的实例化由编译器实现

  • C. 一个类定义中,只要有一个函数模板,则这个类是类模板

  • D. 类模板的成员函数都是函数模板,类模板实例化后,成员函数也随之实例化

查看答案与解析
Question 17

**原题:**类模板的使用实际上是将类模板实例化成一个。

  • A. 函数

  • B. 对象

  • C. 类

  • D. 抽象类

查看答案与解析
Question 18

**原题:**下列关于模板的说法中,错误的是。

  • A. 用模板定义一个对象时,不能省略参数

  • B. 类模板只能有虚拟参数类型

  • C. 类模板的成员函数都是模板函数

  • D. 类模板在编译中不会生成任何代码

查看答案与解析
Question 25

**原题:**下列关于类模板的定义,正确的是。

  • A. template<class T, int i = 0>

  • B. template<class T, class int i>

  • C. template<class T, typename T>

  • D. template<class T1, T2>

查看答案与解析
Question 32

**原题:**模板的使用是为了()。

  • A. 提高代码的可重用性

  • B. 提高代码的运行效率

  • C. 加强类的封装性

  • D. 实现多态性

查看答案与解析
Question 34

**原题:**关于函数模板,描述错误的是( )。

  • A. 函数模板的实例化由编译器实现

  • B. 函数模板必须由程序员实例化为可执行的函数模板

  • C. 类模板的成员函数都是函数模板,类模板实例化后,成员函数也随之实例化

  • D. 一个类定义中,只要有一个函数模板,这个类就是类模板

查看答案与解析
Question 49

**原题:**A template class can have .

  • A. More than one generic data type

  • B. Only one generic data type

  • C. At most two data types

  • D. Only generic type of integers and not characters

查看答案与解析
Question 53

**原题:**Which is the most significant feature that arises by using template classes?

  • A. Code readability

  • B. Ease in coding

  • C. Code reusability

  • D. Modularity in code

查看答案与解析
Question 55

**原题:**How is function overloading different from template class?

  • A. Overloading is multiple function doing same operation, Template is multiple function doing different operations

  • B. Overloading is single function doing different operations, Template is multiple function doing different operations

  • C. Overloading is multiple function doing similar operation, Template is multiple function doing identical operations

  • D. Overloading is multiple function doing same operation, Template is same function doing different operations

查看答案与解析

二、STL、迭代器与 pair

迭代器知识总括

STL 的核心结构:

  • 容器 container:存数据,例如 vector, list, map, set

  • 迭代器 iterator:像指针一样访问容器中的元素。

  • 算法 algorithm:通过迭代器操作容器,例如 sort, find, copy

常见迭代器类型:

  • Input Iterator:只能读,只能向前走。

  • Output Iterator:只能写,只能向前走。

  • Forward Iterator:可多次遍历,只能向前。

  • Bidirectional Iterator:可前进也可后退,如 list 迭代器。

  • Random Access Iterator:可随机访问,如 vector 迭代器。

不存在“删除迭代器”这种基本迭代器类别。

Question 27

**原题:**设有如下代码段:

std::map<char *, int> m;
const int MAX_SIZE = 100;
int main() {
    char str[MAX_SIZE];
    for (int i = 0; i < 10; i++) {
        std::cin >> str;
        m[str] = i;
    }
    std::cout << m.size() << std::endl;
}

读入 10 个字符串,则输出的 m.size() 为:

  • A. 0

  • B. 1

  • C. 10

查看答案与解析
Question 30:pair 模板

**原题:**下列关于 pair<> 类模板的描述中,错误的是。

  • A. pair<> 类模板定义在头文件 utility

  • B. pair<> 类模板作用是将两个数据组成一个数据,两个数据可以是同一个类型也可以是不同的类型

  • C. 创建 pair<> 对象只能调用其构造函数

  • D. pair<> 类模板提供了两个成员函数 firstsecond 来访问这两个数据

查看答案与解析
Question 31

**原题:**下列选项中,哪一项不是迭代器。

  • A. 输入迭代器

  • B. 前向迭代器

  • C. 双向迭代器

  • D. 删除迭代器

查看答案与解析

三、string 与输入输出

Question 41

**原题:**有代码如下:

string s;
s[0] = '1';

则关于以上语句说法正确的是( )。

  • A. 语句 "s[0]='1';" 有问题

  • B. s 的值为字符串 "1"

  • C. s 是空格串

  • D. s 是空串

查看答案与解析
Question 43

**原题:**有代码如下:

int n;
string s;
cin >> n;
getline(cin, s);
cout << s.size() << endl;

则在输入以下数据后得到结果是( )。

1
Hello World
  • A. 11

  • B. 0

  • C. 5

  • D. 12

查看答案与解析
Question 44

**原题:**以下代码的输出结果是( )。

string res="";
string s,t="123456";
s=string(3,'0'); //相当于s="000";
s=s+"123";
for(int i=5;i>=0;i--) {
      char c=s[i]+t[i]-'0';
      res=c+res;
}
cout<<res<<endl;
  • A. 975321

  • B. 236456

  • C. 654632

  • D. 123579

查看答案与解析

四、异常处理

Question 66

**原题:**What is wrong in the following code?

vector<int> v;
v[0] = 2.5;
  • A. The program has a compile error because there are no elements in the vector.

  • B. The program has a compile error because you cannot assign a double value to v[0].

  • C. The program has a runtime error because there are no elements in the vector.

  • D. The program has a runtime error because you cannot assign a double value to v[0].

查看答案与解析
Question 67

**原题:**If you enter 1 0, what is the output of the following code?

#include "iostream"
using namespace std;

int main()
{
    cout << "Enter two integers: ";
    int number1, number2;
    cin >> number1 >> number2;

    try
    {
        if (number2 == 0)
            throw number1;

        cout << number1 << " / " << number2 << " is "
             << (number1 / number2) << endl;

        cout << "C" << endl;
    }
    catch (int e)
    {
        cout << "A";
    }

    cout << "B" << endl;
    return 0;
}
  • A. A

  • B. B

  • C. C

  • D. AB

查看答案与解析
Question 69

**原题:**Which of the following statements are true?

  • A. A custom exception class is just like a regular class.

  • B. A custom exception class must always be derived from class exception.

  • C. A custom exception class must always be derived from a derived class of class exception.

  • D. A custom exception class must always be derived from class runtime_error.

查看答案与解析
Question 71 与 Question 72 对比

共同代码:

try {
    statement1;
    statement2;
    statement3;
}
catch (Exception1 ex1)
{
}
catch (Exception2 ex2)
{
}
catch (Exception3 ex3)
{
    statement4;
    throw;
}
statement5;
Question 71

**原题:**Suppose that statement2 throws an exception of type Exception2 in the above statement. Which statement is executed after statement2 is executed?

  • A. statement2

  • B. statement3

  • C. statement4

  • D. statement5

查看答案与解析
Question 72

**原题:**Suppose that statement3 throws an exception of type Exception3 in the above statement. Which statements are executed after statement3 is executed?

  • A. statement2

  • B. statement3

  • C. statement4

  • D. statement5

查看答案与解析
Question 73

**原题:**下列关于异常处理的说法不正确的是( )。

  • A. 异常处理的 throwcatch 通常不在同一个函数中,实现异常检测与异常处理的分离。

  • B. catch 语句块必须跟在 try 语句块的后面,一个 try 语句块后可以有多个 catch 语句块。

  • C. 在对函数进行异常规范声明时,若形参表后没有任何表示抛出异常类型的说明,它表示该函数不能抛出任何异常。

  • D. catch 语句块中,catch(...) 表示该 catch 可以捕捉任意类型的异常,必须将 catch(...) 放在 catch 结构的最后。

查看答案与解析
Question 78

**原题:**下列关于断言的描述中,错误的是。

  • A. 断言是调试程序的一种手段

  • B. 若断言情况发生,一般会终止程序

  • C. 在 C++ 中,宏 assert() 用来在调试阶段实现断言

  • D. 断言在程序调试与发布版本中都可以使用断言

查看答案与解析
Question 86

**原题:**How many catch blocks can a single try block can have?

  • A. Only 1

  • B. Only 2

  • C. Maximum 127

  • D. As many as required

查看答案与解析
Question 87

**原题:**To catch the exceptions .

  • A. An object must be created to catch the exception

  • B. A variable should be created to catch the exception

  • C. An array should be created to catch all the exceptions

  • D. A string have to be created to store the exception

查看答案与解析
Question 93

**原题:**If catching of base class exception is done before derived class in C++ .

  • A. It gives compile time error

  • B. It doesn’t run the program

  • C. It may give warning but not error

  • D. It always gives compile time error

查看答案与解析
Question 94

**原题:**If a catch block accepts more than one exceptions then .

  • A. The catch parameters are not final

  • B. The catch parameters are final

  • C. The catch parameters are not defined

  • D. The catch parameters are not used

查看答案与解析

五、最开始单独问过的题号映射

你最开始问的问题 对应题号 核心结论
自定义异常类是否必须继承 exception Question 69 不必须,答案 A
try 后可以有几个 catch Question 86 按需多个,答案 D
catch 中为什么还能 throw; Question 72 throw; 是重抛当前异常
异常处理说法不正确 Question 73 void f(); 不代表不能抛异常,答案 C
断言是什么 Question 78 assert 调试用,发布版可能关闭,答案 D
基类异常先于派生类异常捕获 Question 93 可能 warning,但通常非 error,答案 C
一个 catch 捕获多个异常 Question 94 Java multi-catch,参数隐式 final,答案 B

六、最后速记

  • 类模板创建对象一般要写模板实参:Test<int> a;

  • 函数模板通常由编译器根据调用自动实例化,不需要程序员手动实例化。

  • 类模板可以有类型参数,也可以有非类型参数,如 template<class T, int N>

  • pairfirstsecond 是数据成员,不是成员函数。

  • STL 不是容器、迭代器、算法三者互不相干;算法靠迭代器操作容器。

  • map<char*, int> 比较的是指针地址,不是字符串内容。

  • string 不能直接写 s[0]='1'

  • cin >> n 后接 getline,要先处理换行符。

  • vector 不能直接访问 v[0]

  • 多个 catch 从上往下匹配;派生类异常放前,基类异常放后。

  • throw; 只能在处理异常时用于重抛当前异常。

  • assert 是调试手段,不应作为发布版错误处理机制。

  • Java 的 multi-catch 和 C++ 的异常语法不要混在一起。

原始资料下载

作业 TeX PDF
HW1 criticalNotes.tex criticalNotes.pdf
HW2 pta2.tex pta2.pdf
HW3 pta3.tex pta3.pdf
HW4 pta4.tex pta4.pdf
HW5 pta5.tex pta5.pdf
HW6 pta6.tex pta6.pdf

总结:如何复习这组错题

  1. 先判断题目考查的是语言规则、对象模型,还是特定教材的术语。
  2. 代码题先做名字查找、重载决议和静态类型分析,再考虑运行期动态绑定。
  3. 涉及生命周期时,按“构造顺序与析构逆序”画出对象关系。
  4. 涉及多态时,检查虚函数签名、const、访问权限和调用表达式的静态类型。
  5. 模板与 STL 题优先区分模板定义、实例化、容器、迭代器和算法各自的职责。
  6. 对题库中“必定”“只能”等绝对表述保持警惕,并结合当前 C++ 标准验证。