Exam PCAP All QuestionsBrowse all questions from this exam
Question 137

What is the expected behavior of the following code?

    Correct Answer: C

    C

Discussion
macxszOption: C

C. it outputs 1

seaverickOption: C

#question 137 class Class: Variable = 0 def __init__(self): self.value = 0 obj1 = Class() Class.Variable += 1 obj2 = Class() obj2.value += 1 print(obj2.Variable, obj2.value, Class.Variable, obj1.Variable,obj1.value) print(obj2.Variable + obj1.value) Tested: C

stuartzOption: C

Answer is C. Following shows interactions of class variables vs. instance variables >>> class Class: ... Var = 0 ... def __init__(self): ... self.value = 0 ... >>> obj1 = Class() >>> Class.Var += 2 >>> obj2 = Class() >>> Class.Var += 2 >>> obj1.Var += 5 >>> obj2.value += 1 >>> print(obj2.Var, obj2.value, Class.Var, obj1.Var) 4 1 4 9