Which of the following lines of code will work flawlessly when put independently inside the inc() method in order to make the snippet's output equal to 3? (Choose two.)
Explanation:
Step-by-Step Breakdown:
Initialization: When Object = MyClass() is created, the __init__ method increments MyClass.Var to 1 and sets self.prop = 1.
Object.inc(2): The inc() method takes val as an argument, and one of the provided options will be inserted to modify self.prop to 3.
print(Object.get()): Calls Object.get() to return self.prop. To get the output 3, the inc() method must increment self.prop by val (2 in this case).
The correct answers are:
B. self.put(self.prop + val)
self.put() is a method of MyClass that updates self.prop.
self.prop + val calculates 1 + 2 = 3 and assigns it to self.prop.
C. self.put(self.get() + val)
self.get() returns the current value of self.prop, which is 1.
self.get() + val calculates 1 + 2 = 3.
self.put(3) assigns 3 to self.prop.
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.
flthymcnsty
4 months, 3 weeks ago