📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!


    Question

    Consider the following Python code: from abc

    import ABC, abstractmethod class Shape(ABC):     @abstractmethod     def area(self):         pass class Circle(Shape):     def __init__(self, radius):         self.radius = radius     def area(self):         return 3.14 * self.radius * self.radius # What will be the output of the following? # s = Shape() # print(s.area()) What will happen if you try to execute the commented-out lines `s = Shape()` and `print(s.area())`?
    A It will print "None". Correct Answer Incorrect Answer
    B It will print "0.0". Correct Answer Incorrect Answer
    C It will raise a `TypeError` because `Shape` is an abstract class and cannot be instantiated. Correct Answer Incorrect Answer
    D It will raise an `AttributeError` because `area` is not implemented in `Shape`. Correct Answer Incorrect Answer
    E It will execute successfully and print an undefined value. Correct Answer Incorrect Answer

    Solution

    o In Python, when you define a class that inherits from abc.ABC (Abstract Base Class) and includes at least one method decorated with @abstractmethod, that class becomes an abstract class. o Abstract classes cannot be instantiated directly. Their purpose is to serve as blueprints for other classes. You must create a concrete subclass (like Circle in this example) that provides implementations for all abstract methods before you can create an object of that subclass. o Attempting to instantiate Shape() will result in a TypeError.

    Practice Next
    ask-question