GATE CS 2023 (Forenoon) — Question 47

Programming in CFunctions, Parameter Passing, Scope Rules, RecursionMCQ2 marks · −2/3 if wrongMedium

Consider the C function foo and the binary tree shown:
c
1typedef struct node {
2    int val;
3    struct node *left, *right;
4} node;
5int foo(node *p) {
6    int retval;
7    if (p == NULL)
8        return 0;
9    else {
10        retval = p->val + foo(p->left) + foo(p->right);
11        printf("%d ", retval);
12        return retval;
13    }
14}
15
Question image
When foo is called with a pointer to the root node of the given binary tree, what will it print?
  1. A
    3 8 5 13 11 10
  2. B
    3 5 8 10 11 13
  3. C
    3 8 16 13 24 50
  4. D
    3 16 8 50 24 13
Practise the full 2023-FN paper
Challenge a friend: WhatsApp Telegram
From RENYXERAPractise this subject in the exam simulatorTimed, GATE-style tests with analytics that find your weak topics.
Practise more on Functions, Parameter Passing, Scope Rules, RecursionTimed, 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