100 C++ Interview Questions

1. What is OOP?

2. What is a Class?

class Window {public:
Window();
~Window();
};

3. What is an Object?

Window window;

4. List the types of inheritance supported in C++

Single Inheritance
class base_class {
// code
};
class derived_class : <access modifier> base_class {
// code
};
int main() { base_class object1; derived_class object2;}
class base_class1 {
// code
};
class base_class2 {
// code
};
class derived_class : <access modifier> base_class1, <access modifier> base_class2 {
// code
};
int main() {derived_class object;}
Hierarchical Inheritance
class base_class {
// code
};
class derived_class1 : <access modifier> base_class {
// code
};
class derived_class2 : <access modifier> base_class {
// code
};
class derived_class3 : <access modifier> base_class {
// code
};
int main() {derived_class object;}
Multi Level Inheritance
class base_class {
// code
};
class derived_class1 : <access modifier> base_class {
// code
};
class derived_class2 : <access modifier> derived_class1 {
// code
};
int main() {derived_class2 object;}

5. What is the role of the protected access specifier?

6. What is encapsulation?

7. What is abstraction?

8. What is an inline function?

Class A {
public:
inline int add(int a, int b) { return (a + b); }
};
Class A {
public:
int add(int a, int b);
};inline int A::add(int a, int b) { return (a + b); }

9. What is a storage class?

void myFunction() {
int a;
auto int b;
}
{
register int miles;
}
#include <iostream>
int count ;
extern void write_extern();

main() {
count = 5;
write_extern();
}
#include <iostream>

extern int count;

void write_extern(void) {
std::cout << "Count is " << count << std::endl;
}
class A
{
public:
A() : x(4), y(5) { };
mutable int x;
int y;
};

int main()
{
const A var2;
var2.x = 345;
// var2.y = 2345;
}

10. What is a virtual function?

11. What is a pure virtual function?

virtual void display()= 0;
using namespace std;
class Base {
public:
virtual void show() = 0;
};class Derived : public Base {
public:
void show() {
std::cout << "Derived class is derived from the base class" << std::endl;
}};

12. What are the differences between references and pointers?

13. What is polymorphism?

14. What is the this pointer?

15. What is a VTABLE and VPTR?

16. Distinguish between shallow copy and deep copy

17. What is an abstract class in C++?

class abstractClass {public:      virtual void show() = 0;
};

18. What is the role of static member keyword on class member variable?

19. Name the data type which can be used to store wide characters in C++

20. What is/are the operator/operators used to access the class members?

21. What is function overloading?

22. What is operator overloading?

class Account {    Account operator+(const Account& account) {
Account newAccount;
newAccount.users = this->users + account.users;
newAccount.profit = this->profit + account.profit;
}
};

23. Do we have a String primitive data type in C++?

24. Name the default standard streams in C++.

#include <iostream>
using namespace std;
int main() {
cout << "hello world!" << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int input = 0;
cout << "Enter a number: " << endl;
cin >> input;
return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
int main() { char filename[] = "data.txt";
if stream infile(fileName);
if(infile)
cout << infile.rdbuf();
else
cerr << "Error while opening the file " << fileName << endl;
return 0;}
#include <iostream>
#include <fstream>
using namespace std;
int main() {
char fileName[] = "data.txt";
ifstream infile(fileName);
if(infile)
cout << infile.rdbuf();
else
clog << "Error while opening the file " << fileName << endl;
return 0;
}
#include <iostream>
#include <fstream>
int main() {
std::ofstream out("test.txt");

// get the rdbuf of clog
// we need it to reset the value before exiting
auto old_rdbuf = std::clog.rdbuf();
// set the rdbuf of clog
std::clog.rdbuf(out.rdbuf());
// write to clog
// The output should go to test.txt
std::clog << "Test test test/n";
// reset the rdbuf of clog
std::clog.rdbuf(old_rdbuf);
return 0;}

25. What are pros and cons of polymorphism?

26. What is a namespace?

std::vector<std::string> vec;
using std::string
using namespace std;
namespace MyNameSpace {
class MyClass {
public:
void doSomething();
};
void Func(MyClass) {}
}
MyNameSpace::MyClass object;
object.doSomething();
MyNameSpace::Func(object);
using MyNameSpace::MyClass;MyClass object;
object.doSomething();
Func(object);

Bonus 1. Explain the purpose of the volatile keyword

volatile uint16_t x;
uint16_t volatile x;
volatile uint8_t * p_reg;
// OR
uint8_t volatile * p_reg;
uint16_t * volatile * volatile p_y;

Want to hire me? Summary of my recent work can be found on this account. More info at ashleyntharp.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store