Vidyalelo
Python · Q21

MCQs on Python Tuples: Exam

Programming · Python · question 21

Q21

What will be the output of the following code:my_tuple = (1, 2, 3)my_tuple[1] = 4print(my_tuple)

A.
(1, 2, 3)
B.
(4, 2, 3)
C.
(1, 4, 3)
D.
None of these
Answer

Answer: Option D

Solution

Answer: Option D
Solution:
Tuples in Python are immutable, meaning their elements cannot be changed after creation.

In the given code, my_tuple = (1, 2, 3) initializes a tuple with three elements.

The line my_tuple[1] = 4 attempts to modify the second element of the tuple, which is not allowed.

As a result, Python raises a TypeError stating that 'tuple' object does not support item assignment.

Since the code causes an error and does not produce any valid tuple as output, the correct answer is "None of these."