MCQs on Abstract Classes and Interfaces | Dart

In Dart, abstract classes and interfaces are key concepts for object-oriented programming. This set of 30 MCQs will help you master defining abstract classes, implementing interfaces, and using abstract methods.


Chapter 3: Abstract Classes and Interfaces in Dart – MCQs

1. Defining Abstract Classes (10 Questions)

  1. Which of the following is the correct syntax for defining an abstract class in Dart?
    a) abstract class MyClass {}
    b) class abstract MyClass {}
    c) abstract MyClass class {}
    d) class MyClass abstract {}
  2. What is the purpose of an abstract class in Dart?
    a) To create a class that cannot be instantiated.
    b) To define methods for an interface.
    c) To implement functionality.
    d) To handle errors.
  3. Can an abstract class have concrete methods in Dart?
    a) No, only abstract methods.
    b) Yes, but only static methods.
    c) Yes, abstract classes can have both concrete and abstract methods.
    d) No, it must be fully abstract.
  4. What happens when you try to instantiate an abstract class directly in Dart?
    a) It throws an error.
    b) It creates an object of the class.
    c) It runs without any issues.
    d) It causes the program to crash.
  5. How is an abstract method defined in Dart?
    a) abstract void myMethod();
    b) void myMethod();
    c) abstract myMethod();
    d) void abstract myMethod();
  6. Can an abstract class be inherited in Dart?
    a) No, abstract classes cannot be inherited.
    b) Yes, an abstract class can be inherited, but the abstract methods must be implemented.
    c) Yes, but only private abstract methods can be inherited.
    d) No, abstract classes cannot inherit any other classes.
  7. Which of the following is a valid use of an abstract class?
    a) It is used to define a class that will never be instantiated but has implementation logic.
    b) It is used to define only constants.
    c) It is used to create objects directly.
    d) It is used to store functions that cannot be defined elsewhere.
  8. Can a class be abstract and implement an interface in Dart?
    a) No, a class can either be abstract or implement an interface, but not both.
    b) Yes, an abstract class can implement an interface.
    c) No, only concrete classes can implement interfaces.
    d) Yes, but only if the class does not contain any abstract methods.
  9. What will the following code output?dartCopy codeabstract class Animal { void makeSound(); } class Dog extends Animal { void makeSound() { print("Bark"); } } var d = Dog(); d.makeSound(); a) Error
    b) Bark
    c) Nothing
    d) Program crash
  10. What type of class cannot have any method implementations?
    a) Regular class
    b) Abstract class
    c) Interface
    d) Concrete class

2. Implementing Interfaces (10 Questions)

  1. In Dart, how do you implement an interface?
    a) class MyClass implements MyInterface {}
    b) interface class MyClass implements MyInterface {}
    c) class MyClass extends MyInterface {}
    d) interface MyClass implements MyInterface {}
  2. Which of the following is correct about implementing an interface in Dart?
    a) The implementing class must override all methods of the interface.
    b) The interface is optional when implementing a class.
    c) An interface cannot contain any methods.
    d) An interface can only be implemented by abstract classes.
  3. In Dart, how can a class implement multiple interfaces?
    a) By using implements keyword multiple times.
    b) By separating interfaces with commas in the implements clause.
    c) Dart does not support multiple interfaces.
    d) By using the extends keyword.
  4. Which of the following statements is true when implementing an interface?
    a) The class must implement all methods of the interface.
    b) The class can leave methods unimplemented.
    c) The class can only implement one interface at a time.
    d) The class must inherit the interface, not implement it.
  5. What happens if a class implements an interface but does not implement all of its methods?
    a) It will compile, but runtime errors may occur.
    b) It causes a compile-time error.
    c) The program crashes.
    d) It works fine but skips the unimplemented methods.
  6. What is the purpose of implementing an interface in Dart?
    a) To define the contract that a class must follow.
    b) To avoid defining any method implementations.
    c) To create multiple classes from one template.
    d) To allow multiple class inheritance.
  7. Can a class implement interfaces and extend other classes at the same time?
    a) No, Dart does not allow this.
    b) Yes, Dart supports both multiple inheritance and implementation.
    c) Yes, but the class must be abstract.
    d) No, interfaces cannot be used with class extensions.
  8. Which of the following is the correct way to implement an interface in Dart?
    a) class Car implements Vehicle {}
    b) interface class Car implements Vehicle {}
    c) class Car extends Vehicle implements Vehicle {}
    d) interface Car implements Vehicle {}
  9. What happens if a class implements an interface but only partially implements it?
    a) The code will compile, but errors will be thrown during runtime.
    b) The code will not compile until all methods are implemented.
    c) The class will automatically inherit all missing methods.
    d) The class will override the interface.
  10. Which of the following allows a class to follow an interface contract in Dart?
    a) Extending an abstract class
    b) Implementing an interface
    c) Creating a concrete class
    d) Declaring an abstract class

3. Abstract Methods (10 Questions)

  1. What is an abstract method in Dart?
    a) A method without implementation that must be implemented in the subclass.
    b) A method that only works with abstract classes.
    c) A method that can never be overridden.
    d) A method with default implementation.
  2. Can an abstract method have a body in Dart?
    a) No, it must not have a body.
    b) Yes, abstract methods can have a body.
    c) Only static abstract methods can have a body.
    d) Yes, but only in concrete classes.
  3. How does a class implement an abstract method?
    a) By declaring the method without a body.
    b) By using the abstract keyword in the method declaration.
    c) By providing a concrete implementation of the method.
    d) By using the void keyword.
  4. What is the behavior of an abstract method in Dart?
    a) It allows partial method implementations.
    b) It has no implementation and must be overridden.
    c) It is automatically called by Dart when required.
    d) It cannot be overridden in any subclass.
  5. Can a method be both abstract and static in Dart?
    a) No, abstract methods cannot be static.
    b) Yes, abstract methods can be static.
    c) Only concrete methods can be static.
    d) Yes, but only in subclasses.
  6. Which of the following defines an abstract method in Dart?
    a) abstract void myMethod();
    b) void abstract myMethod();
    c) void myMethod();
    d) abstract void myMethod();
  7. How do you define an abstract method in an abstract class in Dart?
    a) abstract void myMethod();
    b) void myMethod();
    c) abstract void myMethod() {}
    d) void abstract myMethod();
  8. In Dart, which of the following can an abstract method be used for?
    a) To provide a default method implementation
    b) To define a method contract without implementation
    c) To create static methods in abstract classes
    d) To restrict method overriding
  9. Which of the following is a valid scenario for defining an abstract method in Dart?
    a) In a concrete class
    b) In an abstract class
    c) In an interface
    d) In a final class
  10. What happens when an abstract class implements an abstract method in Dart?
    a) The abstract method must be implemented in the subclass.
    b) The abstract class provides an implementation for the method.
    c) The abstract class cannot implement an abstract method.
    d) The abstract method will cause a runtime error.

Answers

QnoAnswer
1a) abstract class MyClass {}
2a) To create a class that cannot be instantiated.
3c) Yes, abstract classes can have both concrete and abstract methods.
4a) It throws an error.
5b) void myMethod();
6b) Yes, an abstract class can be inherited, but the abstract methods must be implemented.
7a) It is used to define a class that will never be instantiated but has implementation logic.
8b) Yes, an abstract class can implement an interface.
9b) Bark
10b) Abstract class
11a) class MyClass implements MyInterface {}
12a) The implementing class must override all methods of the interface.
13b) By separating interfaces with commas in the implements clause.
14a) The class must implement all methods of the interface.
15b) It causes a compile-time error.
16a) To define the contract that a class must follow.
17b) Yes, Dart supports both multiple inheritance and implementation.
18a) class Car implements Vehicle {}
19b) The code will not compile until all methods are implemented.
20b) Implementing an interface
21a) A method without implementation that must be implemented in the subclass.
22a) No, it must not have a body.
23c) By providing a concrete implementation of the method.
24b) It has no implementation and must be overridden.
25a) No, abstract methods cannot be static.
26a) abstract void myMethod();
27b) void myMethod();
28b) To define a method contract without implementation
29b) In an abstract class
30a) The abstract method must be implemented in the subclass.

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