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. Assignment operator
B. Unary operator
C. Binary operator
D. Ternary operator

2 How many operands does a binary operator use?

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

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

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

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

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

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

Derived class and base class Easy
A. Base class
B. Derived class
C. Nested class
D. Local 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. Multilevel inheritance
B. Simple inheritance
C. Hierarchical inheritance
D. Multiple inheritance

8 What is multiple inheritance?

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

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

Hierarchical inheritance Easy
A. Hierarchical inheritance
B. Simple inheritance
C. Multiple 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. Private members
B. Protected members
C. Virtual members
D. Public members

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

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

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

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

13 When does member function overriding occur?

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

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

Order of execution of constructors and destructors Easy
A. Base-class constructor
B. Derived-class constructor
C. Default destructor
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. Global destructor
C. Static destructor
D. Base-class destructor

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

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

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

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

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 class converts into a basic type
C. The part always exists without the whole
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. operator
B. override
C. virtual
D. 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()?

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++(Number);
D. 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();

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

Binary operator overloading Medium
A. Define two non-member overloads for the operand orders
B. Define one unary overload taking no arguments
C. Define only a conversion operator to int
D. Define one member overload taking an 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?

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

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. 0
B. 85
C. The code cannot compile
D. The address of 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?

Derived class and base class Medium
A. d.show();
B. d::show();
C. Base.show(d);
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. age and id only
C. name 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; obj->run();
B. C obj; A.run();
C. C obj; obj.A.run();
D. 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();

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

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

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

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

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

35 Which conversion is permitted by public inheritance?

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

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

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

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

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

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

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

Aggregation Medium
A. Team creates a local Player only inside one method
B. Team stores a Player value and exclusively owns its lifetime
C. Team publicly inherits from the Player class
D. 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?

Unary operator overloading Hard
A. Counter& Counter::operator++(int) { ++n; return *this; }
B. Counter Counter::operator++(int) { Counter t(*this); ++n; return t; }
C. Counter& Counter::operator++() { Counter t(*this); ++n; return t; }
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. Complex(2) + 3
B. Complex(2) + Complex(3)
C. 3.0 + Complex(2)
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. void f(Distance); f(4.5);
B. Distance d = 4.5;
C. Distance d(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. void g(bool); g(Flag{});
C. bool b = Flag{};
D. 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?

Derived class and base class Hard
A. Undefined behavior, because the derived part is missing
B. 2, because f remains dynamically dispatched
C. 1, because override disables virtual dispatch
D. 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?

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

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

Multiple inheritance Hard
A. B::f(double) is selected after numeric promotion
B. Both functions execute in base declaration order
C. Compilation fails because member-name lookup is ambiguous
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. The calls are ambiguous between the sibling implementations
B. 0, because both pointers have static type B*
C. 1, because only the first sibling supplies an override
D. 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?

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

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

Public inheritance Hard
A. Passing a D* to a function expecting B*
B. Constructing the B subobject before the D body
C. Destroying the D members before the B subobject
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. 2, while the call dispatches to D::f
C. 1, while the call dispatches to D::f
D. 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?

Order of execution of constructors and destructors Hard
A. B2, B1, m1, m2, then the D body
B. B1, B2, m1, m2, then the D body
C. m1, B2, m2, B1, 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() { this->show(); }
B. void C::test() { show<B>(); }
C. void C::test() { ::B.show(); }
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(3) from D
B. Both A(1) and A(2)
C. A(1) from B
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 : public Engine {};
C. class Garage { static Engine 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 destroys e, then w in increasing index order
B. Destroying Car leaves both e and w alive independently
C. Destroying Car destroys w in increasing index order, then e
D. Destroying 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?

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

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