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++?
operator
override
virtual
typedef
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++(Number);
Number operator+(int);
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();
24
4
10
6
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
operator int()
Time(int)
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;
0
85
s
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();
d::show();
Base.show(d);
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
age and id only
name 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; obj->run();
C obj; A.run();
C obj; obj.A.run();
C obj; obj.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();
SP
PS
P
S
32
Given class Car : public Vehicle and class Bike : public Vehicle, which statement best describes the relationship?
Bike is derived indirectly from Car
Vehicle inherits from two base classes
Car is the base class of Bike
Car and Bike share one base class
33
Consider:
class Base {
public:
void f() {}
protected:
void g() {}
};
class Derived : private Base {
public:
void test() { f(); g(); }
};
Which statement is correct?
test() cannot call either inherited member function
test() can call only f(), and users can call f()
test() can call only g(), and users can call g()
test() can call both, but users cannot call f() through Derived
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 d2 = Base();
Base* p = &d;
Base& r = Base::Derived;
Derived* p = new Base;
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();
Derived
BaseDerived
Base
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?
CBAabc with spaces removed
ABCabc with spaces removed
BACcab with spaces removed
ABCcb a 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.show(Printer);
d::Printer.show();
Printer.d.show();
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 subobjects
Person subobject
Person subobjects
Person subobject
40
Which design most clearly represents aggregation between Team and Player?
Team creates a local Player only inside one method
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
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) { ++n; return *this; }
Counter Counter::operator++(int) { Counter t(*this); ++n; return t; }
Counter& Counter::operator++() { Counter t(*this); ++n; return t; }
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?
Complex(2) + 3
Complex(2) + Complex(3)
3.0 + Complex(2)
Complex(2) + 3.0
43
Given class Distance { public: explicit Distance(double); };, which initialization is valid without a cast?
void f(Distance); f(4.5);
Distance d = 4.5;
Distance d(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{};
void g(bool); g(Flag{});
bool b = Flag{};
if (Flag f; f) {}
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?
2, because f remains dynamically dispatched
1, because override disables virtual dispatch
1, because object slicing leaves a standalone B
46
Given struct B { B(int); }; struct D : B { D(); };, which definition correctly constructs D when B has no default constructor?
D::D() : B(7) {}
D::D() { this->B(7); }
D::D() { B(7); }
D::D() : D(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;?
A(), B(), C(), ~C(), ~B(), ~A()
C(), A(), B(), ~C(), ~A(), ~B()
C(), B(), A(), ~A(), ~B(), ~C()
A(), B(), C(), ~A(), ~B(), ~C()
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?
0, because both pointers have static type B*
1, because only the first sibling supplies an override
3, because each sibling override dispatches independently
50
Given struct B { void f(); }; struct D : private B { void g() { f(); } };, which statement is correct for unrelated client code?
D can implicitly convert to B, and clients may call f directly
D cannot implicitly convert to B, and D::g cannot call f
D cannot implicitly convert to B, but D::g may call f
D can implicitly convert to B, but 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?
p() and q()
p()
q()
p() nor 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*
B subobject before the D body
D members before the B subobject
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
2, while the call dispatches to D::f
1, while the call dispatches to D::f
2, while the call dispatches to B::f
54
For struct D : B1, B2 { M2 m2; M1 m1; D() : m1(), B2(), m2(), B1() {} };, what is the actual construction order?
B2, B1, m1, m2, then the D body
B1, B2, m1, m2, then the D body
m1, B2, m2, B1, 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() { this->show(); }
void C::test() { show<B>(); }
void C::test() { ::B.show(); }
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(3) from D
A(1) and A(2)
A(1) from B
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 : public Engine {};
class Garage { static Engine 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 destroys e, then w in increasing index order
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
59
For a value-like Matrix type, which declaration best supports symmetric implicit conversions on both operands of addition?
static Matrix operator+(Matrix lhs, Matrix rhs);
Matrix operator+(const Matrix& rhs) const;
friend Matrix& operator+(Matrix& lhs, Matrix& rhs);
friend Matrix operator+(Matrix lhs, const 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 →