adplus-dvertising
frame-decoration

Question

What is the output of the following?
def foo(i, x=[]):
    x.append(x.append(i))
    return x
for i in range(3):
    y = foo(i)
print(y)

a.

[[[0]], [[[0]], [1]], [[[0]], [[[0]], [1]], [2]]].

b.

[[0], [[0], 1], [[0], [[0], 1], 2]].

c.

[0, None, 1, None, 2, None].

d.

[[[0]], [[[0]], [1]], [[[0]], [[[0]], [1]], [2]]].

Posted under Python

Answer: (c).[0, None, 1, None, 2, None].

Engage with the Community - Add Your Comment

Confused About the Answer? Ask for Details Here.

Know the Explanation? Add it Here.

Q. What is the output of the following?

Similar Questions

Discover Related MCQs

Q. Which of the following data structures is returned by the functions globals() and locals()?

Q. On assigning a value to a variable inside a function, it automatically becomes a global variable. State whether true or false.

Q. Which is the most appropriate definition for recursion?

Q. Only problems that are recursively defined can be solved using recursion. True or False?

Q. Which of these is not true about recursion?

Q. In _______________ copy, the base address of the objects are copied.
In _______________ copy, the base address of the objects are not copied.

Q. The nested list undergoes shallow copy even when the list as a whole undergoes deep copy. State whether this statement is true or false.

Q. What happens if a local variable exists with the same name as the global variable you want to access?

Q. ______________ returns a dictionary of the module namespace.
________________ returns a dictionary of the current namespace

Q. Which of these is false about recursion?

Q. What is tail recursion?

Q. Which of the following statements is false about recursion?

Q. Recursion and iteration are the same programming approach. True or False?

Q. What happens if the base condition isn’t defined in recursive programs?

Q. Fill in the blanks:
In ____________________ copy, the modification done on one list affects the other list.
In ____________________ copy, the modification done on one list does not affect the other list.

Q. What is the base case in the Merge Sort algorithm when it is solved recursively?