PCAP Exam QuestionsBrowse all questions from this exam

PCAP Exam - Question 98


What is the expected behavior of the following code?

Show Answer
Correct Answer: A

A

Discussion

6 comments
Sign in to comment
Noarmy315Option: A
Jan 1, 2022

print(o_2.Variable, o_1.value, o_1.Variable, o_2.value) #0 0 1 1

sudhanshu1
Dec 20, 2022

i don't understand , variable is a class variable...how is object2.variable = 0?

Mallie
Jan 2, 2023

'Variable' is a class variable to begin with. But then, obejct_1.Variable is created and assigned a value of 1. Obejct_2.Variable, which is also an instance variable, has had no value assigned to it.

skullomaniaOption: A
Oct 3, 2023

Answer is A. The instruction object_1.Variable += 1 creates a new attribute to object_1 and it does not modify class variable 'Variable': object_1.__dict__ ==> {'value':0, 'variable':1} Since class variable 'Variable' is still 0, object2.variable will be 0, then: object_2.Variable (0) + object_1.value (1) = 0 + 1 = 1

macxszOption: A
May 4, 2022

A. it outputs 0

SaadThayabOption: A
Mar 19, 2023

can anyone please explain how the result = 0 please

cufta05
Jun 25, 2023

run this code class Class: Variable = 0 def __init__(self): self.value = 0 object_1 = Class() object_1.Variable += 1 object_2 = Class() object_2.value += 1 print(object_2.Variable + object_1.value)

moteruky
Mar 13, 2024

AttributeError Traceback (most recent call last) Cell In[15], line 10 8 object_1.Variable += 1 9 object_2 = Class() ---> 10 object_2.value += 1 12 print(object_2.Variable + object_1.value) AttributeError: 'Class' object has no attribute 'value' This is what i am getting

brandonkim76Option: C
Aug 30, 2023

Isn't it 0 (object_2.Variable) + 1 (oject_1.value) = 1?