PCPP 32 10x PCPP Certified Professional Python Programmer 1 · Free Practice Question Medium
Question 18
What is the result of the following code?
- from abc import ABC, abstractmethod
- class Figure(ABC):
- @abstractmethod
- def area(self):
- pass
- @abstractmethod
- def perimeter(self):
- pass
- class Square(Figure):
- def __init__(self, a=1):
- self.a = a
- def area(self):
- return self.a * self.a
- def perimeter(self):
- return 4 * self.a
- square = Square(10)
- print(square.area())
- print(square.perimeter())
-
A
This code is erroneous. It will raise the
TypeErrorexception. -
B
This code is erroneous. It will raise the
AttributeErrorexception. -
C
- 100
- 40
-
D
- 1
- 4
Reveal correct answer
Correct answer: C
A.
This would happen if the Square class didn't implement the abstract methods area() and perimeter() from the Figure class. However, since Square does implement both methods, no TypeError is raised. This answer is incorrect.
B.
An AttributeError would be raised if we tried to access an attribute or method that does not exist. Since all required methods and attributes are correctly defined, this error does not occur. This answer is incorrect.
C.
This is the correct answer. The area() method returns 100, and the perimeter() method returns 40.
D.
This would be the output if the Square instance was created with the default value of a = 1. However, since Square(10) is used, this answer is incorrect.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
