Consider the C code fragment given below. typedef struct node { int data;…

2017

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

Answer: B. either cause a null pointer dereference or append list m to the end of list n.Concept: A linked list is represented by a pointer to its first node, and this representation also covers the empty list: a list of zero nodes has its head…

  1. A.

    append list m to the end of list n for all inputs.

  2. B.

    either cause a null pointer dereference or append list m to the end of list n.

  3. C.

    cause a null pointer dereference for all inputs.

  4. D.

    append list n to the end of list m for all inputs.

Attempted by 380 students.

Show answer & explanation

Correct answer: B

Concept:

A linked list is represented by a pointer to its first node, and this representation also covers the empty list: a list of zero nodes has its head pointer equal to NULL, which is itself a trivially NULL-terminated list. Code that dereferences a list's head pointer, for example reading head->next, without first checking it against NULL, behaves correctly for every non-empty list but is undefined for the empty-list case.

(The official GATE 2017 paper itself prints this exact fragment -- typedef struct node { ... node* next; ... } node -- and multiple archived copies of the paper confirm this precise wording. Read strictly against the C standard, the type name node is not yet complete at the point node* next appears inside its own definition, so this exact spelling would not compile under a strict reading; the usual self-referential idiom is struct node* next instead. The exam's own published answer key still keys this question to the option below, confirming the intended reading throughout is that next is a pointer to the same node type -- this printed shorthand does not change that intended reading or the derivation that follows.)

As is standard for join()-style problems, m and n here are read as two independent lists that share no nodes (and m is not the same list as n); if the two pointers aliased shared nodes, the assignment p->next = m could create a cycle instead of a simple concatenation, but that case falls outside the standard reading of "m and n point to valid NULL-terminated linked lists" used by the official key and this derivation.

Application:

  1. p is initialised to n, so p holds exactly whatever value n was passed as.

  2. If n is the empty list (n equal to NULL): p is NULL, and the very first evaluation of the loop condition, p->next, dereferences that null pointer -- this is undefined behaviour before any appending can happen.

  3. If n is a non-empty list (n not equal to NULL): the while loop repeatedly moves p to p->next until p reaches n's last node, the node whose next field is NULL; every node visited during this walk is a real node, so no dereference error occurs.

  4. At that point, p->next = m sets the last node of n's next pointer to m, linking m's first node immediately after n's last node -- this is precisely appending list m to the end of list n. (If m is NULL, n's last node simply now points to NULL, so n is left unchanged, still consistent with appending nothing.)

Cross-check:

  • "Append m to n for all inputs" fails to cover the case where n is the empty list, which the trace above shows behaves differently.

  • "Cause a null pointer dereference for all inputs" fails to cover the case where n is a proper non-empty list, which the trace above shows completes safely.

  • "Append n to the end of m" reverses the roles: the pointer that walks and gets modified, p (derived from n), always belongs to n's own chain, never to m's, so the list being extended is n, not m.

Result:

Because a valid NULL-terminated linked list is allowed to be the empty list, join's behaviour genuinely splits into two mutually exclusive, exhaustive cases: a null pointer dereference when n is empty, or list m appended to the end of list n when n is non-empty. Only the option stating both of these together matches every valid input.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Iocl Engineers Officers Grade A Paper 2

Loading lesson…