Unit 4: Operator Overloading, Type Conversion and Inheritance - Practice Quiz

CSE202 — Object Oriented Programming 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which type of operator acts on only one operand?

Unary operator overloading Easy
A. Unary operator
B. Binary operator
C. Assignment operator
D. Ternary operator

2 How many operands does a binary operator use?

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

3 Which class feature commonly converts a basic type into a class type in C++?

Basic type to class type conversion Easy
A. Virtual function
B. Default destructor
C. Conversion constructor
D. Copy constructor

4 Which C++ feature can convert a class object into a basic type?

Class type to basic type conversion Easy
A. Conversion function
B. Friend function
C. Static function
D. Inline function

5 What is a class called when another class inherits from it?

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

6 What does simple inheritance involve?

Simple inheritance Easy
A. Several classes at multiple levels
B. One derived class and one base class
C. One class and several base classes
D. Several derived classes from one base

7 Which inheritance structure forms a chain such as A to B to C?

Multilevel inheritance Easy
A. Simple inheritance
B. Multiple inheritance
C. Multilevel inheritance
D. Hierarchical inheritance

8 What is multiple inheritance?

Multiple inheritance Easy
A. Several classes inherit from one base class
B. A class inherits from several base classes
C. A class inherits through several levels
D. A class contains several member objects

9 Which inheritance type has several derived classes inheriting from one base class?

Hierarchical inheritance Easy
A. Simple inheritance
B. Multiple inheritance
C. Hierarchical inheritance
D. Multilevel inheritance

10 Under private inheritance, what do the base class's public members become in the derived class?

Private inheritance Easy
A. Public members
B. Protected members
C. Virtual members
D. Private members

11 Under protected inheritance, what do the base class's public members become in the derived class?

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

12 Under public inheritance, what happens to the base class's public members in the derived class?

Public inheritance Easy
A. They become private
B. They become protected
C. They become static
D. They remain public

13 When does member function overriding occur?

Overriding member functions Easy
A. A derived class redefines a base function
B. A class creates several objects
C. A function accepts several parameters
D. A class defines several constructors

14 When a derived-class object is created, which constructor runs first?

Order of execution of constructors and destructors Easy
A. Default destructor
B. Base-class constructor
C. Derived-class constructor
D. Copy constructor

15 When a derived-class object is destroyed, which destructor runs first?

Order of execution of constructors and destructors Easy
A. Derived-class destructor
B. Static destructor
C. Base-class destructor
D. Global destructor

16 Which C++ operator can specify the base class that owns an ambiguous member?

Resolving ambiguities in inheritance Easy
A. Conditional operator
B. Scope resolution operator
C. Member access operator
D. Address-of operator

17 What is the main purpose of a virtual base class?

Virtual base class Easy
A. To hide all inherited members
B. To avoid duplicate base-class copies
C. To overload arithmetic operators
D. To convert objects into integers

18 Which statement best describes aggregation?

Aggregation Easy
A. The contained object depends on its owner
B. The contained object can exist independently
C. The derived class replaces its base class
D. The class inherits from multiple classes

19 Which statement best describes composition?

Composition Easy
A. The part's lifetime depends on the whole
B. The part always exists without the whole
C. The class converts into a basic type
D. The class inherits through several levels

20 Which keyword is used when declaring an overloaded operator function in C++?

Binary operator overloading Easy
A. typedef
B. override
C. operator
D. 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()?

Unary operator overloading Medium
A. -7 and -7
B. -7 and 7
C. 7 and -7
D. 7 and 7

22 Which declaration correctly overloads the postfix increment operator as a member of class Number?

Unary operator overloading Medium
A. Number operator+(int);
B. Number operator++();
C. Number operator++(int);
D. 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();

Binary operator overloading Medium
A. 6
B. 4
C. 10
D. 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?

Binary operator overloading Medium
A. Define one unary overload taking no arguments
B. Define one member overload taking an int
C. Define only a conversion operator to int
D. Define two non-member overloads for the operand orders

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?

Basic type to class type conversion Medium
A. A base-class conversion to Time
B. The single-argument constructor Time(int)
C. A conversion operator operator int()
D. The default constructor of Time

26 Suppose Length declares explicit Length(double value);. Which statement is valid C++?

Basic type to class type conversion Medium
A. Length a = 2.5;
B. Length a(2.5);
C. Length a = {2.5};
D. 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;

Class type to basic type conversion Medium
A. 85
B. The code cannot compile
C. The address of s
D. 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?

Derived class and base class Medium
A. d::show();
B. Base.show(d);
C. d.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();
};

Simple inheritance Medium
A. age, name, and id
B. name and id only
C. age and id only
D. 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?

Multilevel inheritance Medium
A. C obj; A.run();
B. C obj; obj->run();
C. C obj; obj.run();
D. 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();

Multiple inheritance Medium
A. S
B. SP
C. P
D. PS

32 Given class Car : public Vehicle and class Bike : public Vehicle, which statement best describes the relationship?

Hierarchical inheritance Medium
A. Car is the base class of Bike
B. Vehicle inherits from two base classes
C. Car and Bike share one base class
D. 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?

Private inheritance Medium
A. test() can call both, but users cannot call f() through Derived
B. test() can call only g(), and users can call g()
C. test() cannot call either inherited member function
D. 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?

Protected inheritance Medium
A. Both become private members
B. Public stays public and protected becomes private
C. Both remain public members
D. Both become protected members

35 Which conversion is permitted by public inheritance?

class Base {};
class Derived : public Base {};
Derived d;

Public inheritance Medium
A. Derived* p = new Base;
B. Base& r = Base::Derived;
C. Derived d2 = Base();
D. 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();

Overriding member functions Medium
A. Nothing is printed
B. BaseDerived
C. Base
D. 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?

Order of execution of constructors and destructors Medium
A. ABCcb a with spaces removed
B. ABCabc with spaces removed
C. CBAabc with spaces removed
D. 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?

Resolving ambiguities in inheritance Medium
A. d.Printer::show();
B. d::Printer.show();
C. Printer.d.show();
D. 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?

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

40 Which design most clearly represents aggregation between Team and Player?

Aggregation Medium
A. Team stores a Player value and exclusively owns its lifetime
B. Team publicly inherits from the Player class
C. Team stores pointers to independently created Player objects
D. 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?

Unary operator overloading Hard
A. Counter Counter::operator++(int) { Counter t(*this); ++n; return t; }
B. Counter Counter::operator++() { Counter t(*this); ++n; return t; }
C. Counter& Counter::operator++(int) { ++n; return *this; }
D. 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?

Binary operator overloading Hard
A. 3.0 + Complex(2)
B. Complex(2) + Complex(3)
C. Complex(2) + 3
D. Complex(2) + 3.0

43 Given class Distance { public: explicit Distance(double); };, which initialization is valid without a cast?

Basic type to class type conversion Hard
A. Distance d = 4.5;
B. Distance d(4.5);
C. void f(Distance); f(4.5);
D. 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?

Class type to basic type conversion Hard
A. int n = Flag{};
B. bool b = Flag{};
C. if (Flag f; f) {}
D. 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?

Derived class and base class Hard
A. 1, because override disables virtual dispatch
B. 1, because object slicing leaves a standalone B
C. 2, because f remains dynamically dispatched
D. Undefined behavior, because the derived part is missing

46 Given struct B { B(int); }; struct D : B { D(); };, which definition correctly constructs D when B has no default constructor?

Simple inheritance Hard
A. D::D() : D(7) {}
B. D::D() { B(7); }
C. D::D() : B(7) {}
D. 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;?

Multilevel inheritance Hard
A. C(), A(), B(), ~C(), ~A(), ~B()
B. A(), B(), C(), ~A(), ~B(), ~C()
C. C(), B(), A(), ~A(), ~B(), ~C()
D. 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);?

Multiple inheritance Hard
A. Compilation fails because member-name lookup is ambiguous
B. B::f(double) is selected after numeric promotion
C. Both functions execute in base declaration order
D. 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?

Hierarchical inheritance Hard
A. 3, because each sibling override dispatches independently
B. 1, because only the first sibling supplies an override
C. The calls are ambiguous between the sibling implementations
D. 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?

Private inheritance Hard
A. D cannot implicitly convert to B, but D::g may call f
B. D can implicitly convert to B, and clients may call f directly
C. D can implicitly convert to B, but D::g cannot call f
D. 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?

Protected inheritance Hard
A. Only q()
B. Only p()
C. Neither p() nor q()
D. Both 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?

Public inheritance Hard
A. Passing a D* to a function expecting B*
B. Destroying the D members before the B subobject
C. Constructing the B subobject before the D body
D. Selecting 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()?

Overriding member functions Hard
A. 1, while the call dispatches to B::f
B. 1, while the call dispatches to D::f
C. 2, while the call dispatches to B::f
D. 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?

Order of execution of constructors and destructors Hard
A. m1, B2, m2, B1, then the D body
B. B1, B2, m1, m2, then the D body
C. B2, B1, m1, m2, then the D body
D. 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?

Resolving ambiguities in inheritance Hard
A. void C::test() { B::show(); }
B. void C::test() { this->show(); }
C. void C::test() { show<B>(); }
D. 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?

Virtual base class Hard
A. A(1) from B
B. A(3) from D
C. Both A(1) and A(2)
D. A(2) from C

57 Which design most clearly represents aggregation of an existing Engine whose lifetime is independent of a Garage?

Aggregation Hard
A. class Garage { Engine& engine; };
B. class Garage { static Engine engine; };
C. class Garage : public Engine {};
D. 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?

Composition Hard
A. Destroying Car leaves both e and w alive independently
B. Destroying Car destroys w in increasing index order, then e
C. Destroying Car destroys w in reverse index order, then e
D. Destroying 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?

Binary operator overloading Hard
A. friend Matrix& operator+(Matrix& lhs, Matrix& rhs);
B. friend Matrix operator+(Matrix lhs, const Matrix& rhs);
C. Matrix operator+(const Matrix& rhs) const;
D. 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?

Virtual base class Hard
A. static_cast<A*>(static_cast<B*>(&d)) equals the corresponding pointer through C
B. Qualifying d.B::x and d.C::x accesses different integers
C. D contains two independent A::x data members
D. Converting D* directly to A* is ambiguous