GATE DA 2024 — Question 38

Programming, Data Structures and AlgorithmsProgramming in PythonMCQ2 marks · −2/3 if wrongEasy

Consider the following Python code:
python
1def count(child_dict, i):
2    if i not in child_dict.keys():
3        return 1
4    ans = 1
5    for j in child_dict[i]:
6        ans += count(child_dict, j)
7    return ans
8
9child_dict = dict()
10child_dict[0] = [1,2]
11child_dict[1] = [3,4,5]
12child_dict[2] = [6,7,8]
13print(count(child_dict,0))
14
Which ONE of the following is the output of this code?
  1. A
    6
  2. B
    1
  3. C
    8
  4. D
    9
Practise the full 2024 paper
Challenge a friend: WhatsApp Telegram
Recommended book · affiliate linkIntroduction to Algorithms (CLRS)The standard reference behind most GATE algorithm questions.
Practise more on Programming in PythonTimed, in the real exam interface · free to start

A GATE question every morning

One real past-paper question every morning for your branch, answer hidden until you tap. Free on Telegram.

Join
Stuck? Ask in the student group