50 C++ MCQ Questions: A Comprehensive Guide

The capabilities of the C programming language are expanded by the high-level programming language C++. It was created as a development of C to offer object-oriented programming (OOP) features while preserving the effectiveness and low-level capabilities of C. Because C++ and C were created to be interoperable, C++ programs can use and interact with C code. Here are 50 C++ MCQs related to C++ programming, along with their answers to understanding C++ effectively.

C++ MCQs


What does C++ stand for?

Explore Free Engineering Handwritten Notes!

Looking for comprehensive study materials on Python, Data Structures and Algorithms (DSA), Object-Oriented Programming (OOPs), Java, Software Testing, and more?

We earn a commission if you make a purchase, at no additional cost to you.

a) Creative C++

b) Computer C++

c) Common C++

d) C Plus Plus

Answer: d) C Plus Plus


Which of the following is NOT a fundamental data type in C++?

a) int

b) float

c) char

d) string

Answer: d) string


Which operator is used to access the member of a class through a pointer?

a) ->

b) .

c) ::

d) –

Answer: a) ->


In C++, the ‘cin’ object is used for:

a) Output

b) Input

c) Mathematical calculations

d) String manipulation

Answer: b) Input


What is the output of the following code snippet?

int x = 5;
int y = x++;
cout << x << " " << y;

a) 5 6

b) 6 6

c) 5 5

d) 6 5

Answer: a) 5 6


What is the correct way to dynamically allocate memory in C++?

a) new int;

b) malloc(sizeof(int));

c) allocate(int);

d) int* = new int;

Answer: a) new int;


Which C++ keyword is used to define a class?

a) class

b) define

c) struct

d) create

Answer: a) class


What is the purpose of a destructor in C++?

a) To create an object

b) To initialize an object

c) To destroy an object

d) To copy an object

Answer: c) To destroy an object


What is the scope resolution operator in C++?

a) ::

b) .

c) ->

d) :

Answer: a) ::


What is the correct way to include a header file named “iostream”?

a) include <iostream>

b) #include “iostream”

c) include iostream

d) #include <iostream>

Answer: d) #include


 What is the size of the ‘char’ data type in C++?

a) 1 byte

b) 2 bytes

c) 4 bytes

d) It varies based on the system

Answer: a) 1 byte


What is the result of the following code snippet?

int arr[3] = {1, 2, 3};
cout << arr[3];

a) 1

b) 2

c) 3

d) Undefined behavior

Answer: d) Undefined behavior


Which operator is used for dynamic_cast in C++?

a) >

b) ::

c) ::

d) ->

Answer: c) ::


In C++, which keyword is used to indicate that a class cannot be inherited from?

a) sealed

b) static

c) final

d) private

Answer: c) final


What is the correct way to declare a constant in C++?

a) const int x;

b) constant int x;

c) int x const;

d) int const x;

Answer: d) int const x;


What is the output of the following code snippet?

int x = 10;
if (x == 10)
{
    int y = 5;
}
cout << y;

a) 5

b) 10

c) Compiler error

d) Undefined behavior

Answer: c) Compiler error


What is the purpose of the ‘break’ statement in C++?

a) To terminate a loop

b) To skip an iteration

c) To exit a function

d) To continue to the next iteration

Answer: a) To terminate a loop


Which of the following is a preprocessor directive in C++?

a) using

b) typedef

c) namespace

d) #include

Answer: d) #include


What is the correct way to define a member function of a class outside the class definition?

a) void MyClass::Function()

b) function MyClass::Function()

c) void Function()::MyClass

d) void Function()::MyClass::

Answer: a) void MyClass::Function()


 What is the output of the following code snippet?

int x = 5;
int& y = x;
y++;
cout << x;

a) 4

b) 5

c) 6

d) Compiler error

Answer: c) 6


What does the ‘const’ modifier applied to a member function signify?

a) The function cannot be called

b) The function cannot modify the object’s state

c) The function cannot be defined outside the class

d) The function can only be called by const objects

Answer: b) The function cannot modify the object’s state


Which operator is used for pointer arithmetic in C++?

a) +

b) –

c) *

d) /

Answer: b) –


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

a) To represent an empty array

b) To initialize a pointer to zero

c) To indicate a null pointer

d) To allocate memory dynamically

Answer: c) To indicate a null pointer


What is the default access specifier for members of a class in C++?

a) private

b) public

c) protected

d) friend

Answer: a) private


What is the result of the following code snippet?

int x = 5;
int* ptr = &x;
cout << *ptr;

a) 5

b) 0

c) Memory address of x

d) Compiler error

Answer: a) 5


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

a) To indicate a function is private

b) To define a function within a class

c) To optimize code execution by suggesting inlining

d) To create an instance of a class

Answer: a) 5


What is the output of the following code snippet?

int x = 5;
int* ptr = &x;
*ptr = 10;
cout << x;

a) 5

b) 10

c) Memory address of x

d) Compiler error

Answer: b) 10


Which header file is required to use the ‘setw’ manipulator in C++?

a) <iomanip>

b) <ios>

c) <iostream>

d) <set>

Answer: b) 10


What is the purpose of the ‘typeid’ operator in C++?

a) To check the type of an object

b) To access a type’s information

c) To declare a new type

d) To define type aliases

Answer: a) To check the type of an object


What is the output of the following code snippet?

int arr[] = {1, 2, 3};
cout << sizeof(arr) / sizeof(arr[0]);

a) 3

b) 6

c) 0

d) Compiler error

Answer: a) 3


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

a) To create a new object

b) To allocate memory dynamically

c) To initialize a variable

d) To define a new data type

Answer: b) To allocate memory dynamically


Which of the following is an invalid identifier for a variable in C++?

a) myVariable

b) _myVariable

c) 1stVariable

d) MyVariable123

Answer: c) 1stVariable


What is the correct way to define a constant pointer in C++?

a) const int* ptr;

b) int* const ptr;

c) const ptr int;

d) int const* ptr;

Answer: b) int* const ptr;


What is the output of the following code snippet?

int x = 5;
int* ptr = &x;
int** ptr2 = &ptr;
cout << **ptr2;

a) 5

b) 0

c) Memory address of x

d) Compiler error

Answer: a) 5


What is the purpose of the ‘sizeof’ operator in C++?

a) To find the size of a data type in bytes

b) To calculate the sum of an array’s elements

c) To determine the address of a variable

d) To allocate memory for an object

Answer: a) To find the size of a data type in bytes


What is the output of the following code snippet?

int x = 5;
int* ptr = &x;
*ptr = 10;
int y = *ptr;
cout << y;

a) 5

b) 10

c) Memory address of x

d) Compiler error

Answer: b) 10


What is the purpose of the ‘virtual’ keyword in C++?

a) To indicate that a function is overridden

b) To make a class abstract

c) To specify a function as a friend

d) To optimize code execution

Answer: a) To indicate that a function is overridden


What is the correct syntax for creating an object of a class in C++?

a) object MyClass;

b) MyClass object;

c) create object MyClass;

d) object = MyClass;

Answer: b) MyClass object;


Which of the following is NOT a valid way to pass arguments to a function in C++?

a) Pass by value

b) Pass by reference

c) Pass by pointer

d) Pass by object

Answer: d) Pass by object


What is the output of the following code snippet?

int x = 5;
int* ptr = &x;
int& y = *ptr;
y = 10;
cout << x;

a) 5

b) 10

c) Memory address of x

d) Compiler error

Answer: b) 10


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

a) To remove a variable’s value

b) To deallocate memory allocated using ‘new’

c) To delete a function

d) To erase a class definition

Answer: b) To deallocate memory allocated using ‘new’


Which operator is used to access class members through an object?

a) :

b) .

c) ::

d) ->

Answer: b) .


Which operator is used to access class members through an object?

a) :

b) .

c) ::

d) ->

Answer: b) .


What is the output of the following code snippet?

int x = 5;
int* ptr = &x;
int y = *ptr;
y = 10;
cout << *ptr;

a) 5

b) 10

c) Memory address of x

d) Compiler error

Answer: a) 5


What is the purpose of the ‘this’ pointer in C++?

a) To access the base class members

b) To refer to the current object

c) To access global variables

d) To create a new instance of a class

Answer: b) To refer to the current object


Which keyword is used to inherit a base class into a derived class in C++?

a) derives

b) extends

c) inherits

d) class

Answer: d) class


What is the output of the following code snippet?

int x = 5;
int& y = x;
y = 10;
cout << x;

a) 5

b) 10

c) Memory address of x

d) Compiler error

Answer: b) 10


What is the purpose of the ‘default’ keyword in C++?

a) To create a default constructor

b) To define a default value for a variable

c) To specify a default access modifier

d) To initialize an object with default values

Answer: a) To create a default constructor


What is the correct syntax to create a pointer to a function in C++?

a) int* func();

b) func* int();

c) int (*func)();

d) func() int*;

Answer: c) int (*func)();


What is the purpose of the ‘do-while’ loop in C++?

a) To execute a block of code repeatedly as long as a condition is true

b) To execute a block of code at least once, and then repeatedly if a condition is true

c) To iterate over elements of a container

d) To execute a block of code based on a condition

Answer: b) To execute a block of code at least once, and then repeatedly if a condition is true


What is the output of the following code snippet?

#include <iostream>
using namespace std;

int main() {
    int x = 5;
    int* ptr = &x;
    cout << ptr << " " << *ptr;
    return 0;
}

a) Memory address of x and its value

b) Memory address of x

c) Value of x

d) Compiler error

Answer: a) Memory address of


Leave a Reply