CBSE Class 11 Computer Science Question 44 of 98

Python Programming Fundamentals — Question 6

Back to all questions
6
Question

Question 6(a)

Find the output of the following code:

x=3
y=x+2
x+=y
print(x,y)
Answer
Output
8 5
Explanation
x = 3
y = x + 2
  • x is initialized to 3.
  • y is set to x + 2, which means y = 3 + 2 = 5.
x += y

The += operator adds the value of y to x and assigns the result back to x.

  • Current values are:
    • x = 3
    • y = 5
  • Calculation: x + y = 3 + 5 = 8
  • Updating x: x += 5 means x = x + 5
  • So, x = 3 + 5 = 8.
print(x, y)

The final values of the variables are:

  • x = 8
  • y = 5

Hence, the output of the program is:

8 5