Abstraction in Python MCQs Exercise II


Ques 1 Abstraction


Which keyword is used to define an abstract class in Python?
  a) abstract
  b) virtual
  c) abstractclass
  d) ABCMeta


d is the correct option





Ques 2 Abstraction


How do you mark a method as abstract in an abstract class?
  a) By using the @abstractmethod decorator
  b) By prefixing the method name with "abstract"
  c) By using the keyword "abstract"
  d) It's automatically abstract in an abstract class


a is the correct option





Ques 3 Abstraction


Which decorator is used to define an abstract method in an abstract class?
  a) @abstractmethod
  b) @abstract
  c) @virtualmethod
  d) @absmethod


a is the correct option





Ques 4 Abstraction


What is the output of the following python Code?

from abc import ABC, abstractmethod
class Animal(ABC):
    @abstractmethod
    def speak(self):
        pass
class Dog(Animal):
    def speak(self):
        return "Woof!"
class Cat(Animal):
    def speak(self):
        return "Meow!"
pets = [Dog(), Cat()]
for pet in pets:
    print(pet.speak())

  a) Woof! Meow!
  b) Meow! Woof!
  c) Error: Cannot instantiate abstract class
  d) Error: Abstract method not overridden


a is the correct option





Ques 5 Abstraction


What is the output of the following python Code?

from abc import ABC, abstractmethod
class Base(ABC):
    @abstractmethod
    def foo(self):
        pass
class Derived(Base):
    def foo(self):
        return "Derived"
obj = Derived()
print(obj.foo())

  a) Derived
  b) Base
  c) Error: Cannot instantiate abstract class
  d) Error: Abstract method not overridden


a is the correct option