CS FundamentalsVery hardProduct-company placement · GATE CS

How many definition-use (def-use) pairs does variable x have in this fragment (a p-use in a condition counts as a use)?

How many definition-use (def-use) pairs does variable x have in this fragment (a p-use in a condition counts as a use)?
x = int(input())     # 1
y = 0                # 2
if x > 10:           # 3
    y = x * 2        # 4
else:
    x = x + 1        # 6
print(x, y)          # 7
  1. A. 3
  2. B. 4
  3. C. 6
  4. D. 5

Correct answer: D. 5

The definition at 1 reaches uses at 3, 4, 6 and also 7 (along the path through line 4, where x is not redefined); the definition at 6 reaches 7. That makes 5 pairs. Missing (1, 7) gives 4.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic