GateAI
Programming in C

GATE CSE 2021 Set 2 · Q35

MCQ2 marksMedium

Time

Consider the following ANSI C program:#include <stdio.h> #include <stdlib.h> struct Node{ int value; struct Node *next;}; int main(){ struct Node *boxE, *head, *boxN; int index = 0; boxE = head = (struct Node *) malloc(sizeof(struct Node)); head->value = index; for (index = 1; index <= 3; index++){ boxN = (struct Node *) malloc(sizeof(struct Node)); boxE->next = boxN; boxN->value = index; boxE = boxN; } for (index = 0; index <= 3; index++) { printf("Value at index %d is %d ", index, head->value); head = head->next; printf("Value at index %d is %d ", index+1, head->value); } }Which one of the statements below is correct about the program?

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: D) It dereferences an uninitialized pointer that may result in a run-time error.

Programming in C

Let us trace the program step-by-step:

  1. Memory Allocation and Linked List Creation:

    • boxE = head = malloc(...) creates the first node (head->value = 0).

    • The loop for (index = 1; index <= 3; index++) creates 33 more nodes with values 1,2,1, 2, and 33, linking them sequentially (boxE->next = boxN).

    • A total of 44 nodes are allocated (with values 0,1,2,30, 1, 2, 3).

    • Crucially, the next pointer of the last node (with value 33) is never set to NULL. Memory returned by malloc contains uninitialized garbage values, so the next pointer of the last node holds an indeterminate (garbage) memory address.

  2. Traversal in the Second Loop:

    • For index = 0: prints head->value (00), advances head = head->next (points to node with value 11), and prints head->value (11).

    • For index = 1: prints head->value (11), advances head = head->next (points to node with value 22), and prints head->value (22).

    • For index = 2: prints head->value (22), advances head = head->next (points to node with value 33), and prints head->value (33).

    • For index = 3: prints head->value (33), and then executes head = head->next. Since the last node's next pointer was never initialized, head is assigned garbage.

    • The subsequent line head->value dereferences this uninitialized pointer, which leads to undefined behavior and typically causes a segmentation fault / run-time error.

  3. Evaluation of Distractors:

    • (A) The program creates 44 nodes, not 55.

    • (B) The loops have finite bounds (index <= 3), so there is no infinite loop.

    • (C) In ANSI C (C99 and later), falling off the end of main without an explicit return statement implicitly returns 00; it is not a compiler error.

Therefore, option D is the correct statement.

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 Q35 of the GATE CSE 2021 Set 2 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 2021 Set 2 has 3.

How GATE tests Programming in C

More from GATE CSE 2021 Set 2

Share this solution

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

GateAI solution cardGATE CSE 2021 Set 2 · Q35 · MCQ

Consider the following ANSI C program: #include #include struct Node{ int value; struct Node *next;}; int main(){ struct Node *boxE, *head, *boxN; int index = 0; boxE = head = (struct Node *) malloc(sizeof(struct Node)); head->value = index; for (index = 1; index next = boxN; boxN->value = index; boxE = boxN; } for (index = 0; index value); head = head->next; printf("Value at index %d is %d ", index+1, head->value); } } Which one of the statements below is correct about the program?

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.