142k views
1 vote
I have the following class definition: 'class Truck(Automobile):' and the following instance: 'ford_f150 = Truck()'. Which of the following instructions will return True?

isinstance(ford_f150, Automobile)
type(ford_f150) == 'Truck'
isinstance(ford_f150, Truck)
type(ford_f150) == Truck
isinstance(ford_f150, object)
type(ford_f150) == Automobile

User Dhaumann
by
7.7k points

1 Answer

6 votes

Final answer:

Out of the given instructions, 'isinstance(ford_f150, Automobile)', 'isinstance(ford_f150, Truck)', 'isinstance(ford_f150, object)', and 'type(ford_f150) == Truck' (assuming no quotes around Truck) will return True.

Step-by-step explanation:

The question is about determining the validity of different Python statements that check the type of an instance ford_f150 that belongs to a class Truck that inherits from Automobile.

The statement isinstance(ford_f150, Automobile) will return True because Truck is a subclass of Automobile, so an instance of Truck is also an instance of Automobile.

The statement type(ford_f150) == 'Truck' is incorrect because type should be compared to the class Truck, not the string 'Truck'.

The statement isinstance(ford_f150, Truck) will return True since ford_f150 is an instance of the Truck class.

The statement type(ford_f150) == Truck is correct without quotes around Truck, so this will return True.

The statement isinstance(ford_f150, object) will also return True because in Python all classes are derived from the base class object.

The statement type(ford_f150) == Automobile is incorrect because type will return Truck, not Automobile.

User Dave Ferguson
by
7.7k points