Top 50 C++ Interview Questions with Answers

C++ Interview Questions with Answers

1) What is the size of a character in C++?

a) 8 bits
b) 16 bits
c) 32 bits
d) 64 bits

Answer: a) 8 bits

2) Which of the following is the correct syntax for declaring a pointer?

a) int *p;
b) *int p;
c) int p*;
d) *p int;

Answer: a) int *p;

3) What is the output of the following code?

int a = 10, b = 20;
a = a ^ b;
b = a ^ b;
a = a ^ b;
std::cout << a << ” ” << b;
a) 20 10
b) 30 20
c) 10 20
d) 20 30

Answer: a) 20 10

4) Which of the following is a correct way to allocate an integer array of size 10 on the heap?

a) int array[10];
b) int *array = new int[10]();
c) int *array = new int(10);
d) int array = new int[10];

Answer: b) int *array = new int[10]();

5) What is the output of the following code?

int x = 10, y = 5;
std::cout << (x > y ? (x < 15 ? 1 : 2) : 0);
a) 0
b) 1
c) 2
d) Compilation error

Answer: b) 1

6) Which of the following statements is true about the const keyword in C++?

a) It can be used to declare a constant variable.
b) It can be used to declare a function parameter that cannot be modified.
c) It can be used to declare a function return value that cannot be modified.
d) All of the above

Answer: d) All of the above

7) Which of the following is the correct way to declare a virtual function in C++?

a) virtual int foo();
b) virtual int foo(void);
c) virtual int foo() = 0;
d) virtual int foo() const;

Answer: a) virtual int foo();

8) Which of the following is the correct syntax to open a file in C++?

a) std::filestream myFile(“filename”, mode);
b) std::fstream myFile(“filename”, mode);
c) std::ifstream myFile(“filename”, mode);
d) Both b and c

Answer: d) Both b and c

9) What is the output of the following code?

int a = 10, b = 5;
int &refA = a, &refB = b;
refA = refB;
std::cout << a << ” ” << b;
a) 5 10
b) 10 5
c) Compilation error
d) Runtime error

Answer: a) 5 10

10) Which of the following statements about templates in C++ is true?

a) They allow generic programming in C++.
b) They provide a way to define a common functionality for different data types.
c) They can be used to define functions or classes.
d) All of the above

Answer: d) All of the above

11) Which of the following is the correct syntax to pass an array to a function in C++?

a) void func(int * arr, int size);
b) void func(int arr[], int size);
c) void func(int arr[10], int size);
d) All of the above

Answer: b) void func(int arr[], int size);

12) What is the output of the following code?

int a = 10, b = 5, c = 2;
std::cout << (a > b) << ” ” << (a < b) << ” ” << (a == b) << ” ” << (a >= c) << ” ” << (b <= c);
a) 1 0 0 1 0
b) 1 0 0 0 0
c) 0 1 0 1 1
d) Compilation error

Answer: a) 1 0 0 1 0

13) What is a destructor in C++?

a) A function that is called automatically when an object is created.
b) A function that is called automatically when an object is destroyed.
c) A function that is called manually to destroy an object.
d) A function that is used to initialize an object.

Answer: b) A function that is called automatically when an object is destroyed.

14) Which of the following is the correct syntax to define a class in C++?

a) class MyClass { public: int foo(); };
b) class MyClass() { public: int foo(); };
c) class MyClass { int foo(); };
d) class { int foo(); };

Answer: a) class MyClass { public: int foo(); };

15) What is the output of the following code?

std::cout << sizeof(“hello world”);
a) 11
b) 12
c) 13
d) Compilation error

Answer: b) 12

16) Which of the following is the correct syntax to declare an enumeration in C++?

a) enum { RED, GREEN, BLUE };
b) enum colors { RED, GREEN, BLUE };
c) enum colors { RED = 1, GREEN = 2, BLUE = 3 };
d) Both b and c

Answer: d) Both b and c

17) What is the output of the following code?

int arr[] = {1, 2, 3, 4, 5};
std::cout << sizeof(arr) / sizeof(arr[0]);
a) 1
b) 2
c) 3
d) 5

Answer: d) 5

18) What is the output of the following code?

int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;
std::cout << *ptr << ” ” << *(ptr + 3) << ” ” << *(arr + 4);
a) 1 3 5
b) 1 4 5
c) 1 4 4
d) 1 4 1

Answer: b) 1 4 5

19) What is a template specialization in C++?

a) It is a way to define a function or class for a specific data type.
b) It is a way to define a function or class for a generic data type.
c) It is a way to define a function or class for multiple data types at once.
d) It is a way to define a function or class for a derived data type.

Answer: a) It is a way to define a function or class for a specific data type.

20) Which of the following is an example of dynamic polymorphism in C++?

a) Function overloading
b) Constructor overloading
c) Operator overloading
d) Virtual functions

Answer: d) Virtual functions

21) Which of the following is not a type of inheritance in C++?

a) Public inheritance
b) Protected inheritance
c) Private inheritance
d) Multiple inheritance
e) All of the above are types of inheritance

Answer: e) All of the above are types of inheritance

22) Which of the following is the correct way to declare a member function of a class outside the class definition in C++?

a) void MyClass::foo() { }
b) void foo() { }
c) MyClass::void foo() { }
d) MyClass::foo() { }

Answer: a) void MyClass::foo() { }

23) What is the output of the following code?

int a = 10, b = 20, c;
c = a++ + –b;
std::cout << a << ” ” << b << ” ” << c;
a) 10 20 30
b) 11 20 30
c) 10 19 30
d) 11 19 30

Answer: c) 10 19 30

24) Which of the following is the correct syntax to define a destructor in C++?

a) ~ MyClass { }
b) ~MyClass() { }
c) MyClass::~() { }
d) Both a and b

Answer: b) ~MyClass() { }

25) What is the output of the following code?

int a = 10, b = 20;
std::swap(a, b);
std::cout << a << ” ” << b;
a) 10 20
b) 20 10
c) Compilation error
d) Runtime error

Answer: b) 20 10

26) Which of the following is the correct syntax to create a thread in C++?

a) std::thread myThread = new std::thread(foo);
b) std::thread myThread(foo);
c) std::thread myThread = std::thread::create(foo);
d) None of the above

Answer: b) std::thread myThread(foo);

27) What is the output of the following code?

int arr[] = {1, 2, 3, 4, 5};
for (int i : arr) {
std::cout << i << ” “;
}
a) 1 2 3 4 5
b) 5 4 3 2 1
c) Compilation error
d) Runtime error

Answer: a) 1 2 3 4 5

28) Which of the following is a correct way to include a header file in C++?

a) #include “myFile.h”
b) #include
c) #include
d) Both a and b

Answer: d) Both a and b

29) What is the output of the following code?

int arr[] = {1, 2, 3, 4, 5};
std::cout << arr << ” ” << &arr[0];
a) Same memory address
b) Different memory addresses
c) Compilation error
d) Runtime error

Answer: a) Same memory address

30) Which of the following is the correct syntax to declare a constant pointer in C++?

a) int * const ptr = &x;
b) const int * ptr = &x;
c) const int * const ptr = &x;
d) Both a and c

Answer: d) Both a and c

31) What is the output of the following code?

int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;
std::cout << arr + 4 << ” ” << ptr + 4;
a) Same memory address
b) Different memory addresses
c) Compilation error
d) Runtime error

Answer: a) Same memory address

32) Which of the following is the correct syntax for a lambda function in C++?

a) []() { }
b) [](int x) { return x * x; }
c) [x, y](int z) { return x + y + z; }
d) All of the above

Answer: d) All of the above

33) Which of the following is the correct syntax to declare a base class in C++?

a) class Base { protected: int x; }
b) class Base : protected { int x; }
c) class Base { public: int x; }
d) class Base : public { int x; }

Answer: c) class Base { public: int x; }

34) What is a singleton in C++?

a) It is a design pattern that restricts the instantiation of a class to one object.
b) It is a class that can be instantiated multiple times.
c) It is a class that is derived from another class.
d) It is a function that takes a single argument as input.

Answer: a) It is a design pattern that restricts the instantiation of a class to one object.

35) What is the output of the following code?

int a = {10}, b = {20};
std::cout << a << ” ” << b;
a) 10 20
b) {10} {20}
c) Compilation error
d) Runtime error

Answer: a) 10 20

36) Which of the following is the correct syntax for a copy constructor in C++?

a) MyClass(MyClass &obj) { }
b) MyClass(MyClass obj) { }
c) MyClass(MyClass const &obj) { }
d) Both a and c

Answer: c) MyClass(MyClass const &obj) { }

37) What is the output of the following code?

int arr[] = {1, 2, 3, 4, 5};
int *ptr = &arr[2];
std::cout << ptr[-2] << ” ” << *(ptr + 2);
a) 1 5
b) 2 4
c) Compilation error
d) Runtime error

Answer: b) 2 4

38) Which of the following is the correct syntax to call a superclass constructor in C++?

a) SuperClass();
b) SuperClass::constructor();
c) SuperClass::SuperClass();
d) Both b and c

Answer: c) SuperClass::SuperClass();

39) What is the output of the following code?

int x = 10;
int *ptr1 = &x, *ptr2 = &x;
std::cout << (ptr1 == ptr2) << ” ” << (*ptr1 == *ptr2);
a) 1 1
b) 0 0
c) 1 0
d) Compilation error

Answer: a) 1 1

40) Which of the following is the correct syntax to declare a function that takes a variable number of arguments in C++?

a) void foo(…);
b) void foo(int …);
c) void foo(int, …);
d) Both b and c

Answer: d) Both b and c

41) What is the output of the following code?

include

int foo(int x) { return x * x; }
int main() {
int arr[] = {1, 2, 3, 4, 5};
std::transform(arr, arr + 5, arr, foo);
for (int i : arr) {
std::cout << i << ” “;
}
}
a) 1 4 9 16 25
b) 1 2 3 4 5
c) Compilation error
d) Runtime error

Answer: a) 1 4 9 16 25

42) Which of the following is a correct way to declare a constant reference in C++?

a) const int & ref = x;
b) int const & ref = x;
c) Both a and b
d) None of the above

Answer: c) Both a and b

43) What is the output of the following code?

int a = 10, b = 20;
swap(a, b);
std::cout << a << ” ” << b;
a) 10 20
b) 20 10
c) Compilation error
d) Runtime error

Answer: b) 20 10

44) Which of the following is the correct syntax for a move constructor in C++?

a) MyClass(MyClass &&obj) { }
b) MyClass(MyClass obj &&) { }
c) MyClass(MyClass const &&obj) { }
d) Both a and c

Answer: a) MyClass(MyClass &&obj) { }

45) What is the output of the following code?

int a = 10, b = 20;
int *ptr1 = &a, *ptr2 = &b;
std::swap(ptr1, ptr2);
std::cout << *ptr1 << ” ” << *ptr2;
a) 10 20
b) 20 10
c) Compilation error
d) Runtime error

Answer: b) 20 10

46) Which of the following is the correct syntax to call a virtual function in C++?

a) obj.foo();
b) MyClass::foo();
c) virtual obj.foo();
d) None of the above

Answer: a) obj.foo();

47) What is the output of the following code?

int a = 10, b = 20;
int *ptr1 = &a, *ptr2 = &b;
ptr2 = ptr1;
*ptr2 = 30;
std::cout << a << ” ” << b;
a) 10 20
b) 30 20
c) Compilation error
d) Runtime error

Answer: b) 30 20

48) Which of the following is a correct way to declare a static member in C++?

a) class MyClass { public: static int x; };
b) class MyClass { static int x; public: };
c) class MyClass { static int x(); };
d) Both a and b

Answer: a) class MyClass { public: static int x; };

49) What is the output of the following code?

int a = 10, b = 2;
double c = static_cast>(a) / b;
std::cout << c;
a) 5.0
b) 5
c) 5.00
d) Compilation error

Answer: c) 5.00

50) What is a virtual destructor in C++?

a) It is a function that is called manually to destroy an object.
b) It is a function that is used to initialize an object.
c) It is a function that is called automatically when an object is destroyed.
d) It is a function that is used to create an object.

Answer: c) It is a function that is called automatically when an object is destroyed.

Ashwani Kumar
Latest posts by Ashwani Kumar (see all)
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x