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 two operands
B. + with two operands
C. * with two operands
D. - with one operand

3 How many operands does a binary operator normally use?

Binary operator overloading Easy
A. Three operands
B. Two operands
C. No 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 destructor
B. A virtual table
C. A friend variable
D. A constructor

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 default destructor
C. A conversion function
D. A base class

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

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

7 What does inheritance allow a class to do?

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

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

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

9 What is simple inheritance?

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

10 Which structure represents multilevel inheritance?

Multilevel inheritance Easy
A. A, B, and C are unrelated
B. A inherits from B and C
C. A and B inherit from C
D. A inherits from B, and B inherits 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. Multiple classes have no relationship
D. One derived class has multiple base classes

12 Which arrangement represents hierarchical inheritance?

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

13 What is hybrid inheritance?

Hybrid inheritance Easy
A. Inheritance using only one class
B. A combination of two or more inheritance types
C. Inheritance using only private data
D. Inheritance without a base 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. Private members
B. Public members
C. Static members
D. Protected 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. Public
C. Private
D. Hidden

17 What does function overriding mean in inheritance?

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

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

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

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

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

20 Why is a virtual base class used in inheritance?

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

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

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

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 = 5, d = 5
B. c = 4, d = 4
C. c = 5, d = 4
D. c = 4, d = 5

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(6, 2)
D. Point(-2, 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. double d = Distance(12.5);
B. Distance d; d = Distance();
C. int d = static_cast<int>(12.5);
D. Distance d = 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. Score(int value);
D. int Score() const;

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. Place all employee data in global variables
B. Define the common features in one base class
C. Implement each employee class without relationships
D. Repeat the common features in every derived class

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 directly through a base-class object in main
C. Access through any unrelated function
D. Access through any function receiving a base pointer

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. Derived::show() is called
B. The statement fails to compile
C. Both versions are called
D. Base::show() is called

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

Simple inheritance Medium
A. An Employee object can call getName() publicly
B. getName() becomes protected inside Employee
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.A::B::test();
D. C obj; obj.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.read().write();
B. d.Reader(); d.Writer();
C. Reader.d.read(); Writer.d.write();
D. d.read(); 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. Car* to Vehicle*
B. Bike object to Car object
C. Vehicle* to Car*
D. Car* to Bike*

33 Which class arrangement is an example of hybrid inheritance?

Hybrid inheritance Medium
A. A class combines multilevel and multiple inheritance
B. One class contains an object of another class
C. Several unrelated classes define identical functions
D. One class derives directly from only one base 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 become protected members
C. Both become inaccessible to Derived
D. Both remain publicly accessible

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 become protected
C. Both remain public
D. Both become inaccessible

36 Under public inheritance, which access transformation is correct?

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

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. The base function is removed from the derived class
B. The derived function does not override the base function
C. The derived function overrides the base function normally
D. Both functions become non-virtual automatically

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. Base constructor, Derived constructor, Base destructor, Derived destructor
B. Base constructor, Derived constructor, Derived destructor, Base destructor
C. Derived constructor, Base constructor, Base destructor, Derived destructor
D. Derived constructor, Base constructor, Derived destructor, Base 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. r.print(Left);
B. r.Left::print();
C. r::Left.print();
D. Left.r::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. Two shared subobjects
B. Three shared subobjects
C. One shared subobject
D. No shared subobject

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

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

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 non-member operator+(Complex, const Complex&)
B. A non-member operator+(int, Complex) only
C. A member operator+(const Complex&) only
D. A member operator+(int) 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 statement is ill-formed because constructors never convert
B. The compiler creates a temporary integer reference
C. The statement invokes a conversion operator from int
D. The integer is implicitly converted using Distance(int)

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; int* p = m;
C. Meter m; Meter x = m;
D. Meter m; void* 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. Copying common methods into every derived class
D. Replacing all derived classes with global functions

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

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

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 derived class must have only static members
B. The base destructor must be virtual
C. The base class must use private inheritance
D. The derived destructor must be declared before the base destructor

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. The private B::f() hides A::f() and access fails
B. The public A::f() is selected automatically
C. Both functions execute in base-to-derived order
D. The call is resolved by comparing return types

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. C, then B, then A
D. The order depends on the declaration order in C

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. B2 d.show()
C. d.B2::show()
D. d->show<B2>()

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. Each object of B or C contains its own A subobject
C. B contains C's A subobject
D. A single A subobject is shared automatically

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. The object may contain two distinct A subobjects
B. The derived class cannot define any constructor
C. All inherited members become automatically private
D. Multiple inheritance is converted into composition

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 inaccessible even inside D
B. They become private in D
C. They remain public in D
D. They become protected in D

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

Protected mode of inheritance Hard
A. All members of B become private in D
B. Public members remain public and protected members become private
C. Public and protected members of B become protected in D
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. Derived pointer to base pointer
C. Unrelated pointer to derived pointer
D. Base pointer to derived pointer without a cast

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

Overriding of member functions Hard
A. void f(int) const;
B. void f();
C. int f() const;
D. void 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 D, construct B, construct m; destroy B, m, D
C. Construct B, construct m, construct D; destroy D, m, B
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. friend B1::f;
B. using B1::f;
C. using D::f;
D. override 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 rightmost intermediate class
B. The most-derived class
C. The leftmost intermediate class
D. The class whose constructor runs last