What Do You Know About C++ Polymorphism? Trivia Quiz

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Amer248
A
Amer248
Community Contributor
Quizzes Created: 2 | Total Attempts: 3,035
Questions: 25 | Attempts: 569

SettingsSettingsSettings
What Do You Know About C++ Polymorphism? Trivia Quiz - Quiz

What do you know about C++ Polymorphism? C++ Polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function. Take up the quiz below and get to review what you got to understand from the classwork this past week. All the best!


Questions and Answers
  • 1. 

    Polymorphism enables you to:

    • A.

      Program in the general.

    • B.

      Program in the specific.

    • C.

      Absorb attributes and behavior from previous classes.

    • D.

      Hide information from the user.

    Correct Answer
    A. Program in the general.
    Explanation
    Polymorphism enables you to program in the general by allowing you to write code that can work with objects of different classes, as long as they share a common interface or superclass. This means that you can write code that is more flexible and reusable, as it can handle different types of objects without needing to know their specific implementation details.

    Rate this question:

  • 2. 

    Polymorphism specifically enables the creation of programs that handle:

    • A.

      Classes that are containers for other classes

    • B.

      Large amounts of data with efficiency

    • C.

      A wide variety of classes in a general manner

    • D.

      None of the above

    Correct Answer
    C. A wide variety of classes in a general manner
    Explanation
    Polymorphism allows the creation of programs that can work with different classes in a general manner. It enables the use of a single interface to represent multiple classes, allowing objects of different classes to be treated uniformly. This flexibility is particularly useful when dealing with different types of objects that share common behavior or attributes. It simplifies code implementation and maintenance, as it allows for code reuse and promotes modularity. Therefore, the correct answer is that polymorphism enables the handling of a wide variety of classes in a general manner.

    Rate this question:

  • 3. 

    When used correctly, polymorphism will never require changes to be made to any part of the program.

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    False. Parts of a program that need modification are those that require direct knowledge of the particular class that's added to the hierarchy.

    Rate this question:

  • 4. 

    Polymorphism enables objects of different classes that are related by a class hierarchy to be processed generically.

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    Polymorphism allows objects of different classes that are connected through a class hierarchy to be treated uniformly. This means that even though these objects have different types, they can be processed in a generic manner using a common interface or superclass. This flexibility in handling different objects simplifies code and promotes reusability. Therefore, the statement is true.

    Rate this question:

  • 5. 

    Polymorphism allows for specifics to be dealt with during:

    • A.

      Execution

    • B.

      Compilation

    • C.

      Programming

    • D.

      Debugging

    Correct Answer
    A. Execution
    Explanation
    Polymorphism allows for specifics to be dealt with during execution. This means that the behavior of an object can be determined at runtime based on its specific type. During execution, the appropriate method or implementation will be chosen based on the actual type of the object, allowing for flexibility and dynamic behavior. Polymorphism is a key feature in object-oriented programming languages that enables code reusability and flexibility.

    Rate this question:

  • 6. 

    Polymorphism allows the addition of classes providing they were at least considered during program development.

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    False. Polymorphism can be used to include classes that were not even envisioned during program development.

    Rate this question:

  • 7. 

    Polymorphism allows you to command a wide variety of objects even if you do not know the objects' types.

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    Polymorphism is a concept in object-oriented programming that allows objects of different classes to be treated as objects of a common superclass. This means that even if you do not know the specific type of an object, you can still perform operations on it using methods defined in the superclass. This flexibility enables you to write code that can work with a wide variety of objects, making it easier to create reusable and adaptable code. Therefore, the statement that polymorphism allows you to command a wide variety of objects even if you do not know their types is true.

    Rate this question:

  • 8. 

    Which statement best describes the relationship between the base class and derived class types?

    • A.

      A derived class reference cannot be assigned to a base class variable and a base class reference cannot be assigned to a derived class variable.

    • B.

      A derived class reference can be assigned to a base class variable and a base class reference can be assigned to a derived class variable.

    • C.

      A base class reference can be assigned to a derived class variable, but a derived class reference cannot be assigned to a base class variable.

    • D.

      A derived class reference can be assigned to a base class variable, but a base class reference cannot be assigned to a derived class variable.

    Correct Answer
    D. A derived class reference can be assigned to a base class variable, but a base class reference cannot be assigned to a derived class variable.
    Explanation
    A derived class reference can be assigned to a base class variable because a derived class is a specialization of the base class and contains all the properties and methods of the base class. However, a base class reference cannot be assigned to a derived class variable because a base class does not have all the properties and methods of the derived class. The derived class may have additional features that are not present in the base class.

    Rate this question:

  • 9. 

    If a derived class reference is assigned to a base class variable, the variable must be cast back to the derived class before any derived class methods can be called with it.

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    When a derived class reference is assigned to a base class variable, the base class variable only has access to the methods and properties of the base class. In order to call any methods specific to the derived class, the base class variable must be cast back to the derived class. This allows the variable to regain its original derived class type and access the additional methods and properties that are specific to the derived class. Therefore, the statement is true.

    Rate this question:

  • 10. 

    The abstract methods and properties of a class do not provide an implementation.

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    Abstract methods and properties in a class are declared without any implementation. They serve as placeholders for methods and properties that must be implemented in any class that inherits from the abstract class. This allows the abstract class to define a common interface that all its subclasses must adhere to, while leaving the specific implementation details to the subclasses. Therefore, the statement that abstract methods and properties do not provide an implementation is true.

    Rate this question:

  • 11. 

    You may define implementations for abstract methods to act as default implementations.

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    False. Attempting to provide an implementation for an abstract method is a syntax error.

    Rate this question:

  • 12. 

    An abstract base class can be used to declare references.

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    An abstract base class can be used to declare references because it provides a common interface for its derived classes. This allows objects of different derived classes to be referenced using a pointer or reference of the abstract base class type. By declaring a reference to the abstract base class, we can access the common functionality defined in the base class, while still being able to use specific functionality implemented in the derived classes. This helps in achieving polymorphism and code reusability.

    Rate this question:

  • 13. 

    An abstract class may be derived from another abstract class.

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    An abstract class can indeed be derived from another abstract class. This is because abstract classes are designed to be extended and serve as a base for other classes. By deriving one abstract class from another, we can further define and specialize the behavior and attributes of the derived class while still maintaining the abstract nature of the base class. This allows for a hierarchical structure in the design of classes, where each level of abstraction adds more specific functionality.

    Rate this question:

  • 14. 

    If a method needs to be called polymorphically, what type of reference should be used to point to the object that the method is being called with?

    • A.

      A reference of the base class that defines the behavior of the object

    • B.

      A reference of the same type as the object

    • C.

      An object reference to the actual object

    • D.

      None of the above.

    Correct Answer
    A. A reference of the base class that defines the behavior of the object
    Explanation
    When a method needs to be called polymorphically, it means that the method can be overridden by a subclass. In order to achieve this, a reference of the base class that defines the behavior of the object should be used to point to the object. This allows the method to be called based on the actual type of the object at runtime, rather than the type of the reference. By using a reference of the base class, we ensure that the correct version of the method is called, even if the object is of a subclass type.

    Rate this question:

  • 15. 

    Select the best true statement:

    • A.

      Polymorphism is indicated by a runtime warning from the FCL.

    • B.

      Polymorphism is indicated by the use of the keyword virtual in a method signature.

    • C.

      Polymorphism is indicated by the use of the keyword override in a method signature.

    • D.

      Both B and C are correct statements.

    Correct Answer
    D. Both B and C are correct statements.
    Explanation
    Both B and C are correct statements. Polymorphism is indicated by the use of the keyword virtual in a method signature, which allows a derived class to override the implementation of the base class method. Polymorphism is also indicated by the use of the keyword override in a method signature, which explicitly states that the method is intended to override a base class method. These keywords enable the flexibility and extensibility of object-oriented programming, allowing for dynamic method dispatch and the ability to treat objects of different classes as instances of a common base class.

    Rate this question:

  • 16. 

    Which declaration declares abstract method method1 in abstract class Class1 (method1 returns an int and takes no arguments)?

    • A.

      Public int method1();

    • B.

      Public int abstract method1();

    • C.

      Public abstract int method1();

    • D.

      Public int nonfinal method1();

    Correct Answer
    C. Public abstract int method1();
    Explanation
    The correct answer is "public abstract int method1();". This declaration correctly declares an abstract method named method1 in an abstract class named Class1. The method is declared with a return type of int and takes no arguments. The keywords "public" and "abstract" are used to specify the access level and the abstract nature of the method, respectively.

    Rate this question:

  • 17. 

    Consider the abstract class below: public abstract class Foo { private int a; public int b; public Foo( int aVal, int bVal ) { a = aVal; b = bVal; } // end Foo constructor public abstract int calculate(); } // end class Foo Any concrete subclass that extends class Foo:

    • A.

      Must implement a method called calculate.

    • B.

      Will not be able to access the instance variable a.

    • C.

      Will not be able to instantiate an object of class Foo.

    • D.

      All of the above.

    Correct Answer
    D. All of the above.
    Explanation
    The correct answer is "All of the above." Any concrete subclass that extends class Foo must implement a method called calculate. Additionally, the instance variable "a" is private, meaning it cannot be accessed by subclasses. Lastly, since Foo is an abstract class, it cannot be instantiated directly. Therefore, all of the given statements are true.

    Rate this question:

  • 18. 

    Assigning a derived class reference to a base class variable is safe:

    • A.

      Because the derived class has an object of its base class.

    • B.

      Because the derived class is an object of its base class.

    • C.

      Only when the base class is abstract.

    • D.

      Only when the base class is concrete.

    Correct Answer
    B. Because the derived class is an object of its base class.
    Explanation
    Assigning a derived class reference to a base class variable is safe because the derived class is an object of its base class. This means that the derived class inherits all the properties and behaviors of the base class. Therefore, assigning a derived class reference to a base class variable allows us to access the base class's members through the derived class reference, ensuring that no data or functionality is lost in the process.

    Rate this question:

  • 19. 

    Attempting to instantiate an object of an abstract class is a logic error.

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    False. This is a compilation error.

    Rate this question:

  • 20. 

    The keyword sealed is applied to methods and classes to:

    • A.

      Prevent overriding and inheritance

    • B.

      Guarantee an implementation exists

    • C.

      Specify a class is concrete

    • D.

      None of the above.

    Correct Answer
    A. Prevent overriding and inheritance
    Explanation
    The keyword sealed is used to prevent overriding and inheritance in methods and classes. By applying the sealed keyword, we ensure that a method or class cannot be overridden by any derived class, and it cannot be inherited by any other class. This helps in maintaining the integrity and stability of the code, as it prevents unintended modifications or extensions of the sealed elements.

    Rate this question:

  • 21. 

    Classes and methods are declared sealed for all of the following reasons, except:

    • A.

      Sealed methods allow inlining the code.

    • B.

      Sealed methods and classes prevent further inheritance.

    • C.

      Sealed methods are static.

    • D.

      Sealed methods can improve performance.

    Correct Answer
    C. Sealed methods are static.
    Explanation
    Sealed methods are not necessarily static. The purpose of declaring a method as sealed is to prevent further inheritance and overriding of that method in derived classes. Sealed methods can still be instance methods and can be called on objects of the class. On the other hand, static methods belong to the class itself and can be called without creating an instance of the class. Therefore, the statement that sealed methods are static is incorrect.

    Rate this question:

  • 22. 

    Declaring a method sealed means:

    • A.

      It will prepare the object for garbage collection

    • B.

      It cannot be accessed from outside its class

    • C.

      It cannot be overloaded

    • D.

      It cannot be overridden

    Correct Answer
    D. It cannot be overridden
    Explanation
    When a method is declared as sealed, it means that it cannot be overridden by any derived class. This is useful in situations where the base class wants to prevent any modifications or extensions to a particular method implementation. By sealing the method, the base class ensures that it retains control over the behavior of the method and prevents any changes that could potentially break its functionality. This helps in maintaining the integrity and consistency of the code.

    Rate this question:

  • 23. 

    Sealing methods allows the compiler to optimize the program by "inlining code."

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    Sealing methods in programming refers to the practice of marking a class or method as "final" to prevent it from being overridden or extended by other classes. By sealing a method, the compiler can determine at compile-time that the method will not be overridden, allowing it to optimize the program by inlining the code. This means that the compiler can replace the method call with the actual code of the method, eliminating the overhead of a function call. Therefore, the statement "Sealing methods allows the compiler to optimize the program by 'inlining code'" is true.

    Rate this question:

  • 24. 

    A(n) ________ is best used for providing services that bring together objects of otherwise unrelated classes.

    • A.

      Abstract class

    • B.

      Concrete class

    • C.

      Interface

    • D.

      None of the above.

    Correct Answer
    C. Interface
    Explanation
    An interface is best used for providing services that bring together objects of otherwise unrelated classes. This is because an interface allows classes to define a set of methods that they must implement, regardless of their class hierarchy. By implementing the same interface, objects of different classes can be treated uniformly and interact with each other through the common interface methods. This promotes loose coupling and flexibility in the design, allowing for greater reusability and extensibility of code.

    Rate this question:

  • 25. 

    The purpose of an interface is to:

    • A.

      Provide similar objects with the same functionality, even though each will implement the functionality differently

    • B.

      Provide different types of objects with the comparable functionality, even though each will implement the functionality differently

    • C.

      Provide default implementations of methods and properties

    • D.

      None of the above.

    Correct Answer
    B. Provide different types of objects with the comparable functionality, even though each will implement the functionality differently
    Explanation
    An interface in programming is a way to define a contract or a set of rules that a class must follow. It allows different types of objects to have the same functionality, even though each object may implement that functionality differently. This means that objects of different classes can be treated interchangeably as long as they adhere to the interface. Therefore, the purpose of an interface is to provide different types of objects with comparable functionality, regardless of how they implement it.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Mar 21, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Feb 09, 2016
    Quiz Created by
    Amer248
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.