GateAI
Programming in C

GATE CSE 2026 Set 1 · Q39

MCQ2 marksEasy

Time

Consider the following code snippet in C language that computes the number of nodes in a non-empty singly linked list pointed to by the pointer variable head.struct node{ int elt; struct node *next; }; int getListSize (struct node *head) { if( E1 ) return 1; return E2; }Which one of the following options gives the correct replacements for the expressions E1 and E2?

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: B) E1: head->next == NULL E2: 1 + getListSize(head->next)

Programming in C

We are given that the singly linked list is non-empty (n≥1n \ge 1).

  1. Base Case (E1): If the list has only one node, then head->next == NULL. The function should return 1 in this case. Therefore, E1 must be head->next == NULL.

  2. Recursive Step (E2): If the list has more than one node, the total number of nodes is 11 (for the current node) plus the number of nodes in the remaining list starting from head->next. Therefore, E2 must be 1 + getListSize(head->next).

### Why other options are incorrect:

  • Option A: Uses head instead of head->next in E2, causing infinite recursion.

  • Option C: If E1 is head == NULL and returns 1, a single-node list would evaluate to 1 + getListSize(NULL) = 1 + 1 = 2, which is incorrect.

  • Option D: Passes head recursively in E2, leading to infinite recursion.

Thus, B is the correct option.

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 Q39 of the GATE CSE 2026 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. 19 of them are 2-mark MCQs like this one, and GATE CSE 2026 Set 1 has 2.

How GATE tests Programming in C

More from GATE CSE 2026 Set 1

Share this solution

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

GateAI solution cardGATE CSE 2026 Set 1 · Q39 · MCQ

Consider the following code snippet in C language that computes the number of nodes in a non-empty singly linked list pointed to by the pointer variable head . struct node{ int elt; struct node *next; }; int getListSize (struct node *head) { if( E1 ) return 1; return E2; } Which one of the following options gives the correct replacements for the expressions E1 and E2 ?

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.