程设期中理论题解析

来源:互联网 发布:万讯网络中控 编辑:程序博客网 时间:2024/06/13 13:24

Question 1

Which of the following statements about static data members is NOT correct?
A. A static data member can be initialized at declaration.
B. A static data member refers to a member whose value cannot be changed after initialized.
C. We may access static data members before any objects is created.
D. A static data member is shared by objects of the same class.

Standard Answer: B
成员变量被声明为static(称为静态成员变量),意味着它为该类的所有实例所共享。

Question 2

Which of the following variables cannot be a member of class A?
A. A *p
B. A a
C. A &r
D. string s

Standard Answer: B
对于只有声明而没有定义的类,只能声明它的指针类型或引用类型,不能直接声明其对象类型。 因为如果类没有定义,编译器就不知道为这个对象分配多大空间,但是类的指针或引用类型的大小与类大小无关。

Question 3

Which of the following about static members (static variables and functions) is true?
A. Static variables are the same as object variables
B. Object functions can be called within a static function without an object
C. All static variables can be called in functions outside the class
D. Static variables must be defined “again” outside the class

Standard Answer: D
这是因为被static声明的类静态数据成员,其实体远在main()函数开始之前就已经在全局数据段中诞生了(见《Inside The C++ Object Model》page 247)!其生命期和类对象是异步的,(而且静态语意说明即使没有类实体的存在,其静态数据成员的实体也是存的)这个时候对象的生命期还没有开始。假如到类中去初始化类静态数据成员,让静态数据成员的初始化依赖于类的实体,如果类永远不被实例化,我们就永远不能访问到静态数据成员,这显然不符合“静态”的含义,所以static成员一定要在类外初始化。

Question 4

Which of the following function may not be automatically built when it is not defined?
A. The default constructor
B. The copy constructor
C. The assignment operator function
D. The destructor

Standard Answer: A
类的6个默认的成员函数包括:构造函数、析构函数、拷贝构造函数、赋值运算符重载函数、取地址操作符重载、const函数。

Question 5

Which of the following about class is NOT correct?
A. A class is a compound data type.
B. A class is a specification of a type of objects.
C. A class is a set of objects.
D. A class has member functions and member variables.

Standard Answer: C
类是一个概括,对象是一个实例。

Question 6

Which of the following statement does NOT call the constructor Type(int)?
A. Type object(100);
B. Type object = 100;
C. Type * pointer = new Type[100];
D. Type * array = new Type[2] {Type(100), Type(200)};

Standard Answer: C

Question 7

Which of the following about destructors is NOT true?
A. They have no return type
B. Each class can have more than one destructor
C. The last function each object calls must be the destructor
D. They are called automatically to finalize objects.

Standard Answer: B

Question 8

Which of the following about constructors is NOT correct?
A. They are called automatically when objects are created.
B. There can be multiple constructors in a class.
C. Their names are the same name as those of their classes.
D. A class always has a default constructor.

Standard Answer: D
如果用户定义的类中显式的定义了构造函数,编译器就不再会生成默认构造函数.

Question 9

Which of the following about inheritance is NOT true?
A. Private member variables can be accessed by sub-classes (derived classes)
B. Inheritance creates a new class by including everything in an existing class
C. A sub-class (derived class) is also called a derived class
D. A base class is also called a super-class

Standard Answer: A
派生类只可以访问基类的public和protected成员。

Question 10

If p is a pointer to an object with a member function x(), which of the following access to x() is correct?
A. *p.x
B. p->x()
C. *p->x()
D. p.x

Standard Answer: B

Question 11

Which statement about operator overloading is FALSE?
A. New operators can never be created.
B. The precedence of an operator cannot be changed by overloading.
C. Overloading cannot change how an operator works on built-in types.
D. Certain overloaded operators can change the number of arguments they take.

Standard Answer: D
C++11允许用户用 operate"" () 创建自已的字面值,例如下面这段代码

#include <iostream>long double operator"" mm(long double x) { return x / 1000; }long double operator"" m(long double x)  { return x; }long double operator"" km(long double x) { return x * 1000; }int main(){    std::cout << 5.0mm << '\n';    std::cout << 1.0m  << '\n';    std::cout << 1.0km << '\n';}

输出是

0.00511000


Question 12

Copy constructors must use call by reference because
A. otherwise the constructor will only make a copy of a pointer to an object.
B. otherwise the program will get trapped in infinite recursion
C. the copy created using call-by-value has function scope.
D. the pointer needs to know the address of the original data, not a temporary copy of it.

Standard Answer: B
拷贝构造函数如果选择按值传递的方式,在传入参数的时候又会调用一遍它自身,从而无限循环调用下去。

Question 13

Given the code “int a = 3; int &p = a;”, what is the value of p?
A. The address of a
B. The address of p
C. 3
D. None of the mentioned

Standard Answer: C

Question 14

What is the printout of the following program?

class Printer{public:    Printer(std::string name) {std::cout << name;}};class Container{public:    Container():b("b"), a("a"){}    Printer a;    Printer b;  };int main() {    Container c;    return 0;}

A. May be “ab” or “ba” depending on the specific implementation
B. Always is “ba”
C. Always is “ab”

Standard Answer: C
对象成员构造函数的调用顺序按照其声明顺序

Question 15

With the class Base and its inherited class Inherited, which statement will have a compilation error?
A. Base * base = Inherited();
B. Base * base = new Inherited();
C. Base base = Inherited();
D. const Base & base = Inherited();

Standard Answer: A

Question 16

Which of the following about constructor/destructor chaining is NOT correct?
A. The constructor of an inherited class must call one of the constructors in its base class
B. A constructor need to specify a base class’s constructor if the base class has no default constructor.
C. The destructor in an inherited class is called before that of its base class.
D. The constructor in an inherited class is called before that of its base class.

Standard Answer: D
1、构造函数的调用顺序
基类构造函数、对象成员构造函数、派生类本身的构造函数
2、析构函数的调用顺序
派生类本身的析构函数、对象成员析构函数、基类析构函数(与构造顺序正好相反)

Question 17

Suppose you declare variables as below, which of the following statements is true?

int i = 10;int *pi = &i;int &ri = i;

A. *pi is 10
B. &i is 10
C. pi contains the value of i
D. i contains the address of i

Standard Answer: A

Question 18

If two functions within the same class have the same name, what happens?
A. Either one will be called depending on the parameters.
B. Neither function will be called
C. This is dead code and a very difficult problem to debug.
D. Both functions will be called in order. This is named multiple inheritance.

Standard Answer: A

Question 19

Assume that in the run-time environment, an instance of int type occupies 4 bytes. Given the class Min2Max declared as following, the value of sizeof(Min2Max) is .

class Min2Max {public:    int getMax();    void setMax(int m);    int getMin();    void setMin(int m);private:    static int std;    int max, min;};

A. 4
B. 8
C. 12
D. 16

Standard Answer: B
计算一个类对象的大小时的规律:
1. 空类、单一继承的空类、多重继承的空类所占空间大小为:1(字节,下同);
2. 一个类中,虚函数本身、成员函数(包括静态与非静态)和静态数据成员都是不占用类对象的存储空间的;
3. 因此一个对象的大小≥所有非静态成员大小的总和;
4. 当类中声明了虚函数(不管是1个还是多个),那么在实例化对象时,编译器会自动在对象里安插一个指针vPtr指向虚函数表VTable;
5. 虚承继的情况:由于涉及到虚函数表和虚基表,会同时增加一个(多重虚继承下对应多个)vfPtr指针指向虚函数表vfTable和一个vbPtr指针指向虚基表vbTable,这两者所占的空间大小为:8(或8乘以多继承时父类的个数);
6. 在考虑以上内容所占空间的大小时,还要注意编译器下的“补齐”padding的影响,即编译器会插入多余的字节补齐;
7. 类对象的大小=各非静态数据成员(包括父类的非静态数据成员但都不包括所有的成员函数)的总和+ vfptr指针(多继承下可能不止一个)+vbptr指针(多继承下可能不止一个)+编译器额外增加的字节。

Question 20

When a friend function overloads an operator, if its argument list does not contain any arguments, the operator is a _. (1)unary operator (2)binary operator
A. (1)
B. (2)
C. Both (1) and (2) are possible.
D. It would cause an error

Standard Answer: D
友元函数没有this指针,所以在参数列表里必须传入参数。

Question 21

Pick out the correct statement.
A. A friend function may be a member of another class.
B. A friend function may not be a member of another class.
C. A friend function may or may not be a member of another class.
D. None of the mentioned

Standard Answer: C
普通函数和类的成员函数都可以作为友元函数

Question 22

What is the output of this program?

#include <iostream>using namespace std;int main(){    char arr[20];    int i;    for(i = 0; i < 10; i++)        *(arr + i) = 65 + i + 1;    *(arr + i) = '\0';    cout << arr;    return(0);}

A. AAAAAAAAAA
B. JJJJJJJJJJ
C. ABCDEFGHIJ
D. None of the mentioned

Standard Answer: D
输出为BCDEFGHIJK

Question 23

Pick up all of the operators that can’t be overloaded? (1)::(2)?:(3)sizeof (4)[] (5).*
A. (1)(3)(4)(5)
B. (1)(4)(5)
C. All of the operators mentioned can be overloaded.
D. (1)(2)(3)(5)

Standard Answer: D

Question 24

A void pointer cannot point to which of these?
A. class member in c++
B. methods in c++
C. both class member in c++ and methods in c++
D. none of the mentioned

Standard Answer: A
在C++中,慎用void指针指向一个对象。

Question 25

Which constructor will initialize the base class data member?
A. derived class
B. base class
C. class
D. None of the mentioned

Standard Answer: B

原创粉丝点击