MCQs on Inheritance and Polymorphism | Dart

In Dart, mastering inheritance and polymorphism is crucial for object-oriented programming. This chapter explores extending classes, method overriding, the @override annotation, and the use of the super keyword.


Chapter: Inheritance and Polymorphism in Dart – MCQs

1. Extending Classes

  1. Which keyword is used to extend a class in Dart?
    • a) extend
    • b) inherits
    • c) extends
    • d) super
  2. What happens when a class is extended in Dart?
    • a) The child class inherits the methods and properties of the parent class
    • b) The child class replaces the parent class
    • c) The parent class becomes abstract
    • d) The child class cannot have any properties
  3. What is the primary purpose of extending a class in Dart?
    • a) To create an instance of the parent class
    • b) To allow the child class to inherit the parent’s behaviors and attributes
    • c) To prevent the child class from modifying the parent class
    • d) To make the parent class abstract
  4. How do you call a constructor of the parent class from a subclass in Dart?
    • a) Using super()
    • b) Using parent()
    • c) Using call()
    • d) Using extends()
  5. Which of the following code is used to extend the class Vehicle in Dart?
    • a) class Car implements Vehicle {}
    • b) class Car extends Vehicle {}
    • c) class Car inherits Vehicle {}
    • d) class Car create Vehicle {}
  6. Can a class in Dart extend multiple classes?
    • a) Yes, Dart supports multiple inheritance
    • b) No, Dart does not support multiple inheritance
    • c) Only interfaces can be extended multiple times
    • d) Yes, but only with abstract classes
  7. What happens if a child class doesn’t call the constructor of the parent class?
    • a) The program will throw an error
    • b) The parent’s constructor is automatically invoked
    • c) The child class will not be able to access the parent class’s methods
    • d) The child class must explicitly call the constructor

2. Method Overriding

  1. What is method overriding in Dart?
    • a) Defining a new method in the child class with the same name as the parent
    • b) Calling a parent class method in the child class
    • c) Changing the type of method in the child class
    • d) Renaming a method in the parent class
  2. How can you override a method in Dart?
    • a) By defining a method with the same name in the child class
    • b) By using the super keyword
    • c) By using the @override annotation
    • d) By changing the return type of the parent method
  3. Which of the following is true about method overriding in Dart?
    • a) It can only be done with abstract methods
    • b) It changes the implementation of a method in the parent class
    • c) It is used to provide a new implementation for a method in a subclass
    • d) It calls the parent method automatically
  4. Can method overriding be done with static methods in Dart?
    • a) Yes, static methods can be overridden
    • b) No, static methods cannot be overridden
    • c) Static methods in the child class hide the static methods in the parent class
    • d) Static methods in the child class must be declared abstract
  5. Which of the following is an example of method overriding in Dart?
    • a) class Car { void start() {} }
    • b) class Car extends Vehicle { @override void start() {} }
    • c) class Car implements Vehicle { void start() {} }
    • d) class Car { void start() { super.start(); } }
  6. What happens if you do not use the @override annotation in Dart when overriding a method?
    • a) Dart will throw an error
    • b) Dart will not allow method overriding
    • c) The method will still be overridden but without verification
    • d) The parent class method will be called automatically

3. The @override Annotation

  1. What is the purpose of the @override annotation in Dart?
    • a) To indicate that a method is being overridden from the parent class
    • b) To change the method’s return type
    • c) To prevent inheritance
    • d) To make a method static
  2. Which of the following is true about the @override annotation in Dart?
    • a) It is mandatory for method overriding
    • b) It is not required, but it improves code readability and ensures correctness
    • c) It makes the method abstract
    • d) It is used to call a parent method
  3. How does Dart treat method overriding without using the @override annotation?
    • a) It will produce an error
    • b) It will compile and run normally
    • c) It will cause a performance issue
    • d) It will result in the method being ignored
  4. In which situation should you use the @override annotation?
    • a) To indicate that a class is overriding a method
    • b) To change the return type of a method
    • c) To prevent a method from being inherited
    • d) To provide a new implementation for a method inherited from the parent class
  5. What happens if you override a method without using the @override annotation in Dart?
    • a) The method is not overridden
    • b) The Dart compiler will not verify the override
    • c) It will cause an error
    • d) It will throw a runtime exception
  6. Can the @override annotation be used for variables in Dart?
    • a) Yes, to override a variable in the child class
    • b) No, @override is only for methods
    • c) Yes, it can be used for both methods and variables
    • d) Yes, but only for final variables

4. The super Keyword

  1. What is the super keyword used for in Dart?
    • a) To access methods and properties from the parent class
    • b) To invoke the parent class’s constructor
    • c) To define static variables
    • d) Both a and b
  2. How do you use the super keyword in Dart to access the parent class constructor?
    • a) super();
    • b) super.create();
    • c) parent.constructor();
    • d) super.new();
  3. What happens if you call super() in the constructor of a subclass?
    • a) It invokes the constructor of the parent class
    • b) It initializes a variable in the parent class
    • c) It overrides a method from the parent class
    • d) It is ignored
  4. How do you call a method from the parent class in Dart?
    • a) super.methodName();
    • b) super.methodName;
    • c) call.superMethod();
    • d) parent.methodName();
  5. Which of the following statements is true regarding the use of super in Dart?
    • a) You can call a method of the parent class using super
    • b) super is used to call a method from the child class
    • c) super can be used to access the variables in the child class
    • d) super prevents the inheritance of parent methods
  6. Can super be used to refer to an overridden method in Dart?
    • a) Yes, it allows calling the parent’s method
    • b) No, only the overridden method is executed
    • c) It will result in an error
    • d) It calls the subclass method instead
  7. How does super relate to method chaining in Dart?
    • a) It enables calling a method from the superclass while chaining methods
    • b) It only works for constructors
    • c) It breaks the method chaining process
    • d) It cannot be used for method chaining
  8. What will happen if super is used without calling a constructor in Dart?
    • a) It will result in an error
    • b) It will call the default constructor
    • c) It will invoke a method from the parent class
    • d) It will skip initialization
  9. Can the super keyword be used inside static methods in Dart?
    • a) Yes, to access static methods in the parent class
    • b) No, super cannot be used in static methods
    • c) Yes, but only to access instance methods
    • d) Yes, but only to call constructors
  10. Can you access private members of the parent class using super in Dart?
    • a) Yes, if the parent class is in the same library
    • b) No, private members are inaccessible via super
    • c) Yes, always
    • d) Yes, if the subclass is part of the same package
  11. In which scenario would you need to use the super keyword?
    • a) When invoking methods or constructors of the parent class
    • b) When overriding methods in the child class
    • c) When creating instances of the parent class
    • d) When accessing variables in the child class

Answers

QnoAnswer
1c) extends
2a) The child class inherits the methods and properties of the parent class
3b) To allow the child class to inherit the parent’s behaviors and attributes
4a) Using super()
5b) class Car extends Vehicle {}
6b) No, Dart does not support multiple inheritance
7b) The parent’s constructor is automatically invoked
8a) Defining a new method in the child class with the same name as the parent
9a) By defining a method with the same name in the child class
10c) It is used to provide a new implementation for a method in a subclass
11b) No, static methods cannot be overridden
12b) class Car extends Vehicle { @override void start() {} }
13c) The method will still be overridden but without verification
14a) To indicate that a method is being overridden from the parent class
15b) It is not required, but it improves code readability and ensures correctness
16b) It will compile and run normally
17d) To provide a new implementation for a method inherited from the parent class
18b) Dart compiler will not verify the override
19b) No, @override is only for methods
20d) Both a and b
21a) super();
22a) It invokes the constructor of the parent class
23a) super.methodName();
24a) You can call a method of the parent class using super
25a) Yes, it allows calling the parent’s method
26a) It enables calling a method from the superclass while chaining methods
27a) It will result in an error
28b) No, super cannot be used in static methods
29b) No, private members are inaccessible via super
30a) When invoking methods or constructors of the parent class

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top