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 ...
随机推荐
- 解决浏览器跨域限制方案之WebSocket
WebSocket是在HTML5中引入的浏览器与服务端的通信协议,可以类比HTTP. 可以在支持HTML5的浏览器版本中使用WebSocket进行数据通信,常见的案例是使用WebSocket进行实时数 ...
- Baidu地图Map api直接加https不起作用
这种情况一般出现在v1.1等老版本上,加s=1也不起作用,尽量使用新版本. https://api.map.baidu.com/api?v=2.0&ak=你的密钥&s=1:
- Groovy 设计模式 -- 借贷
借贷模式 http://groovy-lang.org/design-patterns.html#_loan_my_resource_pattern The Loan my Resource patt ...
- 【noip 2011】提高组Day1T3.Mayan游戏
Description Mayan puzzle是最近流行起来的一个游戏.游戏界面是一个7行5列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上.游戏通关是 ...
- IIS服务器的安全保护措施
转载自:https://www.williamlong.info/archives/118.html 部分内容做了修改. 通过标注Web服务器安全级别以及可用性的安全策略,网络管理员将能够从容地在不同 ...
- 灾难性遗忘(catastrophic forgetting)
Overcoming catastrophic forgetting in neural networks(克服神经网络中的灾难性遗忘) 原文: https://www.pnas.org/conten ...
- webpack 配置全局 jQuery 对象
将 lodash 添加到当前模块的上下文中 import _ from 'lodash' 但是你想每个模块都引入的话就特别麻烦,这里有插件可以帮助到您,只需在 webpack.config.js 中配 ...
- python 的基础 学习 第三
1,in ,not in 判断子元素是是否在原字符串(字典,列表,集合)中,主要是用在检测敏感字 print('a' in 'abcchhhhd') 有则返回True, print('j' in 'a ...
- 使用cross-env解决跨平台设置NODE_ENV的问题
使用方法: 安装cross-env:npm install cross-env --save-dev 在NODE_ENV=xxxxxxx前面添加cross-env就可以了.
- Spring 源码分析 spring-core 篇
先来看下 spring-core 的包结构 总共有6个模块,分别是 asm.cglib.core.lang.objenesis.util asm包: 用来操作字节码,动态生成类或者增强既有类的功能.主 ...