C++ Programming MCQ Interview Questions

1. Which of the following is not a fundamental data type in C++?

A) int

B) float

C) string

D) char

Answer: C) string

Explanation: In C++, string is not a fundamental data type; it’s part of the Standard Library and represents a sequence of characters. Fundamental data types include int, float, char, among others.

2. What does the ‘volatile’ keyword in C++ signify?

A) It indicates that a variable is constant and cannot be modified.

B) It instructs the compiler to avoid optimizations involving the variable.

C) It specifies the visibility of variables in different scopes.

D) It denotes a variable that can only be accessed by certain functions.

Answer: B) It instructs the compiler to avoid optimizations involving the variable.

Explanation: The volatile keyword in C++ tells the compiler that the variable may be modified externally, preventing certain optimizations, such as caching the variable’s value.

3. What is the output of the following code snippet?

#include <iostream>

using namespace std;

int main()

 {

    int x = 10;

    int &y = x;

    y = 20;

    cout << x << endl;

    return 0;

}

A) 10

B) 20

C) Compilation Error

D) Undefined Behavior

Answer: B) 20

Explanation: The code snippet creates a reference y to variable x. Modifying y changes the value of x as well. Hence, x is assigned the value 20, and the output will be 20.

4. What is the result of the following code snippet?

#include <iostream>

using namespace std;

int main()

 {

    int arr[5] = {1, 2, 3, 4, 5};

    int *ptr = arr;

    cout << *(ptr + 2) << endl;

    return 0;

}

A) 1

B) 2

C) 3

D) 4

Answer: C) 3

Explanation: The code initializes an integer array arr and a pointer ptr pointing to its first element. *(ptr + 2) dereferences the pointer to access the third element of the array, which is 3.

5. Which operator is used for dynamic memory allocation in C++?

A) new

B) malloc

C) alloc

D) alloc_mem

Answer: A) new

Explanation: In C++, the new operator is used for dynamic memory allocation, while malloc is a C function for the same purpose.

6. What is the output of the following code snippet?

#include <iostream>

using namespace std;

class Base

 {

public:

     virtual void display()

 {

         cout << “Base Display” << endl;

     }

};

class Derived : public Base

 {

public:

     void display() override

 {

         cout << “Derived Display” << endl;

     }

};

int main() {

    Base *ptr = new Derived();

    ptr->display();

    return 0;

}

A) Base Display

B) Derived Display

C) Compilation Error

D) Undefined Behavior

Answer: B) Derived Display

Explanation: The code creates a pointer ptr of type Base pointing to an object of Derived class. The display() method is overridden in the Derived class, so the output will be “Derived Display”.

7. What will be the output of the following code?

#include <iostream>

using namespace std;

void swap(int &a, int &b)

 {

    int temp = a;

    a = b;

    b = temp;

}

int main() 

{

    int x = 5, y = 10;

    swap(x, y);

    cout << “x: ” << x << “, y: ” << y << endl;

    return 0;

}

A) x: 5, y: 10

B) x: 10, y: 5

C) x: 0, y: 0

D) Compilation Error

Answer: B) x: 10, y: 5

Explanation: The swap() function exchanges the values of a and b. When swap(x, y) is called, x becomes 10, and y becomes 5.

8. Which statement is true about C++ references?

A) References cannot be null.

B) References can be re-assigned to refer to different variables after initialization.

C) References occupy additional memory space compared to pointers.

D) References are used for dynamic memory allocation.

Answer: A) References cannot be null.

Explanation: Unlike pointers, references cannot be null or uninitialized. They must be initialized when declared and cannot change the variable they refer to after initialization.

9. What does the ‘constexpr’ keyword signify in C++?

A) It specifies a function to be executed at compile time.

B) It is used to declare constants.

C) It denotes a function that can be overridden in derived classes.

D) It is used to allocate memory dynamically.

Answer: A) It specifies a function to be executed at compile time.

Explanation: The constexpr keyword indicates that a function or expression can be evaluated at compile time, allowing computations to be performed during compilation.

10. What is the output of the code snippet below?

#include <iostream>

using namespace std;

class A {

public:

    virtual void show() {

        cout << “Class A” << endl;

    }

};

class B : public A {

public:

    void show() {

        cout << “Class B” << endl;

    }

};

int main() {

    A *ptr = new B();

    ptr->show();

    return 0;

}

A) Class A

B) Class B

C) Compilation Error

D) Undefined Behavior

Answer: B) Class B

Explanation: The code creates a pointer of type A that points to an object of class B. Since show() is a virtual function, the function to call is determined at runtime based on the actual object type, resulting in “Class B” being printed.

11. Which among the following statements is correct regarding ‘iostream’ and ‘cstdio’ in C++?

A) ‘iostream’ and ‘cstdio’ both provide functions for console input and output.

B) ‘iostream’ and ‘cstdio’ are interchangeable and can be used interchangeably in any C++ program.

C) ‘iostream’ is used for console input and output, while ‘cstdio’ is used for file input and output.

D) ‘cstdio’ is used for console input and output, while ‘iostream’ is used for file input and output.

Answer: C) ‘iostream’ is used for console input and output, while ‘cstdio’ is used for file input and output.

Explanation: ‘iostream’ is specifically designed for input and output operations to and from the console, while ‘cstdio’ provides functions for file input and output operations in C++.

12. What does the following code snippet output?

#include <iostream>

using namespace std;

int main()

 {

    int x = 5;

    int *ptr = &x;

    cout << *ptr << endl;

    *ptr = 10;

    cout << x << endl;

    return 0;

}

A) 5, 10

B) 10, 10

C) 5, 5

D) 10, 5

Answer: A) 5, 10

Explanation: Initially, *ptr prints the value pointed by ptr, which is 5. Later, changing the value pointed by ptr to 10 (*ptr = 10) modifies the value of x, resulting in x being printed as 10.

13. What is the purpose of the ‘friend’ keyword in C++?

A) It signifies a function or class that can access private and protected members of another class.

B) It is used to declare a function or class inside another class.

C) It denotes a function or class that is inherited from a base class.

D) It specifies a function or class that cannot be overridden.

Answer: A) It signifies a function or class that can access private and protected members of another class.

Explanation: The ‘friend’ keyword allows a function or another class to access private and protected members of a class in which it’s declared as a friend.

14. What is a pure virtual function in C++?

A) A function that has no definition in the class declaration.

B) A function that cannot be overridden by derived classes.

C) A function that is defined in a base class and overridden in derived classes.

D) A function that can be called only by the base class.

Answer: A) A function that has no definition in the class declaration.

Explanation: A pure virtual function is declared in a base class but has no implementation and is meant to be overridden by derived classes.

15. What is the output of the following code snippet?

#include <iostream>

using namespace std;

int main() {

    int i = 0;

    for (; i < 5; ++i) {

        if (i == 3)

            break;

    }

    cout << i << endl;

    return 0;

}

A) 3

B) 4

C) 5

D) Compilation Error

Answer: A) 3

Explanation: The for loop increments i until it reaches 3 due to the if condition with break. When i becomes 3, the loop breaks, and the value of i (which is 3) is printed.

16. Which operator is used to access the member functions and variables of a class through a pointer in C++?

A) . (dot operator)

B) -> (arrow operator)

C) :: (scope resolution operator)

D) : (colon operator)

Answer: B) -> (arrow operator)

Explanation: The arrow operator (->) is used to access the member functions and variables of a class through a pointer to an object of that class.

17. What is the purpose of the ‘const’ keyword in C++?

A) It declares a constant variable that cannot be modified after initialization.

B) It specifies a function that doesn’t change the state of an object.

C) It denotes a class that cannot be inherited.

D) It creates a copy of an object for use in a function.

Answer: A) It declares a constant variable that cannot be modified after initialization.

Explanation: The ‘const’ keyword is used to create constants that cannot be modified after initialization, ensuring their immutability.

18. What will be the output of the following code snippet?

#include <iostream>

using namespace std;

int main() {

    int arr[] = {1, 2, 3, 4, 5};

    int *ptr = arr;

    cout << ptr[3] << endl;

    return 0;

}

A) 1

B) 2

C) 3

D) 4

Answer: D) 4

Explanation: ptr[3] is accessing the fourth element of the array arr using pointer arithmetic. As array indexing starts from 0, ptr[3] corresponds to the fourth element, which is 4.

19. Which statement regarding C++ namespaces is true?

A) Namespaces provide a way to define global variables in C++.

B) Namespaces prevent variable names from being used multiple times in a program.

C) Namespaces avoid naming conflicts by encapsulating code into a named scope.

D) Namespaces restrict access to functions within a program.

Answer: C) Namespaces avoid naming conflicts by encapsulating code into a named scope.

Explanation: Namespaces in C++ encapsulate code into named scopes, preventing naming conflicts by providing a way to group related code elements.

20. What is the function of the ‘typeid’ operator in C++?

A) It determines the type of a variable or an expression.

B) It checks the memory address of a variable.

C) It converts a variable from one data type to another.

D) It allocates memory dynamically.

Answer: A) It determines the type of a variable or an expression.

Explanation: The ‘typeid’ operator in C++ is used to determine the type of a variable or an expression at runtime.

21. What will be the output of the following code snippet?

#include <iostream>

using namespace std;

class Base {

public:

    Base() {

        cout << “Base Constructor” << endl;

    }

    ~Base() {

        cout << “Base Destructor” << endl;

    }

};

int main() {

    Base* ptr = new Base();

    delete ptr;

    return 0;

}

A) Base Constructor

B) Base Destructor

C) Base Constructor, Base Destructor

D) Base Destructor, Base Constructor

Answer: C) Base Constructor, Base Destructor

Explanation: The code dynamically allocates memory for an object of the Base class using new, calling the constructor. Later, delete is used to deallocate memory and calls the destructor.

22. Which of the following statements about function overloading in C++ is true?

A) Function overloading allows multiple functions with the same name but different return types.

B) Overloaded functions must have different names but can have the same number and types of parameters.

C) Function overloading is not allowed in C++.

D) Overloaded functions must have the same number of parameters but can have different types.

Answer: B) Overloaded functions must have different names but can have the same number and types of parameters.

Explanation: Function overloading allows multiple functions with the same name but different parameters.

23. What does the ‘static’ keyword signify in C++?

A) It specifies a function that is shared among all objects of a class.

B) It declares a variable that cannot be modified after initialization.

C) It defines a variable that retains its value across function calls.

D) It indicates a function or variable accessible only within the same source file.

Answer: C) It defines a variable that retains its value across function calls.

Explanation: In C++, the ‘static’ keyword for a variable in a function makes the variable retain its value between function calls.

24. What is the purpose of the ‘new’ operator in C++?

A) To deallocate memory dynamically.

B) To allocate memory for an object or variable dynamically.

C) To initialize a variable with a default value.

D) To declare a constant variable.

Answer: B) To allocate memory for an object or variable dynamically.

Explanation: The ‘new’ operator in C++ is used for dynamic memory allocation, allowing the allocation of memory for objects or variables during runtime.

25. Which of the following is true about inheritance in C++?

A) Inheritance allows a derived class to inherit all private members of the base class.

B) Multiple inheritance is not supported in C++.

C) Inheritance allows a derived class to inherit the constructors of the base class.

D) Inheritance restricts access to the protected members of the base class.

Answer: C) Inheritance allows a derived class to inherit the constructors of the base class.

Explanation: In C++, a derived class inherits the constructors and destructor of the base class, allowing the derived class to initialize inherited members.

26. What does the ‘this’ pointer refer to in C++?

A) It points to the previous object in a linked list.

B) It points to the current object itself.

C) It points to the base class object in an inheritance hierarchy.

D) It points to a static member of a class.

Answer: B) It points to the current object itself.

Explanation: The ‘this’ pointer in C++ is a pointer that holds the address of the current object when a member function is called.

For a more extensive range of MCQs or specialized questions in C++, utilizing dedicated platforms or resources specifically designed for programming assessments can offer a wider variety of topics and difficulty levels.

27. What is the output of the following code snippet?

#include <iostream>

using namespace std;

void modifyArray(int arr[]) {

    arr[0] = 10;

}

int main() {

    int arr[] = {1, 2, 3, 4, 5};

    modifyArray(arr);

    cout << arr[0] << endl;

    return 0;

}

A) 1

B) 2

C) 3

D) 10

Answer: D) 10

Explanation: Even though arrays are passed by reference-like behavior in C++, they decay into pointers when passed to functions. The function modifyArray modifies the first element of the array to 10, hence the output is 10.

28. Which operator is used to access the memory address of a variable in C++?

A) & (address-of operator)

B) * (dereference operator)

C) -> (arrow operator)

D) . (dot operator)

Answer: A) & (address-of operator)

Explanation: The & operator is used to obtain the memory address of a variable in C++.

29. What is the role of ‘try’, ‘catch’, and ‘throw’ in exception handling in C++?

A) ‘try’ is used to handle exceptions, ‘catch’ is used to throw exceptions, and ‘throw’ is used to begin exception handling.

B) ‘try’ is used to begin exception handling, ‘catch’ is used to handle exceptions, and ‘throw’ is used to throw exceptions.

C) ‘try’ is used to throw exceptions, ‘catch’ is used to begin exception handling, and ‘throw’ is used to handle exceptions.

D) ‘try’, ‘catch’, and ‘throw’ are unrelated to exception handling in C++.

Answer: B) ‘try’ is used to begin exception handling, ‘catch’ is used to handle exceptions, and ‘throw’ is used to throw exceptions.

Explanation: In C++, ‘try’ is used to start a block of code that may generate exceptions, ‘catch’ is used to handle those exceptions, and ‘throw’ is used to throw exceptions explicitly.

For a broader range of C++ MCQs or specific topics, resources like programming books, online platforms, or dedicated quiz websites can offer more comprehensive sets of questions and varying difficulty levels.

30. What is the output of the following code snippet?

#include <iostream>

using namespace std;

class A {

public:

    virtual void display() {

        cout << “A” << endl;

    }

};

class B : public A {

public:

    void display() {

        cout << “B” << endl;

    }

};

int main() {

    A *ptr = new B();

    ptr->display();

    return 0;

}

A) A

B) B

C) Compilation Error

D) Undefined Behavior

Answer: B) B

31. What is the purpose of the ‘inline’ keyword in C++?

A) It specifies that a function will be executed only if certain conditions are met.

B) It indicates that a function’s implementation will be provided later.

C) It suggests to the compiler to inline a function at the point of call.

D) It marks a function that can be overridden by derived classes.

Answer: C) It suggests to the compiler to inline a function at the point of call.

Explanation: The ‘inline’ keyword in C++ suggests to the compiler to perform inline expansion of a function at the point of call, potentially improving performance by avoiding function call overhead.

32. What is a reference variable in C++?

A) It is an alias to an existing variable.

B) It is a variable whose value cannot be changed after initialization.

C) It is a variable that cannot be used as a function argument.

D) It is a variable used to access memory addresses directly.

Answer: A) It is an alias to an existing variable.

Explanation: In C++, a reference variable is an alias or another name given to an existing variable.

33. What will be the output of the following code snippet?

#include <iostream>

using namespace std;

class Base {

public:

    virtual void show() {

        cout << “Base Show” << endl;

    }

};

class Derived : public Base {

public:

    void show() override {

        cout << “Derived Show” << endl;

    }

};

int main() {

    Base obj;

    Derived *ptr = dynamic_cast<Derived*>(&obj);

    if(ptr) {

        ptr->show();

    } else {

        cout << “Null Pointer” << endl;

    }

    return 0;

}

A) Base Show

B) Derived Show

C) Null Pointer

D) Compilation Error

Answer: C) Null Pointer

Explanation: Since obj is an object of type Base, attempting to cast it to Derived using dynamic_cast results in a null pointer because obj is not of type Derived.

34. What is the purpose of the ‘delete’ operator in C++?

A) To remove elements from a container.

B) To deallocate memory for an object or array allocated with ‘new’.

C) To remove a function from a class.

D) To delete a file from the file system.

Answer: B) To deallocate memory for an object or array allocated with ‘new’.

Explanation: In C++, the ‘delete’ operator is used to deallocate memory for an object or an array that was allocated using the ‘new’ operator.

35. Which of the following is true about the ‘virtual’ keyword in C++?

A) It specifies that a function cannot be overridden in derived classes.

B) It is used to declare a function in a base class.

C) It indicates that a function can be overridden in derived classes.

D) It is used to declare a function as private.

Answer: C) It indicates that a function can be overridden in derived classes.

Explanation: In C++, the ‘virtual’ keyword is used to declare a function in a base class that can be overridden in derived classes.

For a more extensive set of C++ MCQs or specific topics, referring to programming books, dedicated online platforms, or resources focusing on programming quizzes and assessments can offer a broader range of questions and varying difficulty levels.

36. What is the purpose of the ‘nullptr’ keyword in C++?

A) It is used to indicate a null value for pointers.

B) It is used to create a new pointer variable.

C) It is used to define a constant pointer.

D) It is used to access the memory address of a variable.

Answer: A) It is used to indicate a null value for pointers.

Explanation: ‘nullptr’ in C++ is a keyword introduced to explicitly represent a null pointer.

37. What is the purpose of the ‘const’ keyword in function declarations in C++?

A) It indicates that the function returns a constant value.

B) It signifies that the function cannot modify any variables passed to it.

C) It specifies that the function cannot be overridden in derived classes.

D) It denotes that the function does not accept any arguments.

Answer: B) It signifies that the function cannot modify any variables passed to it.

Explanation: When ‘const’ is used in a function declaration, it indicates that the function will not modify any of its parameters.

38. Which type of inheritance in C++ allows a class to inherit from multiple base classes?

A) Single Inheritance

B) Multiple Inheritance

C) Hierarchical Inheritance

D) Multilevel Inheritance

Answer: B) Multiple Inheritance

Explanation: Multiple Inheritance in C++ allows a class to inherit from more than one base class.

39. What will be the output of the following code snippet?

#include <iostream>

using namespace std;

class A {

public:

    void print() {

        cout << “A” << endl;

    }

};

class B : public A {

public:

    void print() {

        cout << “B” << endl;

    }

};

int main() {

    B obj;

    A *ptr = &obj;

    ptr->print();

    return 0;

}

A) A

B) B

C) Compilation Error

D) Undefined Behavior

Answer: A) A

Explanation: The function print() is not virtual in class A, so when the pointer of type A points to an object of class B and ptr->print() is called, the base class function A::print() is invoked.

40. What is the use of the ‘static’ keyword when applied to class members in C++?

A) It indicates that a class member is accessible only within its own class.

B) It specifies that a class member is shared among all instances of the class.

C) It prevents the initialization of class members.

D) It defines a constant class member.

Answer: B) It specifies that a class member is shared among all instances of the class.

Explanation: When ‘static’ is applied to class members in C++, it means that the member is shared among all instances of the class and can be accessed without creating an object of the class.

41. What is the role of the ‘override’ specifier in C++?

A) It indicates that a function will be defined later in the code.

B) It ensures that a function from a base class is overridden in a derived class.

C) It denotes that a function can be called from anywhere in the code.

D) It specifies that a function cannot be overloaded.

Answer: B) It ensures that a function from a base class is overridden in a derived class.

Explanation: The ‘override’ specifier in C++ explicitly declares that a function in a derived class is intended to override a virtual function in the base class, preventing accidental mismatch or mistakes in function signatures.

42. What will be the output of the following code snippet?

#include <iostream>

using namespace std;

class Base {

public:

    virtual void display() {

        cout << “Base Display” << endl;

    }

};

class Derived : public Base {

public:

    void display() override {

        cout << “Derived Display” << endl;

    }

};

int main() {

    Base obj;

    Derived* ptr = dynamic_cast<Derived*>(&obj);

    if (ptr) {

        ptr->display();

    } else {

        cout << “Null Pointer” << endl;

    }

    return 0;

}

A) Base Display

B) Derived Display

C) Null Pointer

D) Compilation Error

Answer: C) Null Pointer

Explanation: The dynamic_cast operation from Base* to Derived* fails since the object obj is of type Base. Thus, the pointer ptr becomes a null pointer and the condition in the if statement evaluates to false, resulting in “Null Pointer” being printed.

43. In C++, what is a ‘friend’ function?

A) A function declared within a class that cannot access private members of the class.

B) A function defined outside the class that has access to private and protected members of the class.

C) A function declared with the ‘static’ keyword in a class.

D) A function that can only be accessed within the same source file.

Answer: B) A function defined outside the class that has access to private and protected members of the class.

Explanation: A ‘friend’ function in C++ is a function declared outside the class that has access to private and protected members of the class.

44. What is the purpose of the ‘const’ member function in C++ classes?

A) To declare a constant object of the class.

B) To ensure that the member function does not modify the object’s data members.

C) To define a function that returns a constant value.

D) To specify that the function can only be called by const objects.

Answer: B) To ensure that the member function does not modify the object’s data members.

Explanation: A ‘const’ member function in a class ensures that the function does not modify the object’s data members.

45. What will be the output of the following code snippet?

#include <iostream>

using namespace std;

class A {

public:

    A() { cout << “A”; }

    ~A() { cout << “~A”; }

};

class B : public A {

public:

    B() { cout << “B”; }

    ~B() { cout << “~B”; }

};

int main() {

    B obj;

    return 0;

}

A) ABBA

B) BA

C) AB

D) BA~A

Answer: A) AB~BA

46. Which of the following is true regarding function templates in C++?

A) Function templates allow different functions to be defined for each data type.

B) Function templates allow defining a single function that can operate with different data types.

C) Function templates are used to define functions in derived classes.

D) Function templates restrict the usage of certain data types.

Answer: B) Function templates allow defining a single function that can operate with different data types.

Explanation: Function templates in C++ allow the definition of a single function that can be used with different data types, allowing for code reusability.

47. What is the purpose of the ‘std::move()’ function in C++?

A) To move an object from one memory location to another.

B) To convert an object into a constant.

C) To enable the use of move constructors in a class.

D) To convert an lvalue to an rvalue.

Answer: D) To convert an lvalue to an rvalue.

Explanation: std::move() in C++ is used to convert an lvalue into an rvalue, enabling move semantics and facilitating efficient transfer of resources.

48. What will be the output of the following code snippet?

#include <iostream>

using namespace std;

class Base {

public:

    virtual void display() {

        cout << “Base Display” << endl;

    }

};

class Derived : public Base {

public:

    void display() override {

        cout << “Derived Display” << endl;

    }

};

int main() {

    Base *ptr = new Derived();

    Derived *dptr = dynamic_cast<Derived*>(ptr);

    if (dptr) {

        dptr->display();

    } else {

        cout << “Null Pointer” << endl;

    }

    return 0;

}

A) Base Display

B) Derived Display

C) Null Pointer

D) Compilation Error

Answer: B) Derived Display

Explanation: The dynamic_cast operation from Base* to Derived* succeeds since the object being pointed to (ptr) is of type Derived. Therefore, the condition in the if statement evaluates to true, and “Derived Display” is printed.

49. What is a lambda function in C++?

A) A function used for recursive calls within a class.

B) A function pointer that allows dynamic function invocation.

C) A function used to define pure virtual functions in a class.

D) An anonymous function that can be used as a closure or an inline function.

Answer: D) An anonymous function that can be used as a closure or an inline function.

Explanation: In C++, a lambda function is an anonymous function that can be used as a closure or an inline function.

50. What is the purpose of the ‘std::vector’ container in C++?

A) To store elements in a sorted manner.

B) To store a fixed number of elements.

C) To store elements in a resizable array-like structure.

D) To store elements in a key-value pair.

Answer: C) To store elements in a resizable array-like structure.
Explanation: std::vector in C++ is a container that stores elements in a dynamic array-like structure, allowing for easy resizing and manipulation of elements.

Enquire Now

Enquire Now

Enquire Now

Please Sign Up to Download

Please Sign Up to Download

Enquire Now

Please Sign Up to Download

Enquiry Form