c++ programming banner

C++ Programming Multiple Choice Questions (MCQs) and Answers

Master C++ Programming with Practice MCQs. Explore our curated collection of Multiple Choice Questions. Ideal for placement and interview preparation, our questions range from basic to advanced, ensuring comprehensive coverage of C++ Programming. Begin your placement preparation journey now!

Q121

Q121 Spot the error:
int *ptr;
*ptr = new int;
*ptr = 5;
delete ptr;

A

Dereferencing an uninitialized pointer

B

Syntax error in memory allocation

C

No error

D

Incorrect use of delete

Q122

Q122 What is a function template in C++?

A

A predefined function in the C++ Standard Library

B

A special function used for debugging

C

A way to create functions with a generic type

D

A container in the STL

Q123

Q123 What is a class template in C++?

A

A class defined inside another class

B

A class that serves as a blueprint for creating other classes

C

A class that can handle multiple data types generically

D

A class used exclusively for STL operations

Q124

Q124 Which STL container is typically used for implementing a dynamic array?

A

std::map

B

std::vector

C

std::list

D

std::set

Q125

Q125 How does the std::map container in C++ STL store elements?

A

In a single list

B

In key-value pairs, with unique keys

C

As an array of elements

D

In a stack-like structure

Q126

Q126 What is the primary difference between std::list and std::vector in C++ STL?

A

std::list is for integer types only, while std::vector can hold any type

B

std::list provides sequential access, while std::vector provides random access

C

std::list is a dynamic array, while std::vector is a linked list

D

std::list is a doubly linked list, while std::vector is a dynamic array

Q127

Q127 In C++ STL, what is an iterator?

A

A loop that iterates over a container

B

A pointer-like object used to access elements in a container

C

A function to traverse arrays

D

A class template for container classes

Q128

Q128 What distinguishes std::set from other STL containers?

A

It allows for rapid insertion of elements

B

It automatically sorts the elements in ascending order

C

It stores each element in duplicate

D

It is the only STL container that uses templates

Q129

Q129 Why are templates used in C++?

A

To create error-free code

B

To provide predefined functionality

C

To allow classes and functions to operate with generic types

D

To optimize code execution

Q130

Q130 Given the C++ code snippet:
template T add(T a, T b) {
return a + b;
}
What is this an example of?

A

A class template

B

A function template

C

An overloaded operator

D

An STL container

Q131

Q131 Identify the issue in this C++ code:
std::vector v;
for (int i = 0; i <= v.size(); ++i) {

cout << v[i]; }

A

Using a wrong loop condition

B

Accessing elements outside the vector's bounds

C

Incorrect use of the vector container

D

No error

Q132

Q132 Spot the error:
template class MyContainer { public:
T value; void setValue(T val) { value = val; }
};

A

Incorrect template syntax

B

Missing constructor

C

No error

D

Error in setValue function

Q133

Q133 What is the purpose of exception handling in C++?

A

To handle syntax errors in the code

B

To increase the execution speed of programs

C

To manage runtime errors in a controlled way

D

To debug the program

Q134

Q134 What keyword is used in C++ to throw an exception?

A

throw

B

error

C

exception

D

exit

Q135

Q135 Which block of code is used to handle exceptions in C++?

A

try block

B

catch block

C

throw block

D

error block

Q136

Q136 What is the correct way to handle multiple exceptions in C++?

A

Using multiple throw statements

B

Using several try blocks

C

Using multiple catch blocks after a single try block

D

Using nested try-catch blocks

Q137

Q137 In C++, what happens if an exception is not caught?

A

The program will continue to execute

B

The exception gets rethrown automatically

C

The program terminates abnormally

D

The exception is ignored

Q138

Q138 What is the use of the std::exception class in C++?

A

To terminate a program

B

To catch syntax errors

C

As a base class for standard exception types

D

To declare new variables

Q139

Q139 What is the output of this code?
try { throw 20; } catch (int e) {
cout << "An exception occurred. Exception Nr. " << e;
}

A

An error message

B

An exception occurred. Exception Nr. 20

C

20

D

The program crashes

Q140

Q140 Given the C++ code snippet:
try { int* myarray = new int[1000]; } catch (std::bad_alloc& e) {
cout << "Allocation failed: " << e.what();
},
what does this code do?

A

It handles an out-of-memory error

B

It creates an array of 1000 integers

C

It throws a bad_alloc exception

D

It catches syntax errors

Q141

Q141 Identify the issue in this code:
try { throw "Error occurred"; } catch (...) {
cout << "An error occurred";
}

A

The catch block does not specify the exception type

B

The throw statement is incorrect

C

There is no error

D

The try block is empty

Q142

Q142 Spot the error:
try { int x = -1;
if (x < 0) { throw x; } } catch (unsigned int e) {
cout << "Caught an exception: " << e; }

A

The throw statement should not throw integers

B

The catch block is catching the wrong type of exception

C

There is no error

D

The if condition is incorrect

Q143

Q143 What is the purpose of namespaces in C++?

A

To optimize code execution speed

B

To handle exceptions

C

To avoid name conflicts in larger programs

D

To manage dynamic memory

Q144

Q144 What are smart pointers in C++?

A

Pointers that automatically manage memory allocation and deallocation

B

Pointers that increase execution speed

C

Arrays with advanced features

D

Iterators in the STL

Q145

Q145 What is a lambda expression in C++?

A

A function that cannot have its address taken

B

A compact way of defining anonymous functions

C

A template for creating multiple functions

D

A method in the STL

Q146

Q146 How is multithreading achieved in C++?

A

Using the std::thread library

B

By creating multiple processes

C

Through lambda expressions

D

By using dynamic memory allocation

Q147

Q147 What are regular expressions used for in C++?

A

To optimize algorithms

B

To create complex data structures

C

To search, edit, or manipulate text

D

To manage file input/output operations

Q148

Q148 In C++, which smart pointer type automatically deallocates the memory when it is no longer in use?

A

auto_ptr

B

shared_ptr

C

unique_ptr

D

weak_ptr

Q149

Q149 What is the significance of std::regex in C++?

A

It is used for creating regular expressions

B

It is a class template for sequence containers

C

It is a function for memory allocation

D

It is a method for string manipulation

Q150

Q150 Given the C++ code snippet:
auto lambda = [](int x, int y) { return x + y; }; cout << lambda(2, 3);,
what is the output?

A

5

B

Compilation error

C

2

D

3

ad verticalad vertical
ad