CS FundamentalsHardProduct-company placement · Python
What value does this Command pattern code print?
What value does this Command pattern code print?
v = [0]
class Add:
def __init__(s, n): s.n = n
def do(s): v[0] += s.n
def undo(s): v[0] -= s.n
undo, redo = [], []
def run(c): c.do(); undo.append(c); redo.clear()
def u():
c = undo.pop(); c.undo(); redo.append(c)
def r():
if redo:
c = redo.pop(); c.do(); undo.append(c)
run(Add(5)); run(Add(3)); u(); run(Add(10)); r(); u(); r()
print(v[0])- A. 18
- B. 15
- C. 5
- D. 8
Correct answer: B. 15
5, then 8; undo gives 5 with redo = [Add(3)]; running Add(10) gives 15 and clears redo, so the next r() does nothing; undo removes 10 (5) and redo puts it back (15). Expecting the cleared Add(3) to come back gives 18.
Practise more Software Engineering, Git and Cloud Basics questionsSoftware Engineering, Git and Cloud Basics: notes and key points
More Software Engineering, Git and Cloud Basics questions
- Counting each if, while and conditional expression as one decision (V(G) = decisions + 1), what is the cyclomatic complexity of g, and…
- Module ownership: A is known by Ravi and Sita, B by Sita and Aman, C by Aman and Ravi, D by Ravi and Joe. The bus factor is the fewest…
- Intermediate COCOMO for an organic project uses Effort = 3.2 × KLOC^1.05 × EAF person-months. A 30 KLOC system has two non-nominal cost…
- Counting one decision for each loop, if, else-if, && or || operator, case label (not default) and catch clause, what is the cyclomatic…
- For the decision (A or B) and C, which test set (written as A, B, C) achieves MC/DC with only four tests?
- A leap-year function follows the Gregorian rule (divisible by 4, except centuries, unless divisible by 400). Which test set has one year…