Unit 4: Operator Overloading, Type Casting and Re-usability - Practice Quiz
1 What is the main purpose of operator overloading in C++?
2 Which operator is an example of a unary operator?
- with one operand
+ with two operands
* with two operands
/ with two operands
3 How many operands does a binary operator normally use?
4 Which member is commonly used to convert a basic type into a class type?
5 Which feature is commonly used to convert a class object into a basic type?
6 What is a major benefit of inheritance in C++?
7 What does inheritance allow a class to do?
8 In inheritance, what is the class that provides properties to another class called?
9 What is simple inheritance?
10 Which structure represents multilevel inheritance?
11 What is multiple inheritance?
12 Which arrangement represents hierarchical inheritance?
13 What is hybrid inheritance?
14 In private inheritance, public and protected members of the base class become what in the derived class?
15 In protected inheritance, public members of the base class become what in the derived class?
16 In public inheritance, public members of the base class remain what in the derived class?
17 What does function overriding mean in inheritance?
18 When an object of a derived class is created, which constructor executes first?
19 How can an ambiguity between two inherited functions with the same name be resolved?
20 Why is a virtual base class used in inheritance?
21
Why is operator overloading useful for a class such as Complex in C++?
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?
c = 4, d = 5
c = 5, d = 4
c = 5, d = 5
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)?
Point(8, -3)
Point(6, 4)
Point(-2, 2)
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?
int d = static_cast<int>(12.5);
Distance d = 12.5;
Distance d; d = Distance();
double d = Distance(12.5);
25
Which member declaration enables an object of class Score to be converted to int?
operator int() const;
friend int(Score);
int Score() const;
Score(int value);
26
A program has several employee classes that all require name, id, and displayIdentity(). Which design best promotes re-usability?
27
A base class declares protected: int value;. Which use of value is valid?
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::show() is called
Derived::show() is called
29
Given class Employee : public Person, where Person has a public method getName(), which statement is correct?
getName() becomes protected inside Employee
Employee object can call getName() publicly
Employee object cannot contain Person members
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?
A obj; obj.B::test();
B obj; obj.C::test();
C obj; obj.test();
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?
d.Reader(); d.Writer();
d.read(); d.write();
d.read().write();
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?
Vehicle* to Car*
Bike object to Car object
Car* to Bike*
Car* to Vehicle*
33 Which class arrangement is an example of hybrid inheritance?
34
If Derived privately inherits Base, what happens to the public and protected members of Base inside Derived?
Derived
35
If Derived inherits Base using protected inheritance, how are the public and protected members of Base treated in Derived?
36 Under public inheritance, which access transformation is correct?
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?
38
For class Derived : public Base, a Derived object is created and then destroyed. What is the correct execution order?
39
Left and Right both define print(), and Result inherits publicly from both. How can Result r; call the version inherited from Left?
Left.r::print();
r.Left::print();
r.print(Left);
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?
41 Which statement correctly describes a fundamental restriction of C++ operator overloading?
42
For a class Counter, which pair of member declarations correctly distinguishes prefix and postfix increment overloads?
void operator++(); and void operator++(int, int);
Counter& operator++(int); and Counter operator++();
Counter operator++(int); and Counter& operator++();
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?
operator+(int) only
operator+(Complex, const Complex&)
operator+(int, Complex) only
operator+(const Complex&) only
44
Given class Distance { public: Distance(int); };, what happens in Distance d = 7; if the constructor is not declared explicit?
int
Distance(int)
45
For class Meter { public: operator double() const; };, which expression can use the conversion operator without an explicit cast?
Meter m; double x = m;
Meter m; void* p = m;
Meter m; Meter x = m;
Meter m; int* p = m;
46 Which design most directly demonstrates re-usability through inheritance rather than duplication?
47
If class B : public A { }; is declared, which statement is correct?
A becomes directly accessible in B
A is part of each B object
A object automatically contains a separate B object
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?
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?
B::f() hides A::f() and access fails
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?
B, then A, then C
A, then B, then C
C
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?
d.show(B2)
d.B2::show()
d->show<B2>()
B2 d.show()
52
In hierarchical inheritance, classes B and C both derive from A. Which statement is correct about their base subobjects?
A subobjects globally
B contains C's A subobject
A subobject is shared automatically
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?
A subobjects
54
For class D : private B { };, how are B's public and protected members accessible through D?
D
D
D
D
55
For class D : protected B { };, which access transformation is correct?
B become protected in D
B become private in D
B become protected in D
56 Under public inheritance, which conversion is generally permitted when the base is accessible and unambiguous?
57
A base declares virtual void f() const;. Which derived declaration correctly overrides it?
void f();
void f(int) const;
void f() const;
int f() const;
58
For class D : public B { M m; };, assuming normal construction and destruction, which order is correct?
m, construct B, construct D; destroy D, B, m
B, construct m, construct D; destroy D, m, B
D, construct B, construct m; destroy B, m, D
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?
override B1::f;
using D::f;
friend B1::f;
using B1::f;
60
In a diamond hierarchy using virtual public A, which class is responsible for constructing the virtual base A?
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →