set-3
101. Why do we use a destructor in C++?
- To destroy an object when the lifetime of the object ends
- To create an object when the object needs to be created
- To initialize the data members of an object when it is created
- None of the Above
Show me the answer
Answer: 1) To destroy an object when the lifetime of the object ends
Explanation:
- A destructor is used to:
- Release resources (e.g., memory, file handles) allocated to an object.
- Perform cleanup tasks when the object goes out of scope or is explicitly deleted.
- Therefore, the correct answer is
To destroy an object when the lifetime of the object ends
.
102. A destructor is preceded by:
- /
- |
- ?
- ~
Show me the answer
Answer: 4) ~
Explanation:
-
A destructor is preceded by the tilde symbol (
~
). -
For example:
class MyClass { public: ~MyClass() { cout << "Destructor called!"; } };
-
Therefore, the correct answer is
~
.
103. Which of the following statements is correct?
- A constructor has the same name as that of the class
- A destructor has the same name as that of the class with a tilde symbol at the beginning.
- A constructor is a member function of the class
- All of the Above
Show me the answer
Answer: 4) All of the Above
Explanation:
- All the statements are correct:
- A constructor has the same name as the class.
- A destructor has the same name as the class but is preceded by a tilde (
~
). - Both constructors and destructors are member functions of the class.
- Therefore, the correct answer is
All of the Above
.
104. When an object goes out of scope, which of the following gets called?
- Constructor
- Destructor
- Main
- Void
Show me the answer
Answer: 2) Destructor
Explanation:
- When an object goes out of scope, its destructor is automatically called to clean up resources.
- Therefore, the correct answer is
Destructor
.
105. Which of the following can be declared as virtual?
- Destructor
- Constructor
- Data Members
- All of the Above
Show me the answer
Answer: 1) Destructor
Explanation:
- A destructor can be declared as virtual to ensure proper cleanup of derived class objects when deleted through a base class pointer.
- Constructors and data members cannot be declared as virtual.
- Therefore, the correct answer is
Destructor
.
106. The constructor that either has no parameters, or if it has parameters, all the parameters have default values:
- Default
- Copy
- Parameterized
- Friend function
Show me the answer
Answer: 1) Default
Explanation:
- A default constructor is either:
- A constructor with no parameters.
- A constructor where all parameters have default values.
- Therefore, the correct answer is
Default
.
107. Per class, how many default constructors are possible?
- 1
- 2
- 3
- 4
Show me the answer
Answer: 1) 1
Explanation:
- Only one default constructor can exist per class.
- If multiple constructors are defined, only one can be the default constructor.
- Therefore, the correct answer is
1
.
108. A destructor takes how many arguments?
- 0
- 1
- 2
- 3
Show me the answer
Answer: 1) 0
Explanation:
- A destructor does not take any arguments. It is called automatically when an object goes out of scope.
- Therefore, the correct answer is
0
.
109. How many times is a constructor called in the lifetime of an object?
- Only once
- 3
- 2
- None of the Above
Show me the answer
Answer: 1) Only once
Explanation:
- A constructor is called only once when an object is created.
- Therefore, the correct answer is
Only once
.
110. The area where the local variables are stored is called:
- Heap
- Stack
- Free Memory
- Cache
Show me the answer
Answer: 2) Stack
Explanation:
- Local variables are stored in the stack, which is a memory area used for function calls and local data.
- Therefore, the correct answer is
Stack
.
111. What is the return type of malloc()?
- Char*
- Int *
- Void *
- Void **
Show me the answer
Answer: 3) Void *
Explanation:
-
The
malloc()
function returns a void pointer (void*
), which can be cast to any data type. -
For example:
int* ptr = (int*)malloc(sizeof(int));
-
Therefore, the correct answer is
Void *
.
112. Which of the following obtains a block of memory dynamically?
- Malloc
- Calloc
- Free
- Both A & B
Show me the answer
Answer: 4) Both A & B
Explanation:
- Both
malloc
andcalloc
are used to allocate memory dynamically:malloc
allocates memory without initializing it.calloc
allocates memory and initializes it to zero.
- Therefore, the correct answer is
Both A & B
.
113. Which of the following operators is used to release the dynamically allocated memory in C++?
- New
- Delete
- Free
- Remove
Show me the answer
Answer: 2) Delete
Explanation:
-
The
delete
operator is used to release memory allocated usingnew
in C++. -
For example:
int* ptr = new int; delete ptr;
-
Therefore, the correct answer is
Delete
.
114. Which of the following is used to free the allocated memory for an object in C++?
- Free
- Delete
- Release
- Either delete or free
Show me the answer
Answer: 2) Delete
Explanation:
- In C++, the
delete
operator is used to free memory allocated for an object usingnew
. - The
free
function is used in C, not C++. - Therefore, the correct answer is
Delete
.
115. Which of the following is correct?
- The ‘this’ pointer is accessible within all the member functions of the class
- The ‘this’ pointer is accessible only within functions returning void
- The ‘this’ pointer is accessible only within non-static functions
- The ‘this’ pointer is accessible within the member functions with zero arguments
Show me the answer
Answer: 3) The ‘this’ pointer is accessible only within non-static functions
Explanation:
- The
this
pointer is a hidden pointer available in all non-static member functions of a class. - It points to the object for which the member function is called.
- Therefore, the correct answer is
The 'this' pointer is accessible only within non-static functions
.
116. The ‘this’ pointer can be used to:
- Guard against any kind of reference
- Guard against self-reference
- Guard from other pointers
- Guard from parameter referencing
Show me the answer
Answer: 2) Guard against self-reference
Explanation:
- The
this
pointer is used to:- Avoid ambiguity between class members and function parameters with the same name.
- Guard against self-reference in assignment operations.
- Therefore, the correct answer is
Guard against self-reference
.
117. The ‘this’ pointer is:
- Modifiable
- Non-Modifiable
- Are made variables
- None of the Above
Show me the answer
Answer: 2) Non-Modifiable
Explanation:
- The
this
pointer is a constant pointer, meaning its value cannot be changed. - Therefore, the correct answer is
Non-Modifiable
.
118. Which of the following are types of ‘this’ pointer?
- Const
- Volatile
- Both A & B
- Int
Show me the answer
Answer: 3) Both A & B
Explanation:
- The
this
pointer can be:- Const: When used in a
const
member function. - Volatile: When used in a
volatile
member function.
- Const: When used in a
- Therefore, the correct answer is
Both A & B
.
119. The static member functions have access to:
- All the members of a class
- Only constant members of a class
- Only the static members of a class
- All other class members also
Show me the answer
Answer: 3) Only the static members of a class
Explanation:
- Static member functions can only access static data members and other static member functions of the class.
- They cannot access non-static members directly.
- Therefore, the correct answer is
Only the static members of a class
.
120. The static members are:
- Created and initialized only once
- Created and initialized twice
- Created when necessary
- All of the Above
Show me the answer
Answer: 1) Created and initialized only once
Explanation:
- Static members are:
- Created and initialized only once when the program starts.
- Shared among all objects of the class.
- Therefore, the correct answer is
Created and initialized only once
.
121. Which of the following is correct?
- The static data member can’t be mutable
- If static data members are made inline, those can be initialized within the class
- We can use the static member functions and static data members even if the class object is not created
- All of the Above
Show me the answer
Answer: 4) All of the Above
Explanation:
- All the statements are correct:
- Static data members cannot be declared as
mutable
. - Static data members can be initialized within the class if they are declared as
inline
. - Static members (functions and data) can be accessed without creating an object of the class.
- Static data members cannot be declared as
- Therefore, the correct answer is
All of the Above
.
122. The keyword ‘friend’ is placed only in the:
- Function Declaration
- Function Definition
- Main Function
- Void Function
Show me the answer
Answer: 1) Function Declaration
Explanation:
- The
friend
keyword is used in the function declaration inside the class to grant access to private and protected members. - It is not used in the function definition or other parts of the program.
- Therefore, the correct answer is
Function Declaration
.
123. Which of the following is correct?
- Friend functions can be invoked as a normal function
- Friend functions cannot access the members of the class directly
- Friend functions can be private or public
- All of the Above
Show me the answer
Answer: 4) All of the Above
Explanation:
- All the statements are correct:
- Friend functions are not member functions and can be invoked like normal functions.
- Friend functions can access private and protected members of the class directly.
- Friend functions can be declared in the private or public section of the class.
- Therefore, the correct answer is
All of the Above
.
124. The syntax of a friend function is:
- Friend class;
- Friend class
- Friend class1, class2
- All of the Above
Show me the answer
Answer: 2) Friend class
Explanation:
-
The correct syntax for declaring a friend function is:
friend returnType functionName(parameters);
-
For example:
class MyClass { private: int data; public: friend void display(MyClass obj); };
-
Therefore, the correct answer is
Friend class
.
125. A binary operator performs its action on:
- A single operand
- Two operands
- Three operands
- Any number of operands
Show me the answer
Answer: 2) Two operands
Explanation:
- A binary operator operates on two operands.
- Examples include
+
,-
,*
,/
,==
, etc. - Therefore, the correct answer is
Two operands
.
126. Which of the following is a unary operator?
- &
- ==
-
- /
Show me the answer
Answer: 3) -
Explanation:
- A unary operator operates on a single operand.
- Examples include
-
(negation),++
,--
,!
, etc. - Therefore, the correct answer is
-
.
127. Which of the following is a binary operator?
- &
- ==
- /
- All of the Above
Show me the answer
Answer: 4) All of the Above
Explanation:
- All the given operators are binary operators:
&
(bitwise AND)==
(equality)/
(division)
- Therefore, the correct answer is
All of the Above
.
128. Which is called the ternary operator?
- ?:
- &&
- ||
- ===
Show me the answer
Answer: 1) ?:
Explanation:
-
The ternary operator is
?:
. -
It takes three operands and is used as a shorthand for
if-else
statements. -
For example:
int result = (a > b) ? a : b;
-
Therefore, the correct answer is
?:
.
129. C-style type casting is also known as:
- Cast notation
- Type casting
- Function notation
- C notation
Show me the answer
Answer: 2) Type casting
Explanation:
-
C-style type casting is also known as type casting.
-
It uses the syntax:
(type) expression;
-
Therefore, the correct answer is
Type casting
.
130. There are three major ways in which we can use explicit conversion in C++:
- 2
- 3
- 4
- 5
Show me the answer
Answer: 2) 3
Explanation:
- The three major ways to perform explicit conversion in C++ are:
- C-style casting:
(type) expression
- Static casting:
static_cast<type>(expression)
- Reinterpret casting:
reinterpret_cast<type>(expression)
- C-style casting:
- Therefore, the correct answer is
3
.
131. How many parameters does a conversion operator may take?
- 0
- 1
- 2
- 3
Show me the answer
Answer: 1) 0
Explanation:
- A conversion operator does not take any parameters.
- It is used to convert an object of one type to another type.
- Therefore, the correct answer is
0
.
132. What type of operator is a cast operator?
- Unary
- Binary
- Ternary
- None of the Above
Show me the answer
Answer: 1) Unary
Explanation:
- A cast operator is a unary operator because it operates on a single operand.
- Therefore, the correct answer is
Unary
.
133. The compiler carries out which type of casting?
- Implicit
- Explicit
- Ex-Implicit
- All of the Above
Show me the answer
Answer: 1) Implicit
Explanation:
-
The compiler performs implicit casting automatically when data types are compatible.
-
For example:
int x = 10; double y = x; // Implicit casting from int to double
-
Therefore, the correct answer is
Implicit
.
134. The programmer initiates which type of casting?
- Implicit
- Explicit
- Ex-Implicit
- All of the Above
Show me the answer
Answer: 2) Explicit
Explanation:
- The programmer initiates explicit casting using casting operators like
static_cast
,dynamic_cast
, etc. - Therefore, the correct answer is
Explicit
.
135. How many types of inheritance are there in C++?
- 3
- 4
- 5
- 6
Show me the answer
Answer: 3) 5
Explanation:
- The five types of inheritance in C++ are:
- Single inheritance
- Multiple inheritance
- Multilevel inheritance
- Hierarchical inheritance
- Hybrid inheritance
- Therefore, the correct answer is
5
.
136. Single-level inheritance is:
- A class inheriting a derived class
- A class inheriting a base class
- A class inheriting a nested class
- A class which gets inherited by 2 classes
Show me the answer
Answer: 2) A class inheriting a base class
Explanation:
-
Single-level inheritance occurs when a derived class inherits from a single base class.
-
For example:
class Base {}; class Derived : public Base {};
-
Therefore, the correct answer is
A class inheriting a base class
.
137. Multiple Inheritance is not supported by:
- JAVA
- C++
- C
- JAVA and Smalltalk
Show me the answer
Answer: 1) JAVA
Explanation:
- Multiple inheritance is not supported by JAVA to avoid complexity and ambiguity (e.g., the diamond problem).
- C++ supports multiple inheritance.
- Therefore, the correct answer is
JAVA
.
138. The feature of multiple inheritance in JAVA is done through:
- Interfaces Concept
- Diamond Problem
- Reusability concept
- None of the Above
Show me the answer
Answer: 1) Interfaces Concept
Explanation:
- In JAVA, the concept of multiple inheritance is achieved using interfaces.
- A class can implement multiple interfaces, thus simulating multiple inheritance.
- Therefore, the correct answer is
Interfaces Concept
.
139. The diamond problem is associated with:
- Single
- Multiple
- Multi-Level
- Hierarchical
Show me the answer
Answer: 2) Multiple
Explanation:
- The diamond problem occurs in multiple inheritance when a class inherits from two classes that both inherit from a common base class.
- This creates ambiguity in accessing the base class members.
- Therefore, the correct answer is
Multiple
.
140. Which constructor is called first if a derived class object is created?
- Base Class Constructor
- Copy Constructor
- Default
- Not possible
Show me the answer
Answer: 1) Base Class Constructor
Explanation:
- When a derived class object is created, the base class constructor is called first to initialize the base class part of the object.
- Then, the derived class constructor is called.
- Therefore, the correct answer is
Base Class Constructor
.
141. In a single program, how many types of inheritance can be used?
- 2
- 3
- 4
- Any type, any number of times
Show me the answer
Answer: 4) Any type, any number of times
Explanation:
- In a single program, any type of inheritance (single, multiple, multilevel, hierarchical, hybrid) can be used any number of times.
- Therefore, the correct answer is
Any type, any number of times
.
142. If we use single inheritance, then the program will contain how many classes?
- At least 2
- 3
- 4
- At most 4
Show me the answer
Answer: 1) At least 2
Explanation:
- In single inheritance, there must be at least two classes: a base class and a derived class.
- Therefore, the correct answer is
At least 2
.
143. Which constructor will be called first from the classes involved in single inheritance from the object of the derived class?
- Base class constructor
- Derived class constructor
- Both class constructors at a time
- Runtime error
Show me the answer
Answer: 1) Base class constructor
Explanation:
- In single inheritance, the base class constructor is called first when a derived class object is created.
- Therefore, the correct answer is
Base class constructor
.
144. Multilevel inheritance is:
- A class derived from another derived class
- Classes being derived from other derived classes
- Continuing single-level inheritance
- A class which has more than one parent
Show me the answer
Answer: 1) A class derived from another derived class
Explanation:
-
Multilevel inheritance occurs when a class is derived from another derived class.
-
For example:
class A {}; class B : public A {}; class C : public B {};
-
Therefore, the correct answer is
A class derived from another derived class
.
145. The minimum number of levels for implementing multilevel inheritance is:
- 1
- 2
- 3
- 4
Show me the answer
Answer: 3) 3
Explanation:
- Multilevel inheritance requires at least three levels: a base class, a derived class, and a further derived class.
- Therefore, the correct answer is
3
.
146. In multilevel inheritance, one class inherits:
- 1 class
- 2 classes
- 3 classes
- 4 classes
Show me the answer
Answer: 1) 1 class
Explanation:
- In multilevel inheritance, each class inherits from only one class.
- Therefore, the correct answer is
1 class
.
147. In multilevel inheritance, which is the most significant feature of OOP used?
- Code readability
- Flexibility
- Code reusability
- All of the Above
Show me the answer
Answer: 3) Code reusability
Explanation:
- The most significant feature of OOP used in multilevel inheritance is code reusability.
- Derived classes reuse the code from their base classes.
- Therefore, the correct answer is
Code reusability
.
148. Multiple inheritance is defined as:
- A class is derived from another class
- A class is derived from two or more classes
- When a class is derived from other two derived classes
- When a class is derived from exactly one class
Show me the answer
Answer: 2) A class is derived from two or more classes
Explanation:
-
Multiple inheritance occurs when a class is derived from two or more base classes.
-
For example:
class A {}; class B {}; class C : public A, public B {};
-
Therefore, the correct answer is
A class is derived from two or more classes
.
149. To implement multiple inheritance, at least how many classes should a program contain?
- 1
- 2
- 3
- 4
Show me the answer
Answer: 3) 3
Explanation:
- To implement multiple inheritance, a program must contain at least three classes: two base classes and one derived class.
- Therefore, the correct answer is
3
.