Unit 4: Operator Overloading, Type Conversion and Inheritance - Practice Quiz
1 Which type of operator acts on only one operand?
2 How many operands does a binary operator use?
3 Which class feature commonly converts a basic type into a class type in C++?
4 Which C++ feature can convert a class object into a basic type?
5 What is a class called when another class inherits from it?
6 What does simple inheritance involve?
7
Which inheritance structure forms a chain such as A to B to C?
8 What is multiple inheritance?
9 Which inheritance type has several derived classes inheriting from one base class?
10 Under private inheritance, what do the base class's public members become in the derived class?
11 Under protected inheritance, what do the base class's public members become in the derived class?
12 Under public inheritance, what happens to the base class's public members in the derived class?
13 When does member function overriding occur?
14 When a derived-class object is created, which constructor runs first?
15 When a derived-class object is destroyed, which destructor runs first?
16 Which C++ operator can specify the base class that owns an ambiguous member?
17 What is the main purpose of a virtual base class?
18 Which statement best describes aggregation?
19 Which statement best describes composition?
20 Which keyword is used when declaring an overloaded operator function in C++?
typedef
override
operator
virtual
21
Consider the following C++ class:
class Counter {
int value;
public:
Counter(int v) : value(v) {}
Counter operator-() const {
return Counter(-value);
}
int get() const { return value; }
};
Counter a(7);
Counter b = -a;
What are the values returned by a.get() and b.get()?
-7 and -7
-7 and 7
7 and -7
7 and 7
22
Which declaration correctly overloads the postfix increment operator as a member of class Number?
Number operator+(int);
Number operator++();
Number operator++(int);
Number& operator++(Number);
23
Given the class below, what is printed?
class Point {
int x;
public:
Point(int n) : x(n) {}
Point operator+(const Point& p) const {
return Point(x + p.x);
}
int get() const { return x; }
};
Point p1(4), p2(6);
Point p3 = p1 + p2;
cout << p3.get();
6
4
10
24
24
A class Distance must support both d + 5 and 5 + d, where d is a Distance object and the constructor from int is explicit. Which design directly supports both expressions?
int
int
25
Consider this C++ code:
class Time {
int minutes;
public:
Time(int m) : minutes(m) {}
};
Time t = 90;
Which mechanism performs the conversion in the final statement?
Time
Time(int)
operator int()
Time
26
Suppose Length declares explicit Length(double value);. Which statement is valid C++?
Length a = 2.5;
Length a(2.5);
Length a = {2.5};
Length a; a = 2.5;
27
Given the following class, what is assigned to n?
class Score {
int points;
public:
Score(int p) : points(p) {}
operator int() const { return points; }
};
Score s(85);
int n = s;
85
s
0
28
Consider:
class Base {
public:
void show() { cout << "Base"; }
};
class Derived : public Base {
public:
void display() { cout << "Derived"; }
};
Derived d;
Which statement calls the inherited member function?
d::show();
Base.show(d);
d.show();
d.Base();
29
In the following simple inheritance relationship, which members can Employee::print() directly access?
class Person {
private:
int age;
protected:
string name;
public:
int id;
};
class Employee : public Person {
public:
void print();
};
age, name, and id
name and id only
age and id only
age and name only
30
Given class B : public A and class C : public B, where A has a public method run() and neither B nor C hides it, which call is valid?
C obj; A.run();
C obj; obj->run();
C obj; obj.run();
C obj; obj.A.run();
31
What is printed by this program fragment?
class Printer {
public:
void print() { cout << "P"; }
};
class Scanner {
public:
void scan() { cout << "S"; }
};
class Machine : public Printer, public Scanner {};
Machine m;
m.print();
m.scan();
S
SP
P
PS
32
Given class Car : public Vehicle and class Bike : public Vehicle, which statement best describes the relationship?
Car is the base class of Bike
Vehicle inherits from two base classes
Car and Bike share one base class
Bike is derived indirectly from Car
33
Consider:
class Base {
public:
void f() {}
protected:
void g() {}
};
class Derived : private Base {
public:
void test() { f(); g(); }
};
Which statement is correct?
test() can call both, but users cannot call f() through Derived
test() can call only g(), and users can call g()
test() cannot call either inherited member function
test() can call only f(), and users can call f()
34
When Derived inherits from Base using protected inheritance, what happens to the public and protected members of Base inside Derived?
35
Which conversion is permitted by public inheritance?
class Base {};
class Derived : public Base {};
Derived d;
Derived* p = new Base;
Base& r = Base::Derived;
Derived d2 = Base();
Base* p = &d;
36
What is printed by this code?
class Base {
public:
virtual void show() { cout << "Base"; }
};
class Derived : public Base {
public:
void show() override { cout << "Derived"; }
};
Derived d;
Base* p = &d;
p->show();
BaseDerived
Base
Derived
37
Consider:
class A { / constructor prints A, destructor prints a / };
class B { / constructor prints B, destructor prints b / };
class C : public A, public B {
/ constructor prints C, destructor prints c /
};
If a local C object is created and then goes out of scope, what is the complete output order?
ABCcb a with spaces removed
ABCabc with spaces removed
CBAabc with spaces removed
BACcab with spaces removed
38
Both Printer and Logger define void show(), and Device inherits publicly from both. Which statement explicitly calls the Printer version for object d of type Device?
d.Printer::show();
d::Printer.show();
Printer.d.show();
d.show(Printer);
39
Consider the diamond hierarchy:
class Person { public: int id; };
class Student : virtual public Person {};
class Employee : virtual public Person {};
class Assistant : public Student, public Employee {};
How many Person subobjects does one Assistant object contain?
Person subobject
Person subobject
Person subobjects
Person subobjects
40
Which design most clearly represents aggregation between Team and Player?
Team stores a Player value and exclusively owns its lifetime
Team publicly inherits from the Player class
Team stores pointers to independently created Player objects
Team creates a local Player only inside one method
41
Given class Counter { int n; public: Counter(int v): n(v) {} Counter operator++(int); };, which implementation gives Counter old = c++; the standard postfix semantics?
Counter Counter::operator++(int) { Counter t(*this); ++n; return t; }
Counter Counter::operator++() { Counter t(*this); ++n; return t; }
Counter& Counter::operator++(int) { ++n; return *this; }
Counter& Counter::operator++() { Counter t(*this); ++n; return t; }
42
Assume class Complex { public: Complex(double); Complex operator+(const Complex&) const; };. With no other overloads, which expression is ill-formed?
3.0 + Complex(2)
Complex(2) + Complex(3)
Complex(2) + 3
Complex(2) + 3.0
43
Given class Distance { public: explicit Distance(double); };, which initialization is valid without a cast?
Distance d = 4.5;
Distance d(4.5);
void f(Distance); f(4.5);
Distance d = {4.5};
44
Given class Flag { public: explicit operator bool() const; };, which use is valid solely because C++ permits contextual conversion to bool?
int n = Flag{};
bool b = Flag{};
if (Flag f; f) {}
void g(bool); g(Flag{});
45
Consider struct B { virtual int f() { return 1; } }; struct D : B { int f() override { return 2; } };. What does B b = D{}; int x = b.f(); assign to x?
1, because override disables virtual dispatch
1, because object slicing leaves a standalone B
2, because f remains dynamically dispatched
46
Given struct B { B(int); }; struct D : B { D(); };, which definition correctly constructs D when B has no default constructor?
D::D() : D(7) {}
D::D() { B(7); }
D::D() : B(7) {}
D::D() { this->B(7); }
47
For struct A { A(); ~A(); }; struct B : A { B(); ~B(); }; struct C : B { C(); ~C(); };, what is the complete call order for an automatic C c;?
C(), A(), B(), ~C(), ~A(), ~B()
A(), B(), C(), ~A(), ~B(), ~C()
C(), B(), A(), ~A(), ~B(), ~C()
A(), B(), C(), ~C(), ~B(), ~A()
48
Given struct A { void f(int); }; struct B { void f(double); }; struct C : A, B {};, what happens for C c; c.f(1);?
B::f(double) is selected after numeric promotion
A::f(int) is selected as the exact match
49
Given struct B { virtual int id() const { return 0; } }; struct D1 : B { int id() const override { return 1; } }; struct D2 : B { int id() const override { return 2; } };, what is the result of calling id() through B* p[] = {new D1, new D2}; and summing both results?
3, because each sibling override dispatches independently
1, because only the first sibling supplies an override
0, because both pointers have static type B*
50
Given struct B { void f(); }; struct D : private B { void g() { f(); } };, which statement is correct for unrelated client code?
D cannot implicitly convert to B, but D::g may call f
D can implicitly convert to B, and clients may call f directly
D can implicitly convert to B, but D::g cannot call f
D cannot implicitly convert to B, and D::g cannot call f
51
Given struct B { public: void p(); protected: void q(); }; struct D : protected B {}; struct E : D { void h(); };, which pair may E::h() call directly?
q()
p()
p() nor q()
p() and q()
52
Suppose struct B { virtual ~B() = default; }; struct D : public B {};. Which operation relies specifically on the public accessibility of the base relationship?
D* to a function expecting B*
D members before the B subobject
B subobject before the D body
D constructors for a direct D object
53
Given struct B { virtual void f(int x = 1); }; struct D : B { void f(int x = 2) override; }; B* p = new D;, which argument is passed by p->f()?
1, while the call dispatches to B::f
1, while the call dispatches to D::f
2, while the call dispatches to B::f
2, while the call dispatches to D::f
54
For struct D : B1, B2 { M2 m2; M1 m1; D() : m1(), B2(), m2(), B1() {} };, what is the actual construction order?
m1, B2, m2, B1, then the D body
B1, B2, m1, m2, then the D body
B2, B1, m1, m2, then the D body
B1, B2, m2, m1, then the D body
55
Given struct A { void show(); }; struct B { void show(); }; struct C : A, B { void test(); };, which definition makes test() invoke only B's implementation?
void C::test() { B::show(); }
void C::test() { this->show(); }
void C::test() { show<B>(); }
void C::test() { ::B.show(); }
56
Given struct A { A(int); }; struct B : virtual A { B() : A(1) {} }; struct C : virtual A { C() : A(2) {} }; struct D : B, C { D() : A(3) {} };, which constructor initializes the A subobject when constructing D?
A(1) from B
A(3) from D
A(1) and A(2)
A(2) from C
57
Which design most clearly represents aggregation of an existing Engine whose lifetime is independent of a Garage?
class Garage { Engine& engine; };
class Garage { static Engine engine; };
class Garage : public Engine {};
class Garage { Engine engine; };
58
Given struct Car { Engine e; Wheel w[4]; };, assuming ordinary value members and no dynamic allocation, which lifetime statement is correct?
Car leaves both e and w alive independently
Car destroys w in increasing index order, then e
Car destroys w in reverse index order, then e
Car destroys e, then w in increasing index order
59
For a value-like Matrix type, which declaration best supports symmetric implicit conversions on both operands of addition?
friend Matrix& operator+(Matrix& lhs, Matrix& rhs);
friend Matrix operator+(Matrix lhs, const Matrix& rhs);
Matrix operator+(const Matrix& rhs) const;
static Matrix operator+(Matrix lhs, Matrix rhs);
60
Given struct A { int x; }; struct B : virtual A {}; struct C : virtual A {}; struct D : B, C {}; D d;, which statement is correct?
static_cast<A*>(static_cast<B*>(&d)) equals the corresponding pointer through C
d.B::x and d.C::x accesses different integers
D contains two independent A::x data members
D* directly to A* is ambiguous
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 →