Assess your foundational understanding of Python classes, their structure, and how objects relate to them. This quiz covers basic concepts suitable for beginners exploring object-oriented programming.
Which of the following lines correctly defines a class named Person in Python?
Explanation: The correct way to define a class in Python uses the 'class ClassName:' syntax, like 'class Person:'. 'def Person()' is for defining a function, not a class. 'Person class:' and 'define Person:' are not valid Python syntax.
If you have a class named Animal, how do you create an object named cat from it?
Explanation: To create an object from a class, you call the class as if it were a function, such as 'cat = Animal()'. 'Animal = cat()' assigns the result of calling 'cat', which is incorrect. 'cat.create(Animal)' and 'Animal.cat()' do not follow Python syntax for object creation.
What is an object in Python?
Explanation: An object is an instance created from a class, containing its properties and behaviors. It is not a Python keyword, does not only store integers, and is not a function itself, though functions (methods) can belong to classes.
Which of the following is a valid class name in Python?
Explanation: 'Student1' is a valid class name; class names must start with a letter and can contain numbers. '1Student' starts with a digit, 'student-class' contains an invalid character, and 'class' is a reserved keyword.
In Python, where are attributes usually defined when creating a class?
Explanation: Attributes are commonly defined inside methods such as '__init__', which is the class's initializer. Placing them outside any method defines class attributes (less common for unique object data). 'import' and 'return' statements are unrelated.
If you have an object called car with a property color, how do you access its value?
Explanation: The dot notation 'car.color' is used to access an object's property in Python. The '->', '[ ]', and '::' notations are not valid for attribute access in Python.
What is the purpose of the __init__ method in a Python class?
Explanation: '__init__' is the initializer, automatically called when an object is created, to set up its properties. It does not create constants, check types, or delete objects.
Why is the 'self' parameter needed in a class method definition?
Explanation: 'self' refers to the specific object calling the method, allowing access to its attributes. It isn't used in loops, does not declare static methods, and is unrelated to imports.
Which line correctly changes the name attribute of an object p from 'Tom' to 'Bob'?
Explanation: You change an object's attribute using 'p.name = 'Bob''. The other options use incorrect syntax: '->' and '[ ]' aren't used for attributes, and '==' compares values instead of assigning them.
What happens if you create two objects from the same class?
Explanation: Objects instantiated from the same class are independent, so their properties can differ. They do not overwrite each other, do not need to share all properties, and there is no restriction to a single instance.