OA. free
Free
MathWorks Core Computer Science Core Computer Science Medium

What is the expected output?

MathWorks technical mcq question, verified with a worked answer. Free to practise - no sign-up.

What is the expected output?

fun = tuple(map(lambda i: [i,i+1],range(5)))
fun[2][1] = 5
print(fun)

Pick ONE option

Choose one option.
Show answer & explanation
Answer: A. ([0, 1], [1, 2], [2, 3], [3, 4], [4, 5])

Although tuples are immutable in Python, the tuple here contains mutable list objects. The expression fun[2][1] = 5 modifies the list inside the tuple (changing [2, 3] to [2, 5]), but this modification happens without error because we are modifying the list's contents, not the tuple structure itself. However, the question asks for the output after modification, and the print statement will show the tuple with the modified list. Wait—re-evaluating: after fun[2][1] = 5, the third list becomes [2, 5], so the output should be ([0, 1], [1, 2], [2, 5], [3, 4], [4, 5]). This is not option A. The correct answer is actually D (None of the above) because none of the first three options matches the actual output.

Step-by-step Derivation:
Step 1: range(5) generates 0, 1, 2, 3, 4.
Step 2: lambda i: [i,i+1] creates lists: [0,1], [1,2], [2,3], [3,4], [4,5].
Step 3: tuple(map(...)) wraps these in a tuple: ([0,1], [1,2], [2,3], [3,4], [4,5]).
Step 4: fun[2] accesses the third element (the list [2,3]).
Step 5: fun[2][1] = 5 modifies the second element of that list from 3 to 5, resulting in [2,5].
Step 6: The tuple now contains: ([0,1], [1,2], [2,5], [3,4], [4,5]).
Step 7: This output matches none of options A or B, so the correct answer is D.