C++ Inheritance - Quizizz

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Worksheets Name

C++ Inheritance
Class
Total questions: 12
Worksheet time: 6mins
Date
Instructor name: Ishita Malhotra

 Multiple Choice

1. What is inheritance in C++?

a) Inheritance in C++ is not supported b) Inheritance in C++ is limited to


in object-oriented programming inheriting only data members from
another class
c) Inheritance in C++ allows a class to d) Inheritance in C++ is used to create
inherit properties and behaviors from new classes based on existing
another class. classes

 Multiple Choice

2. Explain the types of inheritance in C++ with examples.

a) Double inheritance, triple inheritance, b) Simple inheritance, complex


quadruple inheritance inheritance, compound inheritance
c) Linear inheritance, parallel d) The types of inheritance in C++ are
inheritance, divergent inheritance single inheritance, multiple
inheritance, multilevel inheritance,
hierarchical inheritance, and hybrid
inheritance.

 Multiple Choice

3. What is the syntax for inheriting a class in C++?

a) class DerivedClass :: BaseClass {} b) class DerivedClass : accessSpecifier


BaseClass {}
c) class DerivedClass : BaseClass {} d) class DerivedClass : public
BaseClass {}
 Multiple Choice

4. What is the difference between public, private, and protected inheritance in C++?

a) Protected inheritance makes the b) Public inheritance makes the private


private members of the base class members of the base class
protected in the derived class accessible in the derived class
c) Public inheritance makes the public d) Private inheritance makes the public
members of the base class members of the base class private in
accessible in the derived class, the derived class
private inheritance makes the public
and protected members of the base
class private in the derived class, and
protected inheritance makes the
public and protected members of the
base class protected in the derived
class.

 Multiple Choice

5. How does C++ support multiple inheritance?

a) C++ supports multiple inheritance by b) C++ supports multiple inheritance by


allowing a class to inherit from only allowing a class to inherit from
one base class. derived classes only.
c) C++ supports multiple inheritance by d) C++ supports multiple inheritance by
allowing a class to inherit from more allowing a class to inherit from
than one base class. interfaces only.
 Multiple Choice

6. Explain the concept of virtual inheritance in C++.

a) Virtual inheritance in C++ is used to b) Virtual inheritance in C++ is used to


create instances of classes that are allow multiple instances of the
not physically present in memory common base class to be inherited
by the derived class
c) Virtual inheritance in C++ is used to d) Virtual inheritance in C++ is used to
prevent a class from being inherited resolve the ambiguity that arises
by any other class when a class is derived from
multiple classes that have a
common base class. It ensures that
only one instance of the common
base class is inherited by the derived
class.

 Multiple Choice

7. What are the advantages of using inheritance in C++?

a) The advantages of using inheritance b) Increasing code complexity


in C++ include code reusability,
promoting polymorphism, and
creating a hierarchical class
structure.
c) Reducing code modularity d) Limiting flexibility in class design

 Multiple Choice

8. Discuss the potential drawbacks of using inheritance in C++.

a) Enhanced encapsulation b) Potential drawbacks of using


inheritance in C++ include tight
coupling between classes and the
diamond problem.
c) Improved modularity d) Increased code reusability
 Multiple Choice

9. How can you prevent a class from being inherited in C++?

a) Use the 'static' keyword when b) Use the 'sealed' keyword when
declaring the class. declaring the class.
c) Use the 'private' keyword when d) Use the 'final' keyword when
declaring the class. declaring the class.

 Multiple Choice

10. Provide an example of a real-world scenario where inheritance in C++ would be


beneficial.

a) Banking system with different types b) Using inheritance in C++ leads to


of accounts inheriting common slower performance and should be
attributes and behaviors from a avoided
base account class.
c) Inheritance in C++ is never d) Inheritance in C++ is outdated and
beneficial, it only adds unnecessary should be replaced with
complexity composition

 Multiple Choice

11. What are the different types of polymorphism in C++? Explain with examples.

a) Static polymorphism and dynamic b) Single polymorphism and multiple


polymorphism. Static polymorphism polymorphism. Single
is achieved through virtual functions polymorphism is achieved through
and dynamic polymorphism is function overloading and multiple
achieved through function polymorphism is achieved through
overloading. operator overloading.
c) Simple polymorphism and complex d) Compile-time polymorphism and
polymorphism. Simple runtime polymorphism. Compile-
polymorphism is achieved through time polymorphism is achieved
operator overloading and complex through function overloading and
polymorphism is achieved through operator overloading, while runtime
function overloading. polymorphism is achieved through
virtual functions and function
overriding.
 Multiple Choice

12. Explain the concept of function overloading in C++ with an example.

a) Function overloading in C++ allows b) Function overloading in C++ allows


a function to be called from another a function to be defined with
function, creating a chain of multiple return statements, each
function calls. returning a different type of value.
c) Function overloading in C++ allows d) Function overloading in C++ allows
a function to be defined with multiple functions with the same
multiple names, each representing a name but different parameters to be
different operation. defined. This enables the same
function name to be used for
different operations based on the
parameters passed.
Answer Keys

1. c) Inheritance in C++ 2. d) The types of 3. b) class DerivedClass :


allows a class to inheritance in C++ accessSpecifier
inherit properties are single BaseClass {}
and behaviors from inheritance,
another class. multiple
inheritance,
multilevel
inheritance,
hierarchical
inheritance, and
hybrid inheritance.

4. c) Public inheritance 5. c) C++ supports 6. d) Virtual inheritance


makes the public multiple inheritance in C++ is used to
members of the by allowing a class resolve the
base class to inherit from more ambiguity that
accessible in the than one base arises when a class
derived class, class. is derived from
private inheritance multiple classes
makes the public that have a
and protected common base
members of the class. It ensures
base class private that only one
in the derived class, instance of the
and protected common base
inheritance makes class is inherited by
the public and the derived class.
protected members
of the base class
protected in the
derived class.

7. a) The advantages of 8. b) Potential 9. d) Use the 'final'


using inheritance in drawbacks of using keyword when
C++ include code inheritance in C++ declaring the class.
reusability, include tight
promoting coupling between
polymorphism, and
creating a classes and the
hierarchical class diamond problem.
structure.
10. a) Banking system 11. d) Compile-time 12. d) Function
with different polymorphism and overloading in C++
types of accounts runtime allows multiple
inheriting polymorphism. functions with the
common Compile-time same name but
attributes and polymorphism is different
behaviors from a achieved through parameters to be
base account function defined. This
class. overloading and enables the same
operator function name to
overloading, while be used for
runtime different
polymorphism is operations based
achieved through on the parameters
virtual functions passed.
and function
overriding.

You might also like