Preview

02 - Classes and Objects

 1. In reference to the following image, which of these statements is true?
OOPcar.png

  The car here is inside the class, so is an object

  The car is an object, as it represents a specific car

  The car in this image is the class itself. The 'car' class has attributes and methods.

  The car in this case is just a procedure as it has only two 'objects'

 2. In the following image, the red Toyota is ….
OOP_carobjects.jpg

  an example of an attribute

  an example of a class

  an example of an instance of a class (i.e an object)

  an example of the attributes and templates of the red class

 3. Blue' in this example is a …..
OOP_carobjects.jpg

  attribute

  function

  method

  object class

 4. The values (attributes) assigned by the constructor to the class members is used to create an ……………...
Note: Not all languages call this the constructor, but it is useful to know the term. 

  attribute list

  object

  object orientated approach

  class template

 5. Here we have a class and an object both being used. What is the output of this code?
class test:
     def __init__(self,a="Hello World"):
         self.a=a
 
     def display(self):
         print(self.a)
obj=test()
obj.display()

  Hello World” is displayed.

  A run time error occurs

  A logic error occurs as 'display' is not a valid attribute

  Error - there is nothing in the display method

 6. setattr(obj,name,value) could be used to set an attribute. If attribute doesn’t exist, …….

  then it would be incorporated and embedded into the object first

  None of the above

  then it would be created

  then it would be deleted

 7. What is getattr() used for?

  to check if an attribute exists or not

  To access the attribute of the object

  to delete the attribute of an object

  None of the above

 8. What is the output of the following code?
 class test:
     def __init__(self,a):
         self.a=a
 
     def display(self):
         print(self.a)
obj=test()
obj.display()

  Runs normally, doesn’t display anything

  Error as one argument is required to create the object

  The letter 'x' is printed.

  Displays 0, which is the automatic default value

 9. In the following code, what is the class, what is the object, and what is happening on the last line?
>>> class A:
	def __init__(self,b):
		self.b=b
	def display(self):
		print(self.b)
>>> obj=A("Hello")
>>> del obj

  The class is 'A', the object is 'obj', and obj is being deleted

  None of the above

  There is no class or object defined here

  The class is 'A', the object is 'b' and the class is being deleted on the last line

 10. ……………………..is the OOP word for creating an instance of a class.

  instant-creation

  abstraction

   Instantiation

  encapsulation

 11. Which piece of code creates an empty class?
a)

class A:
    return
b)

class A:
    pass
c)

class A:

  c

  a

  d

  b

 12. Analyse the following code. Which option correctly creates an object called student1?
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")

  Student("Ruth Marvin",21)

  student1=Student("Ruth Marvin",21)

  student1=Student("Ruth Marvin",21,self,0)

  All of the above are valid

 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!

  Jonathan, 21, Target not achieved

  Error message - as there is no method to print the target

 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")

  Planet' is the class, and there are two objects (planet1 and planet2)

  There are two classes (planet1 and planet2) and one template object 'Planet'

  There are three classes (planet, planet1 and planet2)

  None of the above

 15. The following code is in VB.Net. Can you follow the logic to predict the output?
Module mybox
   Class Box
      Public length As Double   ' Length of a box
      Public breadth As Double  ' Breadth of a box
      Public height As Double   ' Height of a box
   End Class
   Sub Main()
      Dim Box1 As Box = New Box()        ' Declare Box1 of type Box
      Dim Box2 As Box = New Box()        ' Declare Box2 of type Box
      Dim volume As Double = 0.0         ' Store the volume of a box here
      
      ' box 1 specification
      Box1.height = 5.0
      Box1.length = 6.0
      Box1.breadth = 7.0
      
      ' box 2 specification
      Box2.height = 10.0
      Box2.length = 12.0	
      Box2.breadth = 13.0
      
      'volume of box 1
      volume = Box1.height * Box1.length * Box1.breadth
      Console.WriteLine("Volume of Box1 : {0}", volume)
      
      'volume of box 2
      volume = Box2.height * Box2.length * Box2.breadth
      Console.WriteLine("Volume of Box2 : {0}", volume)
      Console.ReadKey()
   End Sub
End Module

  None of the above

  Volume of Box 1: 10

  Error

  Volume of Box1 : 210 and Volume of Box2 : 1560