1. Have a look at the following example. Which of these statements is correct?
class Person:
TITLES = ('Dr', 'Mr', 'Mrs', 'Ms')
def __init__(self, title, name, surname):
if title not in self.TITLES:
raise ValueError("%s is not a valid title." % title)
self.title = title
self.name = name
self.surname = surname VIDEO
The class attribute is TITLES
The class attribute is 'Person'
The class attributes are self.title and %title
The class attributes are: Dr, Mr, Mrs, Ms as well as title, name, surname
2. If we have a method which takes no arguments, then we still have to have one argument – the self. See fun() in the below example. Which statement is true.
3. A function defined in a class is called a _______________
4. Methods have access to all the data contained on the instance of the object; they can access and modify anything previously set on self
5. Instance methods use self. They require an instance of the class in order to be used.
6. Class attributes are attributes that are set at the class-level, as opposed to the instance-level. Normal attributes are introduced in the __init__ method, but some attributes of a class …………………
7. There is a class of methods called static methods, that don't have access to self. Just like class attributes, they are methods that work without requiring an instance to be present.
8. In this example, we have defined the __init__ method to _________________________
9. A ___________________is an action that an object can perform. For example, in VB.Net "Add" is a___________ of the ComboBox object that adds a new entry to a combo box.
10. A method created in a Class is nothing more that a Function or a Sub
11. In Python, instance variables are variables whose value is assigned inside a constructor or method with self. Class variables are variables whose ___________________
12. In the following code, which line is referring to the instance variable?
13. In the following code, which line is referring to to the class variable?
14. What is the output from the following code?
15. What is the following code doing?
16. x = MyClass() << In this case, the instantiation operation (“calling” a class object) creates an empty object.
17. The method in the following code is ______________________ and the output (by adding to the list defined in the class) is:
18. Can you predict the output of the following code?
class Robot:
def __init__(self, name=None):
self.name = name
def say_hi(self):
if self.name:
print("Hi, I am " + self.name)
else:
print("Hi, I am a robot without a name")
x = Robot()
x.say_hi()
y = Robot("Mrs Marvin")
y.say_hi()
Hi, I am a robot without a name, Hi, I am Mrs Marvin
Error - it is not possible to create a class using x and y
Hi, I am Mrs Marvin
x,y,x,y
19. In the following code, the methods are x and y, and the object is say_hi
20. Which of the following are methods?
21. Every object in Python has attributes. You can get a list of those attributes using the built-in “dir” function. For example:
22. In this example, 'name' is an attribute.
23. In this example,we have used an attribute to store a Class-wide, Class-specific constant which is….
24. No attributes have been defined in the following program.
25. What happens if both instance attribute and class attribute is defined?