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!

Q121

Q121 Identify the error in this code:
int x = 5;
int *ptr;
*ptr = &x;
cout << *ptr;

A

Incorrect assignment to pointer

B

Syntax error

C

No error

D

Dereferencing an uninitialized pointer

Q122

Q122 What is a class in C++?

A

A blueprint for creating objects

B

A function in OOP

C

A variable type

D

An operator

Q123

Q123 What is the purpose of a constructor in a C++ class?

A

To initialize the class's member variables

B

To delete objects of the class

C

To allocate memory for objects

D

To declare a new class

Q124

Q124 How is abstraction different from encapsulation?

A

Abstraction hides complexity by showing only essential features, while encapsulation hides the internal state of an object

B

Abstraction and encapsulation are the same

C

Abstraction is used only in inheritance, while encapsulation is not

D

Encapsulation cannot be used in conjunction with polymorphism

Q125

Q125 Given the following C++ code, what is the output?
class MyClass { public: MyClass() {
cout << "Hello"; }
};
MyClass obj;

A

Hello

B

MyClass

C

Error

D

Nothing

Q126

Q126 Consider the code:
class Shape {
public:
virtual void draw() = 0;
}; class Circle : public Shape {
public: void draw() {
cout << "Circle"; }
};
What concept does this illustrate?

A

Encapsulation

B

Inheritance

C

Polymorphism

D

Abstraction

Q127

Q127 Pseudocode:

CLASS Rectangle PRIVATE width, height PUBLIC FUNCTION setDimensions(w, h) SET width TO w SET height TO h END What OOP concept is demonstrated here?

A

Encapsulation

B

Inheritance

C

Polymorphism

D

Abstraction

Q128

Q128 Pseudocode:

CLASS Shape ABSTRACT FUNCTION draw() END CLASS Circle EXTENDS Shape FUNCTION draw() PRINT "Circle" What does this illustrate?

A

Encapsulation

B

Abstraction

C

Inheritance

D

Polymorphism

Q129

Q129 Identify the mistake:
class Base {
public:
virtual void func() {} };
class Derived : public Base {
void func();
};

A

Missing function body in Derived

B

Incorrect use of virtual function

C

No error

D

Syntax error in class declaration

Q130

Q130 What is the primary purpose of the fstream library in C++?

A

To perform mathematical operations

B

To manage screen output

C

To handle file input and output

D

To process user input

Q131

Q131 Given the C++ code
ofstream file("example.txt");
file << "Hello, World!";
file.close();,
what does this code do?

A

Reads "Hello, World!" from example.txt

B

Writes "Hello, World!" to example.txt

C

Deletes "Hello, World!" from example.txt

D

Opens example.txt without modifying it

Q132

Q132 Pseudocode:

OPEN file.txt AS file WRITE "Data" TO file CLOSE file What is the primary action performed by this code?

A

Reading data from file.txt

B

Writing data to file.txt

C

Deleting data from file.txt

D

Creating a new file named file.txt

Q133

Q133 Spot the error:
ofstream file("example.txt");
file >> "Text";
file.close();

A

Wrong operator used for writing to file

B

File is not opened correctly

C

File is not closed properly

D

No error

Q134

Q134 What does the delete operator do in C++?

A

Deletes a variable from the program

B

Frees memory allocated by new

C

Removes a file from the disk

D

Erases a class definition

Q135

Q135 Which of the following statements about dynamic memory allocation is true?

A

Memory allocated by new is automatically deallocated

B

new and delete can only be used with primitive data types

C

new returns null if memory allocation fails

D

Dynamic memory allocation is less efficient than static allocation

Q136

Q136 What is memory leak in the context of dynamic memory allocation?

A

An error that occurs when memory allocation fails

B

The process of deleting unused memory

C

Memory that is not properly released after use

D

A special technique to clean memory

Q137

Q137 How can you prevent a memory leak in a C++ program?

A

By using automatic variables instead of dynamic variables

B

By ensuring every new operator is matched with delete

C

By avoiding the use of pointers

D

By only using static memory allocation

Q138

Q138 Identify the issue in this code:
int *arr = new int[5];
delete arr;

A

Incorrect syntax for deleting an array

B

No error

C

Memory leak

D

Access violation

Q139

Q139 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

Q140

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

A

std::map

B

std::vector

C

std::list

D

std::set

Q141

Q141 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

Q142

Q142 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

Q143

Q143 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

Q144

Q144 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

Q145

Q145 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

Q146

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

A

throw

B

error

C

exception

D

exit

Q147

Q147 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

Q148

Q148 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

Q149

Q149 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

Q150

Q150 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

ad vertical