1. In reference to the following image, which of these statements is true?
2. In the following image, the red Toyota is ….
3. Blue' in this example is a …..
4. The values (attributes) assigned by the constructor to the class members is used to create an ……………...
5. Here we have a class and an object both being used. What is the output of this code?
6. setattr(obj,name,value) could be used to set an attribute. If attribute doesn’t exist, …….
7. What is getattr() used for?
8. What is the output of the following code?
9. In the following code, what is the class, what is the object, and what is happening on the last line?
10. ……………………..is the OOP word for creating an instance of a class.
11. Which piece of code creates an empty class?
12. Analyse the following code. Which option correctly creates an object called student1?
13. In the following code, what will the output be?
class Student:
def __init__(self,name,test_score): #this init method is the first method to be invoked when you create an object
#ensure you initialise all your attributes in the init method
self.name=name
self.test_score=test_score
#=======creating a method to see if the student has met their target
def hasAchievedTarget(self):
#the self is a default parameter that the methods of your class accept, in order to use the attributes of your class
if self.test_score>=90:
print("Target met, Well done!")
else:
print("Target not achieved")
student1=Student("Jonathan",21) #the process of creating an object is called instantiation.
print(student1.name)
print(student1.test_score)
student1.hasAchievedTarget()
Jonathan, 21, Target met, Well done!
Error message - as there is no method to print the target
Jonathan, 21, Target not achieved
14. In the following code, point out the classes and objects.
from turtle import*
class Planet:
#self is how we refer to things in the clas from within itself. .self is the first parameter in any function defined inside a class
#to access functions and variables inside the class, their name must be preceded with self and a full-stop (e.g. self.variable_name)
def __init__(self,x,y,radius):
self.radius=radius #circles need a radius in order to be defined
self.x=x
self.y=y
canvas = Screen()
canvas.setup(800,800)
turtle = Turtle()
def circumference(self):
return 2*3.1415*self.radius
def scaleSize(self,scale):
self.radius=self.radius*scale
def draw(self,colour):
self.turtle.goto(self.x,self.y) #go to (x,y+radius)
self.turtle.color(colour)
self.turtle.dot(self.radius) #dot takes the circle diameter as a parameter (size)
#Specify width and height below e.g. 100,200
planet1=Planet(-200,-100,200)
planet1.draw("red")
planet2=Planet(300,300,100)
planet2.draw("black")
There are three classes (planet, planet1 and planet2)
There are two classes (planet1 and planet2) and one template object 'Planet'
Planet' is the class, and there are two objects (planet1 and planet2)
None of the above
15. The following code is in VB.Net. Can you follow the logic to predict the output?