C++ Interview
1.- What will i and j equal after the code below is executed? Explain your answer.
int i = 5;
int j = i++;
2.- Consider the two code snippets below for printing a vector. Is there any advantage of one vs. the other? Explain.
Option 1:
vector vec;
/* ... .. ... */
for (auto itr = vec.begin(); itr != vec.end(); itr++) {
itr->print();
}
Option 2:
vector vec;
/* ... .. ... */
for (auto itr = vec.begin(); itr != vec.end(); ++itr) {
itr->print();
}
3.- Is there a difference between class and struct?
4.- How many times will this loop execute? Explain your answer.
unsigned char half_limit = 150;
for (unsigned char i = 0; i < 2 * half_limit; ++i)
{
// do something;
}
5.- What is encapsulation and when to use it?
6.- What is the difference between static and extern?
7.- What is difference between shallow copy and deep copy? Which is default?
8. What is realloc() and free()? What is difference between them?
9. Implement a template function IsDerivedFrom() that takes class C and class P as template parameters. It should return true
when class C is derived from class P and false otherwise.
10. What is the output of the following code:
#include <iostream>
class A {
public:
A() {}
~A() {
throw 42;
}
};
int main(int argc, const char * argv[]) {
try {
A a;
throw 32;
} catch(int a) {
std::cout << a;
}
}