Unit 4: Operator Overloading, Type Casting and Re-usability - Practice Quiz

CAP455 — Object Oriented Programming Using C++ 60 Questions
0 Correct 0 Wrong 60 Left
0/60

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

Importance of operator overloading Easy
A. To change the precedence of operators
B. To create new operators from keywords
C. To give operators new meaning for user-defined types
D. To remove operators from a class

2 Which operator is an example of a unary operator?

Unary operator overloading Easy
A. - with one operand
B. + with two operands
C. * with two operands
D. / with two operands

3 How many operands does a binary operator normally use?

Binary operator overloading Easy
A. Two operands
B. No operands
C. Three operands
D. One operand

4 Which member is commonly used to convert a basic type into a class type?

Basic type to class type conversion Easy
A. A virtual table
B. A destructor
C. A constructor
D. A friend variable

5 Which feature is commonly used to convert a class object into a basic type?

Class type to basic type conversion Easy
A. A private data member
B. A base class
C. A default destructor
D. A conversion function

6 What is a major benefit of inheritance in C++?

Importance of re-usability through inheritance Easy
A. It supports code reuse
B. It prevents code reuse
C. It removes all constructors
D. It disables member functions

7 What does inheritance allow a class to do?

Basics of inheritance Easy
A. Delete features from every class
B. Acquire features from another class
C. Avoid defining any objects
D. Use only global variables

8 In inheritance, what is the class that provides properties to another class called?

Base class and derived class Easy
A. Base class
B. Nested class
C. Local class
D. Derived class

9 What is simple inheritance?

Simple inheritance Easy
A. Several unrelated classes
B. One base class and one derived class
C. One class with no members
D. Several base classes and no derived class

10 Which structure represents multilevel inheritance?

Multilevel inheritance Easy
A. A inherits from B and C
B. A, B, and C are unrelated
C. A inherits from B, and B inherits from C
D. A and B inherit from C

11 What is multiple inheritance?

Multiple inheritance Easy
A. One class has multiple constructors only
B. One base class has multiple objects
C. One derived class has multiple base classes
D. Multiple classes have no relationship

12 Which arrangement represents hierarchical inheritance?

Hierarchical inheritance Easy
A. Several derived classes inherit from one base class
B. Several classes form unrelated chains
C. Each class inherits from itself
D. One derived class inherits from several base classes

13 What is hybrid inheritance?

Hybrid inheritance Easy
A. Inheritance without a base class
B. A combination of two or more inheritance types
C. Inheritance using only private data
D. Inheritance using only one class

14 In private inheritance, public and protected members of the base class become what in the derived class?

Private mode of inheritance Easy
A. Global members
B. Constant members
C. Public members
D. Private members

15 In protected inheritance, public members of the base class become what in the derived class?

Protected mode of inheritance Easy
A. Protected members
B. Private members
C. Static members
D. Public members

16 In public inheritance, public members of the base class remain what in the derived class?

Public mode of inheritance Easy
A. Protected
B. Hidden
C. Private
D. Public

17 What does function overriding mean in inheritance?

Overriding of member functions Easy
A. A base class removes a function from memory
B. A function is declared without a return type
C. Two variables receive the same value
D. A derived class defines a function with the same name and signature

18 When an object of a derived class is created, which constructor executes first?

Order of execution of constructors and destructor Easy
A. The friend function
B. The derived-class constructor
C. The destructor
D. The base-class constructor

19 How can an ambiguity between two inherited functions with the same name be resolved?

Resolving ambiguities in inheritance Easy
A. Remove all constructors
B. Declare every function global
C. Use the scope resolution operator
D. Use only a destructor

20 Why is a virtual base class used in inheritance?

Virtual base class Easy
A. To replace all constructors
B. To avoid duplicate copies of a common base class
C. To prevent object creation
D. To make every function private

21 Why is operator overloading useful for a class such as Complex in C++?

Importance of operator overloading Medium
A. It removes the need to define constructors in the class
B. It allows objects to use operators with class-defined behavior
C. It automatically converts every object into a basic type
D. It allows new operator symbols to be created at runtime

22 Consider the following class fragment:

Counter operator++() { ++value; return *this; }

If Counter c(4); Counter d = ++c; is executed, what values will c and d contain?

Unary operator overloading Medium
A. c = 4, d = 5
B. c = 5, d = 4
C. c = 5, d = 5
D. c = 4, d = 4

23 A Point class defines friend Point operator+(const Point& a, const Point& b) to add corresponding coordinates. What is the result of adding Point(2, 3) and Point(4, -1)?

Binary operator overloading Medium
A. Point(8, -3)
B. Point(6, 4)
C. Point(-2, 2)
D. Point(6, 2)

24 Given class Distance { double meters; public: Distance(double m) : meters(m) {} };, which statement performs an implicit basic-type-to-class-type conversion?

Basic type to class type conversion Medium
A. int d = static_cast<int>(12.5);
B. Distance d = 12.5;
C. Distance d; d = Distance();
D. double d = Distance(12.5);

25 Which member declaration enables an object of class Score to be converted to int?

Class type to basic type conversion Medium
A. operator int() const;
B. friend int(Score);
C. int Score() const;
D. Score(int value);

26 A program has several employee classes that all require name, id, and displayIdentity(). Which design best promotes re-usability?

Importance of re-usability through inheritance Medium
A. Define the common features in one base class
B. Repeat the common features in every derived class
C. Place all employee data in global variables
D. Implement each employee class without relationships

27 A base class declares protected: int value;. Which use of value is valid?

Basics of inheritance Medium
A. Access inside a derived-class member function
B. Access through any unrelated function
C. Access through any function receiving a base pointer
D. Access directly through a base-class object in main

28 Suppose Derived publicly inherits Base, both define show(), and Base::show() is not virtual. What happens when Base* p = new Derived; p->show(); is executed?

Base class and derived class Medium
A. The statement fails to compile
B. Both versions are called
C. Base::show() is called
D. Derived::show() is called

29 Given class Employee : public Person, where Person has a public method getName(), which statement is correct?

Simple inheritance Medium
A. getName() becomes protected inside Employee
B. An Employee object can call getName() publicly
C. An Employee object cannot contain Person members
D. Person automatically inherits members from Employee

30 Consider class B : public A and class C : public B. If A has a public method test() and neither class hides it, which expression is valid?

Multilevel inheritance Medium
A. A obj; obj.B::test();
B. B obj; obj.C::test();
C. C obj; obj.test();
D. C obj; obj.A::B::test();

31 Given class Device : public Reader, public Writer, where Reader defines read() and Writer defines write(), which statement is valid for Device d?

Multiple inheritance Medium
A. d.Reader(); d.Writer();
B. d.read(); d.write();
C. d.read().write();
D. Reader.d.read(); Writer.d.write();

32 In hierarchical inheritance, Car and Bike both publicly inherit Vehicle. Which conversion is valid without an explicit cast?

Hierarchical inheritance Medium
A. Vehicle* to Car*
B. Bike object to Car object
C. Car* to Bike*
D. Car* to Vehicle*

33 Which class arrangement is an example of hybrid inheritance?

Hybrid inheritance Medium
A. One class derives directly from only one base class
B. Several unrelated classes define identical functions
C. A class combines multilevel and multiple inheritance
D. One class contains an object of another class

34 If Derived privately inherits Base, what happens to the public and protected members of Base inside Derived?

Private mode of inheritance Medium
A. Both are treated as private members
B. Both remain publicly accessible
C. Both become protected members
D. Both become inaccessible to Derived

35 If Derived inherits Base using protected inheritance, how are the public and protected members of Base treated in Derived?

Protected mode of inheritance Medium
A. Both become private
B. Both remain public
C. Both become protected
D. Both become inaccessible

36 Under public inheritance, which access transformation is correct?

Public mode of inheritance Medium
A. Public becomes private and protected becomes private
B. Public stays public and protected stays protected
C. Public becomes protected and protected stays protected
D. Public stays public and protected becomes private

37 Consider virtual void show() const in a base class and void show() in a derived class. If no override specifier is used, what is true?

Overriding of member functions Medium
A. Both functions become non-virtual automatically
B. The derived function overrides the base function normally
C. The base function is removed from the derived class
D. The derived function does not override the base function

38 For class Derived : public Base, a Derived object is created and then destroyed. What is the correct execution order?

Order of execution of constructors and destructor Medium
A. Derived constructor, Base constructor, Base destructor, Derived destructor
B. Base constructor, Derived constructor, Derived destructor, Base destructor
C. Derived constructor, Base constructor, Derived destructor, Base destructor
D. Base constructor, Derived constructor, Base destructor, Derived destructor

39 Left and Right both define print(), and Result inherits publicly from both. How can Result r; call the version inherited from Left?

Resolving ambiguities in inheritance Medium
A. Left.r::print();
B. r.Left::print();
C. r.print(Left);
D. r::Left.print();

40 Left and Right virtually inherit Shared, and Final inherits from both Left and Right. How many Shared base-class subobjects does one Final object contain?

Virtual base class Medium
A. Three shared subobjects
B. Two shared subobjects
C. No shared subobject
D. One shared subobject

41 Which statement correctly describes a fundamental restriction of C++ operator overloading?

Importance of operator overloading Hard
A. It can change an operator's arity
B. It cannot change an operator's precedence
C. It can change an operator's precedence
D. It can change an operator's associativity

42 For a class Counter, which pair of member declarations correctly distinguishes prefix and postfix increment overloads?

Unary operator overloading Hard
A. void operator++(); and void operator++(int, int);
B. Counter& operator++(int); and Counter operator++();
C. Counter operator++(int); and Counter& operator++();
D. Counter& operator++(); and Counter operator++(int);

43 A class Complex supports Complex + int and int + Complex with implicit conversion from int to Complex. Which implementation best supports both operand orders?

Binary operator overloading Hard
A. A member operator+(int) only
B. A non-member operator+(Complex, const Complex&)
C. A non-member operator+(int, Complex) only
D. A member operator+(const Complex&) only

44 Given class Distance { public: Distance(int); };, what happens in Distance d = 7; if the constructor is not declared explicit?

Basic type to class type conversion Hard
A. The compiler creates a temporary integer reference
B. The statement invokes a conversion operator from int
C. The integer is implicitly converted using Distance(int)
D. The statement is ill-formed because constructors never convert

45 For class Meter { public: operator double() const; };, which expression can use the conversion operator without an explicit cast?

Class type to basic type conversion Hard
A. Meter m; double x = m;
B. Meter m; void* p = m;
C. Meter m; Meter x = m;
D. Meter m; int* p = m;

46 Which design most directly demonstrates re-usability through inheritance rather than duplication?

Importance of re-usability through inheritance Hard
A. Making every class unrelated and self-contained
B. Defining shared behavior once in a base class
C. Replacing all derived classes with global functions
D. Copying common methods into every derived class

47 If class B : public A { }; is declared, which statement is correct?

Basics of inheritance Hard
A. Every private member of A becomes directly accessible in B
B. The base subobject of A is part of each B object
C. An A object automatically contains a separate B object
D. Every public member of A becomes private in B

48 A base pointer refers to a derived object. Which condition is required for delete basePtr; to destroy the complete derived object correctly?

Base class and derived class Hard
A. The base class must use private inheritance
B. The base destructor must be virtual
C. The derived destructor must be declared before the base destructor
D. The derived class must have only static members

49 Consider class B : public A { };. If A has a public member f() and B declares a private member f(), what is the effect of calling b.f() from outside B?

Simple inheritance Hard
A. Both functions execute in base-to-derived order
B. The private B::f() hides A::f() and access fails
C. The call is resolved by comparing return types
D. The public A::f() is selected automatically

50 Given A <- B <- C, where each class has a default constructor, in what order are constructors called when a C object is created?

Multilevel inheritance Hard
A. B, then A, then C
B. A, then B, then C
C. The order depends on the declaration order in C
D. C, then B, then A

51 If class D : public B1, public B2 { }; and both bases define show(), what is the correct way to call B2's version through a D object?

Multiple inheritance Hard
A. d.show(B2)
B. d.B2::show()
C. d->show<B2>()
D. B2 d.show()

52 In hierarchical inheritance, classes B and C both derive from A. Which statement is correct about their base subobjects?

Hierarchical inheritance Hard
A. The compiler merges all A subobjects globally
B. B contains C's A subobject
C. A single A subobject is shared automatically
D. Each object of B or C contains its own A subobject

53 A hierarchy combines multilevel and multiple inheritance, producing two paths from D to A. What issue can arise if the paths are non-virtual?

Hybrid inheritance Hard
A. All inherited members become automatically private
B. Multiple inheritance is converted into composition
C. The object may contain two distinct A subobjects
D. The derived class cannot define any constructor

54 For class D : private B { };, how are B's public and protected members accessible through D?

Private mode of inheritance Hard
A. They become private in D
B. They become protected in D
C. They remain public in D
D. They become inaccessible even inside D

55 For class D : protected B { };, which access transformation is correct?

Protected mode of inheritance Hard
A. Public and protected members of B become protected in D
B. All members of B become private in D
C. Public members remain public and protected members become private
D. Only private members of B become protected in D

56 Under public inheritance, which conversion is generally permitted when the base is accessible and unambiguous?

Public mode of inheritance Hard
A. Private base pointer to public derived pointer
B. Unrelated pointer to derived pointer
C. Base pointer to derived pointer without a cast
D. Derived pointer to base pointer

57 A base declares virtual void f() const;. Which derived declaration correctly overrides it?

Overriding of member functions Hard
A. void f();
B. void f(int) const;
C. void f() const;
D. int f() const;

58 For class D : public B { M m; };, assuming normal construction and destruction, which order is correct?

Order of execution of constructors and destructor Hard
A. Construct m, construct B, construct D; destroy D, B, m
B. Construct B, construct m, construct D; destroy D, m, B
C. Construct D, construct B, construct m; destroy B, m, D
D. Construct B, construct D, construct m; destroy m, D, B

59 If D inherits f() from both B1 and B2, which declaration inside D resolves calls to d.f() by selecting B1's implementation?

Resolving ambiguities in inheritance Hard
A. override B1::f;
B. using D::f;
C. friend B1::f;
D. using B1::f;

60 In a diamond hierarchy using virtual public A, which class is responsible for constructing the virtual base A?

Virtual base class Hard
A. The most-derived class
B. The class whose constructor runs last
C. The rightmost intermediate class
D. The leftmost intermediate class