Programming in Python · Programming, Data Structures and Algorithms
10 questions · asked in 3 of 3 years (2024–2026)
MCQ 7 · MSQ 1 · NAT 21-mark 2 · 2-mark 8
2024Questions per year2026
Questions 1–5 of 10
Consider the given Python program.
def append_to_lst(val, lst=[]):
lst.append(val)
return lst
print(append_to_lst(1))
print(append_to_lst(2))
print(append_to_lst(3, [])) Which of the following is the correct output of this program?Answer: (B)
Python has a known trap with a mutable default argument. A default value (like ) is created once, when the function is defined. It is reused in every call that does not pass its own argument for it, so it is not reset to a fresh empty list on each call.
- : no list is passed, so the call uses (and changes) the shared default list. It becomes and is printed as .
- : again no list is passed, so it reuses the same shared default list, which still holds from before, and then appends . It becomes and is printed as .
- : this call passes a brand-new, separate empty list, so the shared default does not affect it. It becomes and is printed as .
Answer: [1], [1,2], [3]
- : no list is passed, so the call uses (and changes) the shared default list. It becomes and is printed as .
- : again no list is passed, so it reuses the same shared default list, which still holds from before, and then appends . It becomes and is printed as .
- : this call passes a brand-new, separate empty list, so the shared default does not affect it. It becomes and is printed as .
Answer: [1], [1,2], [3]
A recursive function in Python is given.
Assume that a typical runtime stack is used to manage function calls. Each function call is pushed onto the stack and removed only after it finishes execution.
Which of the following options denotes the total number of function calls (i.e., the total number of stack activations), including the initial call, to compute mystery
def mystery(n):
if n <= 0:
return 1
else:
return mystery(n-1) + mystery(n-2) Now, consider the following function call: mystery(4) Assume that a typical runtime stack is used to manage function calls. Each function call is pushed onto the stack and removed only after it finishes execution.
Which of the following options denotes the total number of function calls (i.e., the total number of stack activations), including the initial call, to compute mystery
Answer: (C)
Let stand for the total number of calls (the first call included) made to evaluate . Every call that is not a base case makes two more calls, and it also counts as one call itself:
when , and when (base case: only the call itself, with no more recursion).
Answer: 15
when , and when (base case: only the call itself, with no more recursion).
Answer: 15
Consider the given Python program.
def outer():
x = []
def inner(val):
x.append(val)
return x
return inner
f1 = outer()
f2 = outer()
print(f1(10)) # Line P
print(f1(20)) # Line Q
print(f2(30)) # Line R
print(f1(40)) # Line S Which of the following options is/are correct?One or more options are correct.
Answer: (B), (D)
A Python closure keeps a reference to the variable in the enclosing scope. Here the statement runs again each time is called. (A mutable default argument is different, because it is made only once when the function is defined.) So and each get their own separate, independent list. They do not share the same .
- Line P: adds to the list that belongs to and prints .
- Line Q: adds to the same list that used before, and prints [10, 20].
- Line R: adds to the separate list of , which was empty until now. It prints and not , because the list of is independent of the list of .
- Line S: adds again to the list of , which is now , and prints [10, 20, 40].
Answer: Output of Line Q is [10,20]; and output of Line S is [10,20,40].
- Line P: adds to the list that belongs to and prints .
- Line Q: adds to the same list that used before, and prints [10, 20].
- Line R: adds to the separate list of , which was empty until now. It prints and not , because the list of is independent of the list of .
- Line S: adds again to the list of , which is now , and prints [10, 20, 40].
Answer: Output of Line Q is [10,20]; and output of Line S is [10,20,40].
Consider the given Python program.
def fun(L, i=0):
if i >= len(L)-1:
return 0
if L[i] > L[i+1]:
L[i+1], L[i] = L[i], L[i+1]
return 1+fun(L, i+1)
else:
return fun(L, i+1)
data = [5, 3, 4, 1, 2]
count = 0
for _ in range(len(data)):
count += fun(data)
print(count) The output of the program is ________. (Answer in integer )Answer: 8
The call looks at adjacent elements . If they are out of order, it swaps them and adds to the count. Then it calls itself with . This is exactly one bubble-sort pass, and it counts the swaps made in that pass. The outer loop runs a fresh pass times, since . It adds the swap count each time.
:
Pass 1: swap, swap, swap, swap , swaps.
Pass 2: no swap, swap, swap, no swap , swaps.
Pass 3: swap, swap, no swap, no swap , swaps.
Passes 4, 5: the array is already sorted, so each pass has swaps.
Total:
Answer: 8
:
Pass 1: swap, swap, swap, swap , swaps.
Pass 2: no swap, swap, swap, no swap , swaps.
Pass 3: swap, swap, no swap, no swap , swaps.
Passes 4, 5: the array is already sorted, so each pass has swaps.
Total:
Answer: 8
Consider the following Python declarations of two lists:
Which one of the following statements results in ?
Which one of the following statements results in ?
Answer: (A)
puts every element of one by one at the end of . The result is exactly .
works differently. It puts the whole list in as one nested element. The result is , which is wrong.
A Python list has no method (that belongs to dictionaries and sets), so raises an
Answer: (A)
works differently. It puts the whole list in as one nested element. The result is , which is wrong.
A Python list has no method (that belongs to dictionaries and sets), so raises an
AttributeError. The method needs two arguments, an index and an element, so raises a TypeError.Answer: (A)
Keep practising