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!

Q91

Q91 In C++, what is polymorphism?

A

The ability of different classes to have methods with the same name

B

The capability of a class to have multiple constructors

C

The ability of a function to perform different tasks based on the context

D

The concept of creating many objects from a single class

Q92

Q92 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

Q93

Q93 What is operator overloading in C++?

A

Giving additional meaning to C++ operators

B

Creating new operators in C++

C

Using existing operators in different contexts

D

Defining operators only in classes

Q94

Q94 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

Q95

Q95 What is the output of this C++ code?
class Test {
public:
int x;
}; Test obj;
obj.x = 5;
cout << obj.x;

A

5

B

0

C

Error

D

Undefined

Q96

Q96 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

Q97

Q97 In the code class Base {
};
class Derived : public Base {
};,
which OOP concept is illustrated?

A

Encapsulation

B

Polymorphism

C

Inheritance

D

Abstraction

Q98

Q98 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

Q99

Q99 Pseudocode:

CLASS Animal FUNCTION makeSound() PRINT "Sound" What is this an example of?

A

A function in C++

B

A class in C++

C

Polymorphism in C++

D

Encapsulation in C++

Q100

Q100 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

Q101

Q101 Pseudocode: CLASS Bird EXTENDS Animal FUNCTION makeSound() PRINT "Chirp" What concept is shown here?

A

Inheritance

B

Polymorphism

C

Encapsulation

D

Abstraction

Q102

Q102 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

Q103

Q103 Find the error:
class MyClass { public: MyClass(int x) {
cout << x; }
};
MyClass obj;

A

Missing default constructor in class definition

B

Syntax error

C

No error

D

Incorrect constructor usage

Q104

Q104 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

Q105

Q105 Spot the error:
class Test {
public:
int x;
void Test() {
x = 0; }
};

A

Incorrect constructor syntax

B

No error

C

Syntax error in variable declaration

D

Member initialization error

Q106

Q106 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

Q107

Q107 In C++, which function is used to open a file?

A

file.open()

B

file.read()

C

file.write()

D

file.close()

Q108

Q108 What is a file pointer in C++?

A

A pointer to the contents of a file

B

A variable that stores the memory address of a file

C

A marker that keeps track of the file's current read/write position

D

A pointer to the file's metadata

Q109

Q109 How do you move the file pointer to a specific location for reading or writing in a file in C++?

A

Using the move() function

B

Using the relocate() function

C

Using the seekg() and seekp() functions

D

Using the transfer() function

Q110

Q110 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

Q111

Q111 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

Q112

Q112 Identify the issue in this C++ code:
ifstream file;
file.open("data.txt");

A

Incorrect file mode

B

File not closed

C

Missing file path

D

No error

Q113

Q113 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

Q114

Q114 What is the purpose of the new operator in C++?

A

To create a new class

B

To allocate memory on the stack

C

To allocate memory on the heap

D

To define a new data type

Q115

Q115 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

Q116

Q116 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

Q117

Q117 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

Q118

Q118 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

Q119

Q119 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

Q120

Q120 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

ad verticalad vertical
ad