GateAI
Programming in C

GATE CSE 2017 Set 1 · Q8

MCQ1 markEasy

Time

Consider the C code fragment given below.typedef struct node { int data; node* next; } node; void join(node* m, node* n){ node* p = n; while(p->next != NULL) { p = p->next; } p->next = m; }Assuming that \(m\) and \(n\) point to valid NULL-terminated linked lists, invocation ofjoin will

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: B) either cause a null pointer dereference or append list m to the end of list n.

Programming in C

Let us analyze what happens when join(m, n) is called:

  1. Pointer p is initialized to n (node* p = n;).

  2. If list n is empty, i.e., n == NULL, accessing p->next in the condition while(p->next != NULL) attempts to dereference a NULL pointer, which causes a null pointer dereference (segmentation fault / runtime error).

  3. If list n is non-empty (n != NULL), the while loop traverses to the last node of list n (the node where p->next == NULL).

  4. Once the loop terminates at the last node of n, the statement p->next = m; sets its next pointer to point to the head of list m, effectively appending list m to the end of list n.

Therefore, depending on whether list n is empty or non-empty, the function will either cause a null pointer dereference or append list m to the end of list n.

Hence, (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 Q8 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. 20 of them are 1-mark MCQs 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 · Q8 · MCQ

Consider the C code fragment given below. typedef struct node { int data; node* next; } node; void join(node* m, node* n){ node* p = n; while(p->next != NULL) { p = p->next; } p->next = m; } Assuming that m and n point to valid NULL-terminated linked lists, invocation of join will

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.