set-3

101. Why do we use a destructor in C++?

  1. To destroy an object when the lifetime of the object ends

  2. To create an object when the object needs to be created

  3. To initialize the data members of an object when it is created

  4. 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:

  1. /

  2. |

  3. ?

  4. ~

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?

  1. A constructor has the same name as that of the class

  2. A destructor has the same name as that of the class with a tilde symbol at the beginning.

  3. A constructor is a member function of the class

  4. 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?

  1. Constructor

  2. Destructor

  3. Main

  4. 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?

  1. Destructor

  2. Constructor

  3. Data Members

  4. 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:

  1. Default

  2. Copy

  3. Parameterized

  4. 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. 1

  2. 2

  3. 3

  4. 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?

  1. 0

  2. 1

  3. 2

  4. 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?

  1. Only once

  2. 3

  3. 2

  4. 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:

  1. Heap

  2. Stack

  3. Free Memory

  4. 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()?

  1. Char*

  2. Int *

  3. Void *

  4. 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?

  1. Malloc

  2. Calloc

  3. Free

  4. Both A & B

Show me the answer

Answer: 4) Both A & B

Explanation:

  • Both malloc and calloc 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++?

  1. New

  2. Delete

  3. Free

  4. Remove

Show me the answer

Answer: 2) Delete

Explanation:

  • The delete operator is used to release memory allocated using new 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++?

  1. Free

  2. Delete

  3. Release

  4. 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 using new.

  • The free function is used in C, not C++.

  • Therefore, the correct answer is Delete.

115. Which of the following is correct?

  1. The 'this' pointer is accessible within all the member functions of the class

  2. The 'this' pointer is accessible only within functions returning void

  3. The 'this' pointer is accessible only within non-static functions

  4. 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:

  1. Guard against any kind of reference

  2. Guard against self-reference

  3. Guard from other pointers

  4. 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:

  1. Modifiable

  2. Non-Modifiable

  3. Are made variables

  4. 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?

  1. Const

  2. Volatile

  3. Both A & B

  4. 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.

  • Therefore, the correct answer is Both A & B.

119. The static member functions have access to:

  1. All the members of a class

  2. Only constant members of a class

  3. Only the static members of a class

  4. 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:

  1. Created and initialized only once

  2. Created and initialized twice

  3. Created when necessary

  4. 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?

  1. The static data member can’t be mutable

  2. If static data members are made inline, those can be initialized within the class

  3. We can use the static member functions and static data members even if the class object is not created

  4. 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.

  • Therefore, the correct answer is All of the Above.

122. The keyword 'friend' is placed only in the:

  1. Function Declaration

  2. Function Definition

  3. Main Function

  4. 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?

  1. Friend functions can be invoked as a normal function

  2. Friend functions cannot access the members of the class directly

  3. Friend functions can be private or public

  4. 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:

  1. Friend class;

  2. Friend class

  3. Friend class1, class2

  4. 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:

  1. A single operand

  2. Two operands

  3. Three operands

  4. 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?

  1. &

  2. ==

  3. /

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?

  1. &

  2. ==

  3. /

  4. 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?

  1. ?:

  2. &&

  3. ||

  4. ===

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:

  1. Cast notation

  2. Type casting

  3. Function notation

  4. 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++:

  1. 2

  2. 3

  3. 4

  4. 5

Show me the answer

Answer: 2) 3

Explanation:

  • The three major ways to perform explicit conversion in C++ are:

    1. C-style casting: (type) expression

    2. Static casting: static_cast<type>(expression)

    3. Reinterpret casting: reinterpret_cast<type>(expression)

  • Therefore, the correct answer is 3.

131. How many parameters does a conversion operator may take?

  1. 0

  2. 1

  3. 2

  4. 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?

  1. Unary

  2. Binary

  3. Ternary

  4. 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?

  1. Implicit

  2. Explicit

  3. Ex-Implicit

  4. 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?

  1. Implicit

  2. Explicit

  3. Ex-Implicit

  4. 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++?

  1. 3

  2. 4

  3. 5

  4. 6

Show me the answer

Answer: 3) 5

Explanation:

  • The five types of inheritance in C++ are:

    1. Single inheritance

    2. Multiple inheritance

    3. Multilevel inheritance

    4. Hierarchical inheritance

    5. Hybrid inheritance

  • Therefore, the correct answer is 5.

136. Single-level inheritance is:

  1. A class inheriting a derived class

  2. A class inheriting a base class

  3. A class inheriting a nested class

  4. 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:

  1. JAVA

  2. C++

  3. C

  4. 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:

  1. Interfaces Concept

  2. Diamond Problem

  3. Reusability concept

  4. 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:

  1. Single

  2. Multiple

  3. Multi-Level

  4. 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?

  1. Base Class Constructor

  2. Copy Constructor

  3. Default

  4. 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?

  1. 2

  2. 3

  3. 4

  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?

  1. At least 2

  2. 3

  3. 4

  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?

  1. Base class constructor

  2. Derived class constructor

  3. Both class constructors at a time

  4. 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:

  1. A class derived from another derived class

  2. Classes being derived from other derived classes

  3. Continuing single-level inheritance

  4. 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. 1

  2. 2

  3. 3

  4. 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. 1 class

  2. 2 classes

  3. 3 classes

  4. 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?

  1. Code readability

  2. Flexibility

  3. Code reusability

  4. 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:

  1. A class is derived from another class

  2. A class is derived from two or more classes

  3. When a class is derived from other two derived classes

  4. 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. 1

  2. 2

  3. 3

  4. 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.

Last updated