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 6–10 of 10
Consider the following Python code snippet.
A={"this","that"}
B={"that","other"}
C={"other","this"}
while "other" in C:
if "this" in A:
A,B,C=A-B,B-C,C-A
if "that" in B:
A,B,C=C|A,A|B,B|C
When the above program is executed, at the end, which of the following sets contains "this"? Answer: (B)
Trace the sets one step at a time. At the start: , , .
Iteration 1 (is "other" ? Yes):
Is "this" ? Yes
Is "that" (now )? Yes
Iteration 2 (is "other" ? Yes, ):
Is "this" (now )? Yes
Is "that" (now )? No, so skip.
Check the loop condition: is "other" (now )? No, so the loop ends.
Final state: . Only has "this".
Answer: Only B
Iteration 1 (is "other" ? Yes):
Is "this" ? Yes
Is "that" (now )? Yes
Iteration 2 (is "other" ? Yes, ):
Is "this" (now )? Yes
Is "that" (now )? No, so skip.
Check the loop condition: is "other" (now )? No, so the loop ends.
Final state: . Only has "this".
Answer: Only B
Consider the following Python code snippet.
def f(a,b):
if (a==0):
return b
if (a%2==1):
return 2*f((a-1)/2,b)
return b+f(a-1,b)
print(f(15,10))
The value printed by the code snippet is _________ (Answer in integer)Answer: 160
For : when , the function gives back . When is odd, it gives back . In every other case it gives back . The values are all odd, so the odd rule applies each time:
Answer: 160
Answer: 160
Consider the following Python code:
def count(child_dict, i):
if i not in child_dict.keys():
return 1
ans = 1
for j in child_dict[i]:
ans += count(child_dict, j)
return ans
child_dict = dict()
child_dict[0] = [1,2]
child_dict[1] = [3,4,5]
child_dict[2] = [6,7,8]
print(count(child_dict,0))
Which ONE of the following is the output of this code?Answer: (D)
count(child_dict, i) counts the nodes in the tree below node i, including i itself.If
i is not a key in child_dict, it has no children, so the function returns . Otherwise it starts with ans = 1 for the node itself, then adds count(child_dict, j) for every child j, and returns ans.The dictionary is:
child_dict[0] = [1, 2]child_dict[1] = [3, 4, 5]child_dict[2] = [6, 7, 8]Trace
count(child_dict, 0):Nodes are not keys, so each returns .
count(child_dict, 1): .count(child_dict, 2): .count(child_dict, 0): .The output is .
Answer: (D)
Consider the function whose pseudocode is given below:
S[1] ← 1
for i ← 2 to length(X)
S[i] ← 1
if X[i-1] ≤ X[i]
S[i] ← S[i]+S[i-1]
end if
end for
return S
Which ONE of the following values is returned by the function for ?Answer: (C)
Use 1-based indices as in the pseudocode. . by default, and it becomes when . So is the length of the non-decreasing run that ends at position .
: is false, so .
: is true, so .
: is false, so .
: is true, so .
The result is . Options (A) and (B) wrongly carry the count over at , where 5 > 4. Option (D) gives 5 at the end, but so .
Answer: (C)
: is false, so .
: is true, so .
: is false, so .
: is true, so .
The result is . Options (A) and (B) wrongly carry the count over at , where 5 > 4. Option (D) gives 5 at the end, but so .
Answer: (C)
Consider the following Python function:
def fun(D, s1, s2):
if s1 < s2:
D[s1], D[s2] = D[s2], D[s1]
fun(D, s1+1, s2-1) What does this Python function fun() do? Select the ONE appropriate option below.Answer: (C)
When , each call swaps and , then calls itself on . So the outermost pair is swapped first, then the next pair inward, and so on. The calls stop when the two indices meet (odd length, the middle element stays) or cross (even length).
Example: , . Swap indices 0 and 4, then 1 and 3, then stop at . The result is , so the elements from to are reversed.
If at the first call, the condition fails at once and nothing changes. The question is about the range to , so that case is not the intended one.
The other options are wrong. The function never compares values, so it does not find a minimum (A) or sort (B). It swaps many pairs, not just the one at and , when (D).
Answer: (C)
Example: , . Swap indices 0 and 4, then 1 and 3, then stop at . The result is , so the elements from to are reversed.
If at the first call, the condition fails at once and nothing changes. The question is about the range to , so that case is not the intended one.
The other options are wrong. The function never compares values, so it does not find a minimum (A) or sort (B). It swaps many pairs, not just the one at and , when (D).
Answer: (C)
Keep practising