an HCL GUVI product

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!

Q61

Q61 Given the C++ code:
int *ptr = new int(5);
*ptr = 10;
delete ptr;,
what does this code do?

A

Allocates memory for an integer, sets it to 10, then releases the memory

B

Causes a memory leak

C

Generates a runtime error

D

Allocates memory for an integer without releasing it

Q62

Q62 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

Q63

Q63 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

Q64

Q64 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

Q65

Q65 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

Q66

Q66 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

Q67

Q67 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

Q68

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

A

try block

B

catch block

C

throw block

D

error block

Q69

Q69 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

Q70

Q70 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

Q71

Q71 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

Q72

Q72 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

Q73

Q73 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

Q74

Q74 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

Q75

Q75 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

Q76

Q76 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

Q77

Q77 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

Q78

Q78 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

Q79

Q79 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

Q80

Q80 What is an object in object-oriented programming?

A

A code snippet

B

A variable

C

An instance of a class

D

A function

Q81

Q81 Which of the following is a feature of procedure-oriented programming?

A

Class-based

B

Focus on functions

C

Inheritance

D

Polymorphism

Q82

Q82 Which principle of OOP is illustrated by defining different methods with the same name but different parameters?

A

Inheritance

B

Polymorphism

C

Encapsulation

D

Abstraction

Q83

Q83 What distinguishes a class from an object in OOP?

A

A class is a blueprint, while an object is an instance of a class

B

A class cannot contain methods, while an object can

C

Objects are templates, while classes are real entities

D

There is no difference

Q84

Q84 Consider the following code snippet:
class Animal { public: void eat() { cout << "Eating..."; } };
What is demonstrated in this code?

A

Inheritance

B

Encapsulation

C

Class definition

D

Polymorphism

Q85

Q85 Identify the issue in this C++ code snippet:
class Car {
public:
int speed;
void Car() { speed = 0; }
};

A

Incorrect class constructor syntax

B

Missing data encapsulation

C

No error

D

Inheritance misuse

Q86

Q86 Which of these is not a fundamental data type in C++?

A

int

B

float

C

string

D

char

Q87

Q87 What is the output of the expression 5 + 3 * 2 in C++?

A

16

B

11

C

10

D

8

Q88

Q88 What does the ++ operator do in C++?

A

Divides the value by two

B

Decreases the value by one

C

Increases the value by one

D

Squares the value

Q89

Q89 What is the result of the expression !true && false in C++?

A

true

B

false

C

Syntax error

D

None of the above

Q90

Q90 What is the result of int x = 10; x *= 3; in C++?

A

30

B

13

C

33

D

10

ad vertical