Python Programming

GATE DA previous year questions · 6 questions

GATE DA 2025 | Question 23

MCQ · 1 mark · −0.33

Consider the following Python declarations of two lists.

A=[1,2,3]
B=[4,5,6]

Which one of the following statements results in A= \([1, 2, 3, 4, 5, 6]\)?

Options for GATE DA 2025 | Question 23

Explanation

Answer: (A)

A.extend(B) adds each element of B to the end of A in place, so A becomes [1, 2, 3, 4, 5, 6].

  • A.append(B) adds the whole list as a single element, giving [1, 2, 3, [4, 5, 6]].
  • Lists have no update method (dictionaries and sets do), so option C raises AttributeError.
  • insert needs two arguments, an index and a value, so A.insert(B) raises TypeError.

Answer: A

GATE DA 2025 | Question 47

MCQ · 2 marks · −0.66

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"?

Options for GATE DA 2025 | Question 47

Explanation

Answer: (B)

Each tuple assignment evaluates its whole right-hand side with the old sets before assigning anything.

  • Pass 1, first if: A={"this"}, B={"that"}, C={"other"}.
  • Pass 1, second if ("that" is in B): A={"other","this"}, B={"this","that"}, C={"that","other"}.
  • Pass 2: "other" is still in C and "this" is in A, so A={"other"}, B={"this"}, C={"that"}. Now "that" is not in B, so the second if is skipped.

"other" is no longer in C, so the loop stops. Only B contains "this".

Answer: B

GATE DA 2025 | Question 63

NAT · 2 marks

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)

Explanation

Answer: 160

From \(a = 15\), every value of \(a\) reached is odd until 0, so only return 2*f((a-1)/2,b) runs:

\[ f(15, 10) = 2f(7, 10) = 4f(3, 10) = 8f(1, 10) = 16f(0, 10) = 16 \times 10 = 160 \]

In Python 3, (a-1)/2 gives floats (7.0, 3.0, 1.0, 0.0), but the tests a%2==1 and a==0 behave just as they would for integers, and the result stays an integer because it is built from b = 10.

In general the function returns \((a + 1) \cdot b\), not \(a \cdot b\), so 150 is the tempting wrong answer.

Answer: 160

GATE DA 2024 | Question 38

MCQ · 2 marks · −0.66

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?

Options for GATE DA 2024 | Question 38

Explanation

Answer: (D)

child_dict describes a tree: node 0 has children 1 and 2, node 1 has children 3, 4, 5, and node 2 has children 6, 7, 8. A node that is not a key (3 to 8) is a leaf and returns 1. Any other node returns 1 for itself plus the counts of its children, so count counts every node in the subtree.

  • count(child_dict, 1) \(= 1 + 3 = 4\), and likewise count(child_dict, 2) \(= 4\).
  • count(child_dict, 0) \(= 1 + 4 + 4 = 9\).

The tree has the nine nodes 0 to 8. Option A (6) counts only the leaves, and option C (8) leaves out the root.

Answer: D

GATE DA 2024 | Question 39

MCQ · 2 marks · −0.66

Consider the function computeS(X) whose pseudocode is given below:

computeS(X)
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 computeS(X) for \(X = [6, 3, 5, 4, 10]\)?

Options for GATE DA 2024 | Question 39

Explanation

Answer: (C)

\(S[i]\) is the length of the non-decreasing run that ends at position \(i\): it restarts at 1 and adds \(S[i-1]\) only when \(X[i-1] \le X[i]\). With \(X = [6, 3, 5, 4, 10]\), indexed from 1:

  • \(S[1] = 1\).
  • \(i = 2\): \(6 \le 3\) is false, so \(S[2] = 1\).
  • \(i = 3\): \(3 \le 5\), so \(S[3] = 1 + S[2] = 2\).
  • \(i = 4\): \(5 \le 4\) is false, so \(S[4] = 1\).
  • \(i = 5\): \(4 \le 10\), so \(S[5] = 1 + S[4] = 2\).

Options A and B miss the restart at the drop from 5 to 4; option D restarts there but then overstates \(S[5]\).

Answer: C

GATE DA 2024 | Question 41

MCQ · 2 marks · −0.66

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.

Options for GATE DA 2024 | Question 41

Explanation

Answer: (C)

Each call swaps the two outer elements D[s1] and D[s2], then recurses on the next pair inward, s1+1 and s2-1. It stops when the indices meet or cross, so the middle element of an odd-length range stays where it is.

For example, with D = [1, 2, 3, 4, 5], fun(D, 0, 4) swaps 1 and 5, then 2 and 4, giving [5, 4, 3, 2, 1]: the segment from s1 to s2 is reversed in place.

Option D describes only the first call, but the recursion keeps swapping the inner pairs. The function never compares values, so it cannot find a minimum (A) or sort (B).

Answer: C