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
shawn19810609
1 week, 4 days ago
Selected Answer: A
class variables are shared across objects, as long as you change their values at instance level, e,g. object.Variable += 1
upvoted 1 times
...
shawn19810609
2 weeks ago
Selected Answer: C
>>> class Class: ... Var = 0 ... def __init__(self): ... self.value = 0 ... >>> obj_1 = Class() >>> obj_1.Var += 1 >>> obj_2 = Class() >>> obj_2.Var += 1 >>> print(obj_1.Var) 1 >>> print(obj_2.Var) 1 >>> print(obj_2.Var + obj_1.value) 1 >>>
upvoted 1 times
shawn19810609
1 week, 4 days ago
sorry for misleading, line 9 is incorrect, it should be obj_2.value += 1, so obj_1.value and obj_2.Var are both zeros A is correct
upvoted 1 times
...
...
skullomania
1 year, 4 months 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, 5 months ago
Isn't it 0 (object_2.Variable) + 1 (oject_1.value) = 1?
upvoted 1 times
...
SaadThayab
1 year, 10 months ago
can anyone please explain how the result = 0 please
upvoted 1 times
cufta05
1 year, 7 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
10 months, 4 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
2 years, 1 month ago
i don't understand , variable is a class variable...how is object2.variable = 0?
upvoted 2 times
Mallie
2 years, 1 month 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, 9 months ago
Selected Answer: A
A. it outputs 0
upvoted 1 times
...
Noarmy315
3 years, 1 month 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 ...
exam
Someone Bought Contributor Access for:
SY0-701
London, 1 minute ago