3.4 Features of object-oriented programming

OPERATOR OVERLOADING(UNARY AND BINARY)

 

  1. What is operator overloading in C++?

a) A technique to load operators with multiple functions

b) A feature that allows operators to be redefined for user-defined types

c) An approach to increase the efficiency of operators in C++

d) A mechanism to restrict the use of operators in C++
 

Answer: b) A feature that allows operators to be redefined for user-defined types
 

Explanation: Operator overloading in C++ enables you to redefine the behavior of operators for user-defined data types, allowing them to work with the same syntax as 

built-in types.

 

  1. Which of the following is true about unary operator overloading in C++?

a) Unary operators cannot be overloaded in C++

b) Unary operator overloading requires two operands

c) Unary operator overloading requires only one operand

d) Unary operator overloading is automatically handled by the compiler
 

Answer: c) Unary operator overloading requires only one operand
 

Explanation: Unary operator overloading involves defining the behavior of unary operators such as ++, --, +, and - for user-defined types, and it requires only one operand.

 

  1. In C++, which symbol is used for defining a binary operator function?

a) $

b) *

c) &

d) +
 

Answer: d) +
 

Explanation: In C++, the '+' symbol is used to define binary operator functions for overloading binary operators such as addition, subtraction, multiplication, etc.

 

  1. What is the purpose of operator overloading in C++?

a) To create new operators

b) To improve the performance of existing operators

c) To redefine the behavior of existing operators for user-defined types

d) To limit the usage of certain operators
 

Answer: c) To redefine the behavior of existing operators for user-defined types
 

Explanation: Operator overloading allows you to redefine how operators behave when used with user-defined types, providing flexibility and customization.

 

  1. Which keyword is used to overload unary operators in C++?

a) unary

b) operator

c) overload

d) unary_operator
 

Answer: b) operator
 

Explanation: The 'operator' keyword is used to overload operators in C++, including unary operators.

 

  1. What is the syntax for overloading the binary operator '+' for a user-defined class in C++?

a) void operator+(Class obj)

b) Class operator+(Class obj)

c) Class operator+(Class obj1, Class obj2)

d) operator+(Class obj)
 

Answer: c) Class operator+(Class obj1, Class obj2)
 

Explanation: The syntax for overloading the binary operator '+' for a user-defined class in C++ involves using the 'operator' keyword followed by the operator symbol, and it takes two arguments of the user-defined class type.

 

  1. Which of the following operators cannot be overloaded in C++?

a) +

b) .

c) []

d) ?
 

Answer: b) .
 

Explanation: The '.' operator (dot operator) cannot be overloaded in C++. It is used for member selection and pointer dereferencing, and its behavior cannot be altered.

 

  1. What happens if the binary operator '+' is overloaded as a member function in C++?

a) It becomes a friend function

b) It becomes a static function

c) It requires two operands

d) It requires only one operand
 

Answer: c) It requires two operands
 

Explanation: When the binary operator '+' is overloaded as a member function in C++, it requires an additional operand compared to when it's overloaded as a global function.

 

  1. Which of the following statements about operator overloading in C++ is false?

a) Operators cannot be overloaded to work with built-in types

b) Overloaded operators follow the same precedence and associativity as built-in operators

c) Overloaded operators can be declared as friend functions

d) Overloaded operators can have default arguments
 

Answer: a) Operators cannot be overloaded to work with built-in types
 

Explanation: Operators can indeed be overloaded to work with user-defined types in C++. However, they cannot be overloaded to work with built-in types.

 

  1. What is the significance of providing a return type for an overloaded operator function in C++?

a) It improves performance

b) It allows the function to return multiple values

c) It is required by the compiler

d) It enables chaining of operators
 

Answer: d) It enables chaining of operators
 

Explanation: Providing a return type for an overloaded operator function allows for chaining of operators, where one operation can be immediately followed by another, mimicking the behavior of built-in operators.

 

DATA CONVERSION

 

  1. Which of the following statements about data conversion in C++ is true?

a) Data conversion is always implicit and does not require explicit casting

b) Data conversion can only be performed between variables of the same data type

c) Data conversion may result in loss of information or precision

d) Data conversion is not supported in C++
 

Answer: c) Data conversion may result in loss of information or precision
 

Explanation: When converting data from one type to another, there may be a loss of information or precision, especially when converting from a higher precision type to a lower precision type.

 

  1. What is the purpose of static_cast in C++?

a) To perform dynamic type casting

b) To cast between unrelated class types

c) To perform safe and explicit type conversion

d) To automatically convert between data types
 

Answer: c) To perform safe and explicit type conversion
 

Explanation: static_cast is used for safe and explicit type conversion in C++, converting between related types in a type-safe manner.

 

  1. Which type of data conversion in C++ requires an explicit cast?

a) Implicit conversion

b) Dynamic conversion

c) Explicit conversion

d) Automatic conversion
 

Answer: c) Explicit conversion
 

Explanation: Explicit conversion requires an explicit cast operator to convert data from 

one type to another, ensuring clarity and intention in the code.

 

  1. What is the result of converting an integer to a floating-point number in C++?

a) Truncation of decimal part

b) Rounding to the nearest integer

c) Conversion to the nearest even integer

d) Addition of decimal part with zero
 

Answer: a) Truncation of decimal part
 

Explanation: Converting an integer to a floating-point number in C++ involves truncating the decimal part, meaning the fractional part is discarded.

 

  1. Which C++ keyword is used for performing type conversion during inheritance?

a) dynamic_cast

b) static_cast

c) reinterpret_cast

d) const_cast
 

Answer: a) dynamic_cast
 

Explanation: dynamic_cast is used for performing type conversion during inheritance in C++, ensuring type safety and allowing for downcasting within inheritance hierarchies.

 

  1. What is the primary purpose of the const_cast operator in C++?

a) To cast away the const qualifier from a variable

b) To perform dynamic type casting

c) To cast between unrelated class types

d) To convert between arithmetic data types
 

Answer: a) To cast away the const qualifier from a variable
 

Explanation: const_cast is primarily used to remove the const qualifier from a variable, allowing it to be modified despite its original const qualification.

 

  1. When does an implicit conversion occur in C++?

a) When there is loss of information or precision

b) When converting between unrelated class types

c) When no explicit cast operator is required

d) When casting away the const qualifier from a variable
 

Answer: c) When no explicit cast operator is required
 

Explanation: Implicit conversion in C++ occurs when the compiler automatically converts one data type to another without the need for an explicit cast operator, such as converting an integer to a floating-point number.

 

  1. Which type of conversion involves the loss of precision in C++?

a) Implicit conversion

b) Explicit conversion

c) String-to-number conversion

d) Floating-point to integer conversion
 

Answer: d) Floating-point to integer conversion
 

Explanation: Floating-point to integer conversion in C++ can result in the loss of precision, as the fractional part of the floating-point number is truncated.

 

  1. What is the purpose of the std::to_string() function in C++?

a) To convert a string to uppercase

b) To convert an integer to a string

c) To concatenate two strings

d) To split a string into substrings
 

Answer: b) To convert an integer to a string
 

Explanation: The std::to_string() function in C++ converts an integer value to its corresponding string representation.

 

  1. Which C++ function is used for converting a string to a floating-point number?

a) std::stoi()

b) std::stof()

c) std::to_string()

d) std::istringstream()
 

Answer: b) std::stof()
 

Explanation: The std::stof() function in C++ is used to convert a string containing numerical characters into a floating-point number.

 

INHERITANCE (single, multiple, multilevel, hybrid, multipath)

 

  1. What is inheritance in C++?

a) A mechanism for code reuse and creating new classes from existing ones

b) A technique for encrypting sensitive data within classes

c) A method for dynamic memory allocation in classes

d) A process for defining private member functions within a class
 

Answer: a) A mechanism for code reuse and creating new classes from existing ones
 

Explanation: Inheritance in C++ allows a class (subclass or derived class) to inherit properties and behaviors from another class (superclass or base class), facilitating code reuse and the creation of class hierarchies.

 

  1. Which type of inheritance allows a derived class to inherit from multiple base classes?

a) Single inheritance

b) Multiple inheritance

c) Multilevel inheritance

d) Hybrid inheritance
 

Answer: b) Multiple inheritance
 

Explanation: Multiple inheritance in C++ enables a derived class to inherit from more than one base class, allowing it to access the properties and behaviors of all the base 

classes.

 

  1. In C++, what is the access specifier used to specify private inheritance?

a) private

b) protected

c) public

d) internal
 

Answer: a) private
 

Explanation: Private inheritance in C++ restricts access to the base class members to the derived class only, and it is specified using the 'private' access specifier.

 

  1. Which type of inheritance involves creating a new class from an existing derived class?

a) Single inheritance

b) Multiple inheritance

c) Multilevel inheritance

d) Multipath inheritance
 

Answer: c) Multilevel inheritance
 

Explanation: Multilevel inheritance in C++ involves creating a chain of derived classes where each subsequent class is derived from the previous one, forming a hierarchical structure.

 

  1. What is the diamond problem in C++?

a) A problem related to compiling source code with multiple errors

b) A problem arising from multiple inheritance when two base classes have a common ancestor

c) A problem encountered during dynamic memory allocation

d) A problem associated with recursive function calls
 

Answer: b) A problem arising from multiple inheritance when two base classes have a 

common ancestor
 

Explanation: The diamond problem occurs in C++ when a class inherits from two classes that have a common ancestor. It can lead to ambiguity in the inheritance hierarchy and result in compilation errors.

 

  1. Which type of inheritance involves combining multiple types of inheritance, such as single and multiple inheritance?

a) Single inheritance

b) Multiple inheritance

c) Multilevel inheritance

d) Hybrid inheritance
 

Answer: d) Hybrid inheritance
 

Explanation: Hybrid inheritance in C++ is a combination of different types of inheritance, such as single, multiple, multilevel, or hierarchical inheritance, within a single class hierarchy.

 

  1. In C++, which keyword is used to derive a class publicly?

a) private

b) protected

c) public

d) derived
 

Answer: c) public
 

Explanation: In C++, the 'public' keyword is used to specify that the derived class inherits publicly from the base class, allowing access to public members of the base class.

 

  1. What is the primary advantage of using inheritance in C++?

a) Increased encapsulation

b) Improved memory management

c) Code reuse and extensibility

d) Enhanced runtime performance
 

Answer: c) Code reuse and extensibility
 

Explanation: The primary advantage of using inheritance in C++ is code reuse and extensibility, as it allows for the reuse of existing code and the creation of new classes with additional functionality.

 

  1. Which type of inheritance involves a chain of derived classes, with each derived class inheriting from the previous one?

a) Single inheritance

b) Multiple inheritance

c) Multilevel inheritance

d) Hybrid inheritance
 

Answer: c) Multilevel inheritance
 

Explanation: Multilevel inheritance in C++ involves a chain of derived classes, where each subsequent class inherits from the previous one, forming a multilevel hierarchy.

  1. In C++, what is the syntax for invoking a base class constructor from a derived class constructor?

a) super()

b) base()

c) parent()

d) DerivedClassName()
 

Answer: b) base()
 

Explanation: In C++, the syntax for invoking a base class constructor from a derived class constructor is to use the base class name followed by parentheses, e.g., base(). This is often used to initialize the base class part of the derived class object.

 

CONSTRUCTOR / DESTRUCTOR IN SINGLE/MULTILEVEL INHERITANCE

 

  1. In C++, which constructor is invoked first in a multilevel inheritance hierarchy?

a) Base class constructor

b) Derived class constructor

c) Grandchild class constructor

d) Parent class constructor

 

Answer: a) Base class constructor

 

Explanation: In multilevel inheritance, the constructor of the base class is invoked first, followed by the constructors of the derived classes, forming a chain of constructor calls.

 

  1. What happens if a derived class does not explicitly call the constructor of the base class in C++?

a) The compiler automatically inserts a call to the base class constructor

b) It results in a compilation error

c) The base class constructor is called implicitly

d) It leads to undefined behavior

 

Answer: b) It results in a compilation error

 

Explanation: In C++, if a derived class does not explicitly call the constructor of the base class, it results in a compilation error as the compiler expects a call to the base class constructor.

 

  1. Which constructor is automatically called when an object of a derived class is created in C++?

a) Derived class constructor

b) Base class constructor

c) Destructor

d) Assignment operator

 

Answer: a) Derived class constructor

 

Explanation: When an object of a derived class is created in C++, the constructor of the derived class is automatically called to initialize the derived class part of the object.

 

  1. In C++, how are constructors and destructors inherited in single inheritance?

a) Constructors are inherited, but destructors are not

b) Destructors are inherited, but constructors are not

c) Both constructors and destructors are inherited

d) Neither constructors nor destructors are inherited

 

Answer: c) Both constructors and destructors are inherited

 

Explanation: In single inheritance in C++, both constructors and destructors are inherited from the base class to the derived class, following the inheritance hierarchy.

 

  1. What is the purpose of the constructor in C++?

a) To allocate memory for class objects

b) To initialize the data members of a class object

c) To destroy the class object

d) To perform dynamic type casting

 

Answer: b) To initialize the data members of a class object

 

Explanation: Constructors in C++ are special member functions used to initialize the data members of a class object when it is created.

 

  1. How are constructors and destructors called in a multilevel inheritance hierarchy in C++?

a) Constructors are called from the base class to the derived class, and destructors are called in the reverse order

b) Constructors are called from the derived class to the base class, and destructors are called in the same order

c) Constructors and destructors are called randomly

d) Constructors are called in the derived class only, and destructors are called in the base class only

 

Answer: a) Constructors are called from the base class to the derived class, and destructors are called in the reverse order

 

Explanation: In a multilevel inheritance hierarchy in C++, constructors are called from the base class to the derived class, and destructors are called in the reverse order, from the derived class to the base class.

 

  1. In C++, which type of constructor is called implicitly by the compiler when no constructor is defined in a class?

a) Default constructor

b) Parameterized constructor

c) Copy constructor

d) Destructor

 

Answer: a) Default constructor

 

Explanation: In C++, if no constructor is defined in a class, the compiler provides a default constructor, which initializes the object with default values for its data members.

 

  1. What is the purpose of a destructor in C++?

a) To initialize the object of a class

b) To allocate memory for the object of a class

c) To free the resources acquired by the object of a class

d) To perform arithmetic operations on the object of a class

 

Answer: c) To free the resources acquired by the object of a class

 

Explanation: Destructors in C++ are special member functions used to release resources (such as memory, file handles, etc.) acquired by the object of a class before it is destroyed.

 

  1. In single inheritance, when is the base class constructor called?

a) After the derived class constructor

b) Before the derived class constructor

c) Simultaneously with the derived class constructor

d) Base class constructor is not called in single inheritance

 

Answer: b) Before the derived class constructor

 

Explanation: In single inheritance in C++, the base class constructor is called before the derived class constructor, ensuring that the base class part of the object is initialized before the derived class part.

 

  1. What is the order of constructor and destructor calls in single inheritance in C++?

a) Constructor of base class, constructor of derived class, destructor of derived class, destructor of base class

b) Constructor of derived class, constructor of base class, destructor of base class, destructor of derived class

c) Constructor of base class, destructor of base class, constructor of derived class, destructor of derived class

d) Constructor of derived class, destructor of derived class, constructor of base class, destructor of base class

 

Answer: c) Constructor of base class, destructor of base class, constructor of derived class, destructor of derived class

 

Explanation: In single inheritance in C++, the constructor of the base class is called first, followed by the constructor of the derived class. Similarly, the destructor of the derived class is called first, followed by the destructor of the base class.