PCAP 31 03 PCAP Certified Associate Python Programmer · Free Practice Question Easy
Question 12
Q426 - OOP
What is the expected output of the following code?
- class A:
- pass
- class B(A):
- pass
- class C(B):
- pass
- print(issubclass(A, C))
-
A
1 -
B
False -
C
True -
D
The code is erroneous.
Reveal correct answer
Correct answer: B
Explanation
Topics: class issubclass()
Try it yourself:
- class A:
- pass
- class B(A):
- pass
- class C(B):
- pass
- print(issubclass(A, C)) # False
- print(issubclass(C, A)) # True
- class D:
- pass
- print(issubclass(C, (A, D))) # True
Explanation:
This question is a little tricky.
C is a subclass of A but just not the other way around.
The first argument needs to be the class you want to check
and the second parameter the class you want to check it against.
The second argument could also be a tuple of classes.
(There is another question about the same topic Q130.)
Q426 (Please refer to this number, if you want to write me about this question.)
A. The output of the issubclass() function in Python is a boolean value, either True or False. In this code snippet, class A is not a subclass of class C, so the expected output is False, not 1.
B. The issubclass() function in Python is used to check if a class is a subclass of another class. In this code snippet, class A is not a subclass of class C, so the expected output is False.
C. The issubclass() function in Python is used to check if a class is a subclass of another class. In this code snippet, class A is not a subclass of class C, so the expected output is False, not True.
D. The code provided is not erroneous. It defines three classes A, B, and C, and then checks if class A is a subclass of class C using the issubclass() function. The expected output is False, as class A is not a subclass of class C.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
