MCQ of CPP
MCQ of CPP
A) int
B) float
C) string
D) char
Answer: C) string
6. 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.
10. 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)
11. Wrapping data and its related functionality into a single entity is known as :-
a) Abstraction
b) Encapsulation
c) Polymorphism
d) Modularity
Answer: b) Encapsulation
12. Which of the following explains Polymorphism?
14. 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
15. 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
a) Friend constructor
b) Copy constructor
c) Default constructor
d) Parameterized constructor
17. Which of the following cannot be used with the virtual keyword?
a) Class
b) Member functions
c) Constructors
d) Destructors
Answer: c) Constructors
18. 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.
Answer: B) ‘try’ is used to begin exception handling, ‘catch’ is used to handle exceptions, and
‘throw’ is used to throw exceptions.
i ) Only a
ii ) a, b and c
iii ) a and c
iv ) a and b
Answer: ii ) a, b and c
int fun(int n)
{
int count = 0;
for (int i = n; i > 0; i /= 2)
for (int j = 0; j < i; j++)
count += 1;
return count;
}
a) O(n2 )
b) O(n*log(n))
c) O(n )
d) O(n*log(n*log(n)))
Answer: b) O(n*log(n))
22. O( n2 ) is the worst case time complexity, so among the given options it can represent :-
a) O( n )
b) O( 1 )
c) O ( nlogn )
d) All of the above
e) Answer: d) All of the above
23. What is the time complexity of the below function?
void fun(int n, int arr[])
{
int i = 0, j = 0;
for (; i < n; ++i)
while (j < n && arr[i] < arr[j])
j++;
}
a) O(n)
b) O(n2)
c) O(n*log(n))
d) O(n*log(n)2)
Answer: (a) O(n)
a. O(1)
b. O(n)
c. O(logn)
d. O(nlogn)
Answer: a ) O(1)
b) As Flow charts
c) As Syntax
d) As Pseudo Codes
Answer: c ) As Syntax
a) Best-case complexity
b) Average-case complexity
c) Worst-case complexity
29. Which notation is commonly used to express the worst-case time complexity of an
algorithm? a) O(n)
b) Ω(n)
c) Θ(n)
d) O(log n)
Answer: a) O(n)
32. Which container class in the STL allows constant time insertion and deletion operations at
both the beginning and end of the container?
a) Stack
b) Queue
c) Deque
d) Priority queue
Answer: c ) Deque
33. Which STL container class is implemented as a dynamic array and provides random access
iterators?
a) Vector
b) List
c) Set
d) Map
Answer: a ) Vector
34. What type of iterator does a list in the STL typically use?
a) Random access iterator
b) Bidirectional iterator
c) Forward iterator
d) Input iterator
Answer: b) Bidirectional iterator
35. Which operation is commonly associated with a priority queue in the STL?
a) FIFO (First-In-First-Out)
b) LIFO (Last-In-First-Out)
c) Sorting elements based on a comparison function
d) Retrieving the element with the highest priority
Answer: d) Retrieving the element with the highest priority
36. Which STL container is typically implemented as a FIFO (First-In-First-Out) data
structure?
a) Stack
b) Queue
c) Deque
d) Priority queue
Answer: b ) Queue
39. Which STL container class automatically sorts its elements based on a specified comparison
function?
a) Vector
b) List
c) Set
d) Map
Answer: c) Set
40. Which method is used to retrieve the number of elements in a std::map?
a) count( )
b) size( )
c) length( )
d) elements( )
Answer: b ) size ( )
Answer: c ) 16
44. What happens when the backtracking algorithm reaches a complete solution?
a) It backtracks to the root
b) It continues searching for other possible solutions
c) It traverses from a different route
d) Recursively traverses through the same route
Answer: b) It continues searching for other possible solutions