B02B – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Exam Questions-CBSE12A-2018-B02B #PYTHON#6116    siteicon   siteicon  

Problem Statement - B02B

class Vehicle:                        #Line 1
    Type = 'Car'                      #Line 2
    def __init__(self, name):         #Line 3
        self.Name = name              #Line 4
    def Show(self):                   #Line 5
        print self.Name,Vehicle.Type  #Line 6

V1=Vehicle("BMW")                     #Line 7
V1.Show()                             #Line 8
Vehicle.Type="Bus"                    #Line 9
V2=Vehicle("VOLVO")                   #Line 10
V2.Show()                             #Line 11

(i) What is the difference between the variable in Line 2 and Line 4 in the above Python code?

(ii) Write the output of the above Python code.

Solution

TC++ #6116

(i) The variable in Line 2 is a class attribute. This belongs to the class itself. These attributes will be shared by all the instances. The variable in Line 4 is an instance attribute. Each instance creates a
separate copy of these variables.

(ii) BMW Car
VOLVO Bus


Share

CSKC| Created: 9-Jan-2019 | Updated: 9-Jan-2019|CBSE12A-2018









Back