Challenge - 5 Problems
Type Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Output of type() with subclass instance
What is the output of this code?
Python
class Animal: pass class Dog(Animal): pass pet = Dog() print(type(pet) == Animal)
Attempts:
2 left
๐ก Hint
type() returns the exact class of the object, not its parent classes.
โ Incorrect
type(pet) returns Dog, which is not equal to Animal, so the expression is False.
โ Predict Output
intermediate2:00remaining
Using isinstance() with multiple classes
What will this code print?
Python
class Vehicle: pass class Car(Vehicle): pass class Bike(Vehicle): pass obj = Car() print(isinstance(obj, (Bike, Vehicle)))
Attempts:
2 left
๐ก Hint
isinstance() returns True if the object is an instance of any class in the tuple.
โ Incorrect
obj is an instance of Car, which is a subclass of Vehicle, so isinstance returns True.
โ Predict Output
advanced2:00remaining
Difference between type() and isinstance()
What is the output of this code?
Python
class A: pass class B(A): pass obj = B() print(type(obj) == A, isinstance(obj, A))
Attempts:
2 left
๐ก Hint
type() checks exact type, isinstance() checks inheritance too.
โ Incorrect
type(obj) is B, so type(obj) == A is False; isinstance(obj, A) is True because B inherits from A.
โ Predict Output
advanced2:00remaining
What error does this code raise?
What error will this code raise when run?
Python
x = 5 print(isinstance(x, (int, str, list))) print(type(x) in (int, str, list)) print(isinstance(x, int, str))
Attempts:
2 left
๐ก Hint
Check the number of arguments passed to isinstance().
โ Incorrect
isinstance() expects exactly two arguments; passing three causes TypeError.
๐ง Conceptual
expert2:00remaining
Why use isinstance() over type()?
Which of the following is the best reason to prefer isinstance() over type() when checking an object's type?
Attempts:
2 left
๐ก Hint
Think about inheritance and flexibility.
โ Incorrect
isinstance() returns True for instances of a class or its subclasses, making it more flexible than type() which checks exact type only.