Welcome to ExamTopics
ExamTopics Logo
- Expert Verified, Online, Free.
exam questions

Exam PCAP All Questions

View all questions & answers for the PCAP exam

Exam PCAP topic 1 question 98 discussion

Actual exam question from Python Institute's PCAP
Question #: 98
Topic #: 1
[All PCAP Questions]

What is the expected behavior of the following code?

  • A. it outputs 0
  • B. it raises an exception
  • C. it outputs 1
  • D. it outputs 2
Show Suggested Answer Hide Answer
Suggested Answer: A 🗳️

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
skullomania
1 year, 1 month ago
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
upvoted 2 times
...
brandonkim76
1 year, 3 months ago
Isn't it 0 (object_2.Variable) + 1 (oject_1.value) = 1?
upvoted 1 times
...
SaadThayab
1 year, 8 months ago
can anyone please explain how the result = 0 please
upvoted 1 times
cufta05
1 year, 5 months ago
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)
upvoted 2 times
moteruky
8 months, 2 weeks ago
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
upvoted 1 times
...
...
...
sudhanshu1
1 year, 11 months ago
i don't understand , variable is a class variable...how is object2.variable = 0?
upvoted 2 times
Mallie
1 year, 11 months ago
'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.
upvoted 1 times
...
...
macxsz
2 years, 6 months ago
Selected Answer: A
A. it outputs 0
upvoted 1 times
...
Noarmy315
2 years, 11 months ago
print(o_2.Variable, o_1.value, o_1.Variable, o_2.value) #0 0 1 1
upvoted 2 times
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...