Consider the code below. Which of the following is an example of an instance attribute?

Prepare for the Computer Science (CS) III Exam. Study with multiple choice questions, detailed explanations, and comprehensive resources. Boost your confidence and ace the exam!

An instance attribute is a variable that is bound to the instance of a class and is typically defined within the class's methods, especially the constructor (often called __init__ in Python). Instance attributes hold data that is specific to each object created from the class.

In the options provided, last_name is an example of an instance attribute. When you create a class, you might define a constructor where you establish properties like last_name for each instance of that class, allowing each object (or instance) to maintain its own state. For example, if you have a class called Student, you could define last_name within the constructor so that every student instance has its own last name.

In contrast, print_name could be interpreted as a method, which operates on instance attributes but is itself not an attribute. The student1 option refers to a specific instance of the Student class, and Student refers to the class itself, not its individual attributes. Therefore, last_name is the only option that correctly represents an instance attribute that would exist for each object of a class.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy