most asked interview questions for C/C++
1. compared to prefix ++, postfix increment needs one more step to create a temporary variable? what's more , the return type is not reference, there will be another
temp var being created to store the return value.
int & operator++(int &a) { ++a; return a;}
int operator++(int &a){int b = a; ++a; return b;}
2. what's the difference between using quotes ("") and angle brackets(<>) when include file?
the first file location to start searching is current folder, then go to default folder, which can be set through env variables
3. difference among cerr, clog and cout?
cout is standard output which connected to console;
cerr is same to cout but for error message, clog is also for err msg, but it does have a buffer.
4. static language and dynamic language
A language is statically typed if the type of a variable is checked at compile time, this enable all checking be done by compiler.
A language is dynamically typed if the type is associated with run-time values, and not named variables/fields/etc. thus we don't have to specify the type every
time we use a variable
5. how to compare two floats/doubles
bool areEqualRel(float a, float b, float epsilon) {
return (fabs(a - b) <= epsilon * std::max(fabs(a), fabs(b)));
}
6. what is undefined behavior?
the behavior depends on the compiler implementation for the code, systems it runs on... , it's importable
6.1 initialization and assignment
for built-in types, they all assign variable with certain value (an uninitialized variable contains garbage). but for user-define types, sometimes, initialization
should allocate mem for pointer, while assignment should delete the exiting mem and allocate a new one.
7. define and declare
define will allocate memory for the variable, declare not.
with prefix keyword extern, to declare a variable, which defined in other place., complier don't have to know where it's located. useful when its share
among several modules.
8. function of keyword const
const indicate complier that this variable will not be modified, it can do some optimization for it. but it doesn't mean we can't modify the variable indirectly, it depends on
which segment compiler put it. for example, if it's stored in RO segment, OS will prevent any attempt to change it.
it's useful to prevent magic number, which may repeat many times.
all global default is extern, but a const global variable has a file scope, internal linakge
9. situation where union is used
union are used to construct a discriminator
structmy_variant_t{
int type;
union{
char char_value;
short short_value;
int int_value;
long long_value;
float float_value;
double double_value;
void* ptr_value;
};
};
* construct a new float variant instance */
void init_float(structmy_variant_t* v,float initial_value){
v->type = VAR_FLOAT;
v->float_value = initial_value;
}
/* Increments the value of the variant by the given int */
void inc_variant_by_int(structmy_variant_t* v,int n){
switch(v->type){
case VAR_FLOAT:
v->float_value += n;
break;
case VAR_INT:
v->int_value += n;
break;
...
}
}
10. how to use enumerator
enumerator is a type!! enum class is introduced in c++11; usually we will add a scope to enum, rather than make it global.
structDays
{
enum type
{
Saturday,Sunday,Tuesday,Wednesday,Thursday,Friday
};
};
Days::type day =Days::Saturday;
if(day ==Days::Saturday)
11. difference between #define and typedef
#define can do a lot of things besides typedef, typedef only do typedef, what's more , typedef have its scope!!
12. why there is a colon in the end of definition of class?
see code< int c; class A { int b; } c = 7; > compiler will mistake c as an object of class A;
13. getline will drop the std::endl it received and store other chars into buffer.
strlen will not count on the char '\0', similar to string.size()
if char a[] = "he", then a[2] = '\0'; to access array with index outside bound causes undefined behavior.
14. difference between #include "string.h" and #include <cstring>
the first one is in a C-type way, contents of both are same, but functions in the first one are not in namespace std!!
15. when do comparison in for() statement, why != is preferred than < ?
NOT EQUAL can be used to compared index array and iterators as well.
16. array is deprecated to use when vector is enough
17. why size_t matters
max value of size_t is defined as the largest number of element a array can have in certain OS. portable and readable
18. what's NULL
NULL is a preprocess sign, don't defined in the namespace std
19. reinterpret_cast: keep the original bits and treat it as another type
20. when parameter is an array reference , its size should be fixed!!! int func ( int ( &int )[10]);
21. return result of function will be used to initialize a temporary variable, if the type is not a reference
22. why func1 in subclass B will override same one in base class A;
same to local variable hide global, every class has a scope. compiler will start to find its suitable implement from its local scope
23. why ostream can't be copied!
ostream works like pipe, which connect its data source (usually buffers) and it’s destination, to copy an ostream will just get an empty pipe, no data will obtained.
That why all functions with parameters of type ostream should take a reference.
24. List all Container types in C++
A container is a holder object that stores a collection of other objects (its elements). They are implemented as class templates, which allows a great flexibility
in the types supported as elements.
i) Sequence containers:
Vector : random access, push/pop_back(), it’s inefficient to insert data at other position;
Deque : random access, push/pop_back & front, inefficient to insert data at other position
List : efficient to insert data, provide only linear access
Array : fixed-size array class
ii) Associative containers
std::sets contain only the key, while in std::map there is an associated value,If you want to build a dictionary of all the words that appear in a text, you could use a std::set<std::string>, but if you also want to count how many times each word appeared
iii) container adapeter
Container adaptors are not full container classes, but classes that provide a specific interface relying on an object of one of the container classes
Stack : first in last out
Queue : first in first out
Priority_queue: the greatest element among the remaining ones always keep in the top;
25. the implemention of reference is indeed a pointer, but we can’t depend it in the code
26. why should we initialize member variables in the constructor rather assign values to them?
No mater whether we initialize varialbles or not, it will be initialized( allocate mem and assign value if it have)
But for reference, constant variable, class object without default constructor, they must be initialized, and initialization order is same to that them declared
27. synthesized default constructor will be created only when there is no other constructor
28. what is indirect type conversion ( class)
Class A have constructor with parameter of type B, func1 of class A takes a parameter of class A, if we pass an object of class B, it will be converted to create
a temp object of class A; we can use explicit to prevent this
another method: define and conversion operator: operator TYPE(), no paremeters and return type
but when there are non-member operator function and member operator function, the last one was first used when compile
29. static is a global but has special access control, if a class member is static ,it should be initialized outside class,mem is allocated when it’s initialized.
30. when should we define copy constructor/ assignment operator fun
when we should allocate resource for class (member data), such as deep copy, if there is member variable which is a pointer.
31. friendship can’t be inherited, classic friend function, ostream & opearator<<(ostream &, const class &b)
if same function exits in both base class and subclass, it works, but no polymorphism will happen,
constructor will create an virtual-table( or several v-table when there are several base-class)
32. difference between static member function and non-static member function?
Compared to other normal function, class member function has a narrower scope and has pointer this as its
Parameter (static member function doesn’t)
All functions are a segment of machine-code. Its scope is used to help compiler to find its address.
anywhere in code a function is called, it will replaced by the function code or function address
33. If D inherited function1 from base classes C and B, both inherited from Class A?
Then D do have inherited two versions of function1, both are of different scope. If we just call function1 in class D, then compiler will fail to help to decide
which one should be used.
34. What is member function ?
same to global function, with a scope helping compiler to get its address. stored in text segemt
35. why constructor and assignment function should not be virtual?
When compile, to construct an object, a constructor needs the exact type of the object it is to create and it’s size. Before call constructor,
there is no virtual table and no pointer to object (haven’t be created).
assignment operator is not inherited. It may be re-defined (with different parameter type) by the implementer of the derived class or else it is automatically
synthesized by the compiler, so there's not much point in declaring it virtual.
36. Pure virtual function and virtual function
the only difference is that if a class has a pure virtual function, then it's abstract class, can't be instantiated.
if subclasses don't define the implementation, then this subclass can't be instantiated as well.
37. virtual inheritance
used to solve diamond problem. virtual inheritance will create a new special virtual table.

38. procedure to create and destroy an subclass object
assume class B inherited from class A, when construct an object of class B, it will call constructor of class A fist to create an A object and then call B constructor;
when destroy B object, call destructor of B, and then its type become type A, call destructor of A.
39. overload happens in the same scope, function with name (even with different signature) in local scope will hide global ones
40. when use template function, compiler will instantiate function according to arguments passed in.
but for template class, we should pass the type name to it
template<class T> parm fcn(T * array) { typename T::type * P;}
//use type name here to declare that it's a type , not an object multiply p;
41. how to compare two object (a,b) with <?
if (a<b) else if (b<a) else
what is instantiation of template?
waht is instantiate?
template specialization
what is stack unwind, when do this, local varialbe will be freed, but for those created by operator new
signed vs unsigned

most asked interview questions for C/C++的更多相关文章
- WCF学习系列二---【WCF Interview Questions – Part 2 翻译系列】
http://www.topwcftutorials.net/2012/09/wcf-faqs-part2.html WCF Interview Questions – Part 2 This WCF ...
- [译]Node.js Interview Questions and Answers (2017 Edition)
原文 Node.js Interview Questions for 2017 什么是error-first callback? 如何避免无止境的callback? 什么是Promises? 用什么工 ...
- WCF学习系列三--【WCF Interview Questions – Part 3 翻译系列】
http://www.topwcftutorials.net/2012/10/wcf-faqs-part3.html WCF Interview Questions – Part 3 This WCF ...
- WCF学习系列四--【WCF Interview Questions – Part 4 翻译系列】
WCF Interview Questions – Part 4 This WCF service tutorial is part-4 in series of WCF Interview Qu ...
- [转]Design Pattern Interview Questions - Part 4
Bridge Pattern, Composite Pattern, Decorator Pattern, Facade Pattern, COR Pattern, Proxy Pattern, te ...
- [转]Design Pattern Interview Questions - Part 2
Interpeter , Iterator , Mediator , Memento and Observer design patterns. (I) what is Interpreter pat ...
- [转]Design Pattern Interview Questions - Part 3
State, Stratergy, Visitor Adapter and fly weight design pattern from interview perspective. (I) Can ...
- [转]Design Pattern Interview Questions - Part 1
Factory, Abstract factory, prototype pattern (B) What are design patterns? (A) Can you explain facto ...
- 101+ Manual and Automation Software Testing Interview Questions and Answers
101+ Manual and Automation Software Testing Interview Questions and Answers http://www.softwaretesti ...
- 115 Java Interview Questions and Answers – The ULTIMATE List--reference
In this tutorial we will discuss about different types of questions that can be used in a Java inter ...
随机推荐
- [Android] Android 的singleLine废弃解决
之前写代码时,都没有注意singleLine已经废弃,每次想让TextView或Edittext单行显示都是直接使用,但是这样其实不好,因为废弃的函数可能在有的手机上出现问题,所以需要自己去找到替换的 ...
- windows10下TensorFlow安装记录
1.安装anaconda 安装最新版:https://repo.anaconda.com/archive/Anaconda3-5.3.0-Windows-x86_64.exe 加入环境变量: path ...
- maven打包可执行jar文件运行报错
起因 项目中同时依赖了Spring和MyBatis,并使用mybatis-spring集成MyBatis和Spring. 使用maven打包为可执行jar文件运行,打包插件为:maven-shade- ...
- Nginx 配置反向代理后,页面中取绝对URL地址的问题显示代理端口
本文有V型知识库提供 upstream tomcat { server 127.0.0.1:82;} location / { proxy_pass http://tomcat;} 如上 ...
- luogu 3582 线段树
线段树内存下mx[k]的值是动态的1-i这个区间的贡献答案 实际上点存的就是区间答案,但用max是为了求最大区间答案(有可能虽然贡献被消除但后来有更大的贡献填补答案空缺) #include<bi ...
- 【51nod 1191】消灭兔子
Description 有N只兔子,每只有一个血量B[i],需要用箭杀死免子.有M种不同类型的箭可以选择,每种箭对兔子的伤害值分别为D[i],价格为P[i](1 <= i <= M).假设 ...
- Linux 下磁盘挂载
Linux 磁盘挂载 新硬盘挂载: fdisk /dev/sdb p # 打印分区 d # 删除分区 n # 创建分区,(一块硬盘最多4个主分区,扩展占一个主分区位置.p主分区 e扩展) w # 保存 ...
- WIN10配置MongoDB
WIN10配置MongoDB 1. 下载 [MongoDB 官网下载链接](https://www.mongodb.com/download-center?jmp=nav#community) 2. ...
- Python18 Django 基础
本节内容 上节项目基础语法补充 Django请求的生命周期 通过这张图,我们可以很好的解释一下这个问题. Django请求的生命周期是由客户端的请求开始:经由路由系统找到相对应的视图函数:视图函数到h ...
- Javascript入门(五)数组操作、循环语句
一.数组与数组操作 <script type="text/javascript"> //数组定义方式 var list1 = new Array(1,2,3); var ...