GateAI
Programming in C

GATE CSE 2017 Set 1 · Q55

NAT2 marksMedium

Time

The output of executing the following C program is _________ .#include <stdio.h> int total(int v) { static int count = 0; while(v) { count += v&1; v >>= 1; } return count; } void main() { static int x = 0; int i = 5; for(; i > 0; i--) { x = x + total(i); } printf("%d ", x); }

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 23 (±0)

Programming in C

The function total(v) counts the number of set bits (1s) in binary representation of v and adds it to the static variable count, which retains its value across function calls.

Let us trace the loop for i=5,4,3,2,1i = 5, 4, 3, 2, 1:

  • i=5i = 5:

    • Binary of 5=10125 = 101_2 (number of set bits = 2)

    • count becomes 0+2=20 + 2 = 2

    • total(5) returns 22

    • x=0+2=2x = 0 + 2 = 2

  • i=4i = 4:

    • Binary of 4=10024 = 100_2 (number of set bits = 1)

    • count becomes 2+1=32 + 1 = 3

    • total(4) returns 33

    • x=2+3=5x = 2 + 3 = 5

  • i=3i = 3:

    • Binary of 3=01123 = 011_2 (number of set bits = 2)

    • count becomes 3+2=53 + 2 = 5

    • total(3) returns 55

    • x=5+5=10x = 5 + 5 = 10

  • i=2i = 2:

    • Binary of 2=01022 = 010_2 (number of set bits = 1)

    • count becomes 5+1=65 + 1 = 6

    • total(2) returns 66

    • x=10+6=16x = 10 + 6 = 16

  • i=1i = 1:

    • Binary of 1=00121 = 001_2 (number of set bits = 1)

    • count becomes 6+1=76 + 1 = 7

    • total(1) returns 77

    • x=16+7=23x = 16 + 7 = 23

After the loop finishes, printf outputs x=23x = 23.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Programming in C previous year question is Q55 of the GATE CSE 2017 Set 1 paper. Find more GATE CSE PYQs on Programming in C in Programming and Data Structures, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Programming in C PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Programming in C in GATE CSE

Programming in C has 74 GATE CSE PYQs across 25 papers from 2011 to 2026. 17 of them are 2-mark NAT questions like this one, and GATE CSE 2017 Set 1 has 4.

How GATE tests Programming in C

More from GATE CSE 2017 Set 1

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2017 Set 1 · Q55 · NAT

The output of executing the following C program is … #include int total(int v) { static int count = 0; while(v) { count += v&1; v >>= 1; } return count; } void main() { static int x = 0; int i = 5; for(; i > 0; i--) { x = x + total(i); } printf("%d ", x); }

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.