Question

Consider the following C program:

#include <stdio.h>

int g(int n) {
    return (n+10);
}

int f(int n) {
    return g(n*2);
}

int main() {
    int sum, n;
    sum=0;
    for (n=1; n<3; n++)
        sum += g(f(n));
    printf ("%d", sum);
    return 0;
}

The output of the given C program is ________. (Answer in integer)

Official answer and solution
Official answer
46
From
Official GATE 2025 answer key

Solution

Express g(f(n)) algebraically as 2n+202n + 20 and sum over n=1,2n = 1, 2.

From the function definitions:

  • g(n)=n+10g(n) = n + 10
  • f(n)=g(2n)=2n+10f(n) = g(2n) = 2n + 10
  • g(f(n))=(2n+10)+10=2n+20g(f(n)) = (2n + 10) + 10 = 2n + 20

The for loop executes for n=1n = 1 and n=2n = 2:

  • For n=1n = 1: g(f(1))=2(1)+20=22g(f(1)) = 2(1) + 20 = 22
  • For n=2n = 2: g(f(2))=2(2)+20=24g(f(2)) = 2(2) + 20 = 24

Thus sum =22+24=46= 22 + 24 = 46.

Final answer

46

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2025 Computer Science and Information Technology - Session 2 (CS2)
Question
63 of 65
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.