Getting Started
Q1
The source code of program is as follows:
1 | int main(int argc, char *argv[]) |
The source code has been compiled to program hello, when you run it as follows, what argc
will be?
1 | ./hello I LOVE CPP |
Answer: 4
4个command line arguments分别是: ./hello, I, LOVE, CPP
Q2
The following code is the prototype/declaration of function mul.
1 | int mul(int a, int b) |
Answer: False
这段代码是定义(definition)而非声明(prototype/declaration)
Q3
The data type of the variable PI
is double
1 |
Answer: False
宏不是变量(variable)
Q4
When a function prototype is declared in the header file you create, you do NOT need to define it in a CPP file.
Answer: False
头文件里的声明+CPP文件的定义才能使用函数。
Q5
IDE (Integrated development environment) is a powerful compiler with many userful features.
Answer: False
集成开发环境可不只是编译器啊
Q6
If you get 75 for one of your projects, which situation should most likely be?
Answer: Finish all tasks
为什么要复习这个啊=-=
Data Types and Arithmetic Operators
Q1
1 | signed char c1 = 127; |
What’s the output of the source code?
Answer: 128
为了计算,先转换成了int再做加法,结果储存进int型变量避免了溢出。
整数运算只有int和long long两类运算,所以下面那段代码会输出-2147483648,就是先按int型计算溢出后再储存进long long。
1
2
3
4
5 int x = INT_MAX;
int y = 1;
long long p = x + y;
cout << p;
//output: -2147483648
Q2
sizeof()
is a function and can yield the size in bytes of a type.
Answer: False
sizeof
是个操作符,不是函数!
Q3
size_t
is an unsigned integer type.
Answer: True
size_t 是无符号整数类型, 32 bit or 64 bit
Q4
What’s the value of the variable num
?
1 | int num = 23 / 4 * 4; |
Answer: 20
(int)23/4=5, (int)5*4=4.
Q5
What’s the value of variable num
?
1 | int num = 23 / 4. * 4; |
Answer: 23
23/(double)4.=5.75, 5.75*4=(double)23.00, (int)23.00=23
Q6
What’s the value of variable num
?
1 | int num = 23 / 4 * 4.; |
Answer: 20
(int)23/4=5, 5*(double)4.=(double)20.00, (int)20.00=20
Q7
In the following code, since the variable num is not initialized explicitly, it will be initialized to 0 automatically.
1 | int num; |
Answer: False
在某些平台,未初始化的变量可能拥有随机值
Q8
auto is a placeholder type specifier in C++11. What is the value of the variable val
in the following code?
1 | auto val = 2 / 3; |
Answer: 6
auto=int
Branching and Looping Statements
Q1
What’s the output of the following source code?
1 | int x = 100; |
Answer: 120
先执行括号里的,再执行括号外的
Q2
What’s the output of the following source code?
1 | int x = 100; |
Answer: 100
自增在后,先赋值再自增
Q3
What’s the output of the following source code:
1 | int x = 100; |
Answer: 200
先执行括号里的,再执行括号外的
Q4
What’s the output of the following source code?
1 | int a = 2, b = 0; |
Answer: 0
逻辑短路,自增不执行
Q5
What’s the output of the following source code?
1 | int i = 0; |
Answer: 101
循环结束条件i<100不满足时i=101
Q6
What’s the output of the source code
1 | int x = 5; |
Answer: 0
执行了一次就润了
Q7
The following source code (empty in the parentheses) can be compiled successfully.
1 | for() |
Answer: False
for至少要有条件项
Q8
What is the output of the following code?
1 | int num = 5; |
Answer: Undefined key.
注意char型的’5’≠int型的5
Q9
How many lines will be printed?
1 | for(size_t i = 10; i >= 0; i--) |
Answer: $\infty$
size_t 自减到负数之后会“溢出”到最大整数,死循环
Data Structures
Q1
What’s the output of the source code?
1 | int idx = 0; |
Answer: Unpredictable result
负下标越界,编译是能过,运行时要么段错误要么非法访问其他内存
Q2
What’s the output of the source code?
1 | int idx = 0; |
Answer: Unpredictable result
下标越界,编译是能过,运行时要么段错误要么非法访问其他内存
Q3
What’s the output if you compile the following source code with C++11 standard?
1 | string str{"hello"}; |
Answer: e
str[1]是第二个元素
Q4
What’s the output?
1 | char str[16] = {"C++"}; |
Answer: 3
赋值的时候末尾带了个’\0’,strlen读到’\0’就停了
Q5
What’s the output?
1 | char str1[16] = {"C++"}; |
Answer: Compilation error.
数组相当于常量指针,不能直接对其赋值
Q6
What’s the output?
1 | string str1 = {"C++"}; |
Answer: C++
但是string可以直接赋值
Q7
What’s the output?
1 | union twonumbers |
Answer: 0
union各成员共用一块内存,所占用的内存长度等于最长的成员的内存长度,通过n修改为0后用d访问也是0
Q8
The output of the following code is:
1 | struct Person |
Answer: 12
虽然bool和char都只占用一字节,但被占用4字节的int分隔开,结构体会为了对齐而给bool和char也分配4字节,总共12字节
Pointers and Dynamic Memory Management
Q1
The following code can be compiled successfuly.
1 | double value = 0.0; |
Answer: True
type *const代表指针是常量,但指针所指的数据可以修改
Q2
The following source code is correct and cannot cause bugs.
1 | int *pint = (int *)malloc(8 * sizeof(int)); |
Answer: True
指针类型的转换,通过指针操作内存均合法,没有越界等问题
Q3
The following code can be compiled successfuly.
1 | double value = 0.0; |
Answer: True
const type p和type const p表示指针所指内容是常量,无法通过指针更改
但可以通过其他方式修改内容
Q4
The following source code is correct.
1 | int *ptr; |
Answer: False
不能对未分配内存的指针进行赋值
Q5
What’s the output of the following code on 64bit OS and CPU?
1 | int *numbers = new int[8]; |
Answer: 8
64位环境下,指针长度为8字节
Q6
What’s the output of the following code?
1 | int *numbers = new int[8]; |
Answer: 10
第三行给number[0]赋值0x0A0B0C0D,一个int对应四个char,pc[3]代表其中第四个字节,即0x0A,转换后为10
Q7
What’s the output of the following code on 64bit OS and CPU?
1 | int numbers[8]; |
Answer: 32
对于数组,sizeof会返回其所指内存的大小
Basics of Functions
Q1
What’s the output of the following code?
1 | float foo(float *p, int n) |
Answer: 3 5 1 1
四个数分别为 v[0], v[0]+2, v[2], v[2],注意优先级
Q2
What’s the output of the following source code?
1 | struct people |
Answer: 0
p作为引用参数传入init,age被修改为0
Q3
What’s the output?
1 | int area(int &x) |
Answer: 100
n作为引用参数传入area,乘方
Q4
What’s the output of the following code?
1 | struct people |
Answer: Compilation error
init函数要求传入指针,但传入的是people结构体变量
Q5
What’s the output of the following code?
1 | float area(float &x) |
Answer: 9
3.0f*3.0f=9.0f
Q6
What’s the output of the following code?
1 | float area(float &x) |
Answer: 9 9
通过引用修改了x,并返回了修改后的x
Q7
What’s the ouput of function sum()?
1 | int main() |
Answer: 4 or 8
传入参数实际上是指针,指针占用字节数可能是4或8
Q8
What’s the output of the following code?
1 | float area(float *x) |
Answer: Compilation error
要求传入浮点型指针,实际传入浮点型变量
Q9
What’s the output of the following code?
1 | struct people |
Answer: -1
函数参数是结构体变量,在传入后是对变量的拷贝进行操作,不影响变量本身
Q10
What’s the output of the following code?
1 | struct people |
Answer: 0
传入参数是指针,通过指针对结构体成员变量age进行操作
Advances in Functions
Q1
The following declaration correctly defines some default arguments
1 | int harpo(int n = 3, int m, int k = 3); |
Answer: False
所有默认参数必须集中放在最后
Q2
The functions and a function pointer are declared as follows. Which answers are correct?
1 | float norm(float x, float y); // declaration |
Answer:
1
2
3
4
5
6 norm_ptr = &norm;
(*norm_ptr)(-3.0f, 4.0f);
norm_ptr(-3.0f, 4.0f);
norm_ptr = norm;
(*norm_ptr)(-3.0f, 4.0f);
norm_ptr(-3.0f, 4.0f);大概意思就是函数指针和函数名在使用时可以适当混用
Q3
Function overloading is that multiple functions share the same function name but different signatures as the two functions below:
1 | float foo(float arg); |
Answer: True
你说得对
Q4
There is a function template. The specialization is correctly implemented in the following code.
1 | template <typename T> |
Answer: False
特例化应该这么写:
1
2 template<>
Point sum(Point pt1, Point pt2)
Q5
The following code correctly defines a function template:
1 | template <typename T> |
Answer: True
你说得对
Basics of Classes
Q1
The this pointer points to the object and can be used to invoke a member as in the following code.
1 | class Person |
Answer: False
静态函数无法调用非静态对象的成员,逻辑上不可行
Q2
What’s the output of the following code?
1 | class Person |
Answer: Compilation error
class成员在未声明为public时默认private,无法被外部直接调用
Q3
What’s the output of the source code?
1 | class Hello |
Answer: Link error
在对类的静态变量做操作前,需要先在类外进行定义。
Q4
A class is declared as follows. Please select correct answers for creating a variable.
1 | class Stock |
Answer:
1
2
3
4 Stock st1;//OK
Stock st2("A",3,2.0f);//OK
Stock st3 = Stock("A",3,2.0f);//OK
Stock st4 = ("A",,2.0f);//Error无参构造器,含参构造器,默认参数不能间隔省略
Q5
What’s the output of the following source code?
1 | class Hello |
Answer: 4
用sizeof对类进行操作时,其大小为对齐后的非静态成员数据的类型大小之和
如果是空类,占1字节
如果含有虚函数,则额外占有4字节虚函数表大小
与构造、析构函数等成员函数、静态变量无关
Advances in Classes
Q1
Class Stonewt is declared as follows.
1 | class Stonewt |
Which function will be invoked by the following line of code ?
1 | Stonewt wt = 120; |
Answer: Stonewt(double lbs);
隐式转换后调用上述构造器,决定用哪个构造器发生在编译过程
Q2
Class Stonewt is declared as above.
Which function will be invoked by the following line of code ?
1 | Stonewt wt(120); |
Answer: Stonewt(double lbs);
梅开二度
Q3
Class Stonewt is declared as above.
Which function will be invoked by the following line of code ?
1 | wt = 120.0; //wt is an object of type Stonewt |
Answer: Stonewt(double lbs);
三连加关注,CS不迷路
Q4
Class Stonewt is declared as above.
Which function will be invoked by the following line of code ?
1 | double f = wt; //wt is an object of type Stonewt |
Answer: operator double() const;
隐式转换再赋值
Q5
We can change operators’ precedence by overloading.
Answer: False
运算含义可以改,但优先级改不了
Q6
If we define a memeber function as follows for class Time
1 | Time Time::operator*(double mult) const |
then we can calculate as follows
1 | a = 3.3 * b;//a and b are objects of type Time |
Answer: False
重载的乘号,左边Time右边double,反过来不行
Q7
Assignment operator ‘=’ can be overloaded by a non-member function.
Answer: False
赋值符是一个特例,只能作为成员函数被重载
Q8
If the friend function is defined as in the following source code, it is a member function in the class.
1 | class Time |
Answer: False
友元函数需要在类内声明,私有公有没区别,也可以在类内定义,但友元终究不是成员
Q9
operator+() overloads the + operator, and it can only be used for mathematical addition.
Answer: False
你想把加号重载成乘法运算符也没人拦你(
Q10
A conversion function is defined outside of the declaration of class Stonewt as follow.
1 | Stonewt::operator double() const |
Answer: It’s correctly defined.
冇问题
Dynamic Memory Management in Classes
Q1
Please read the following code and choose correct answers:
1 | class Person |
Answer:
- The code can be compiled without error.
- Runtime error.
- It can cause memory double free problem.
- It can cause memory leak.
编译是能过,但是由于p1和p2的析构函数会对name做两次delete,所以会双重释放运行错误,而且p1原本申请的char[128]会没有指针指向它,所以会有内存泄漏风险
Q2
If you do not define a default constructor for a class explicitly, then no default constructor for that class.
Answer: False
如果你没有显式定义任何一个构造器,类会隐含一个默认无参构造器
Q3
If assignment operator is not defined in class Person, the following code will invoke default assignment operator.
1 | p1 = p2 = p3; //p1, p2 and p3 are objects of type Person |
Answer: True
如果没有重载赋值符,则会调用默认赋值符
Q4
For class Person, which of the constructors is its default constructor?
Answer:
1 Person::Person(int n = 0);有参数,但是所有参数都有默认值,就是默认构造器
Q5
You can define two constructors as follows for class Person
1 | Person(){...} |
Answer: False
那么问题来了,猜猜看Person()会调用哪个?
Q6
For class Person, which of the constructors is a copy constructor?
Answer:
1 Person::Person(const Person & p);将常量引用作为参数的构造器是复制构造器
Class Inheritance
Q1
What’s the output?
1 | class Animal |
Answer: Dog 5
由于覆写了虚函数print(),主函数语句调用的是Dog::print(),而Dog::print()再调用了Animal::print()
Q2
What is the output?
1 | class Animal |
Answer: 5
由于p是Animal类,其成员函数均为Animal类中的定义
Q3
What is the output?
1 | class Animal |
Answer: Compilation error
p是Animal类对象,speak()是其子类的函数,p的成员函数编译时静态绑定至Animal定义,因此p未定义speak()函数。
Class Templates and std Library
Q1
Please choose the right answer(s) for declaring a class template
Answer:
1
2
3
4 template <class Type>
class ClassName{...}
template <typename Type>
class ClassName{...}本来是class的,在众人要求下添加了typename关键字,于是两个都能用
Q2
Matx and Matx12f are declared in the following figure. Please choose the correct statement(s).
Answer: Matx is a class tempate, Matx12f is a template class.
不准确地来说,模板类是由类模板衍生出的类
Error Handling
Q1
A try block can be followed by multiple catch blocks.
Answer: True
你说得对,而且catch括号内的类型不会隐式转换,但是子类可以被父类catch
Q2
The following source code cannot be compiled successfully.
1 | double gmean(double a, double b) |
Answer: True
没有catch的try是不完整的
Q3
The following code cannot be compiled successfully since ‘try’ is commented.
1 | double gmean(double a, double b) |
Answer: False
throw了可以不try-catch,不过一抛就终止运行而已
Q4
When an exception is thrown, the program must be terminated.
Answer: False
如果抛的异常被catch住了,程序就可以继续运行catch内的内容