GATE / CS ARCHIVE

GATE CSE 2017 SET 1 / QUESTION 8

GATE CSE 2017 Set 1 Q8

Programming in C · Pointers & Memory · Full GATE CSE 2017 Set 1 paper

GATE 2017 Set 1, Q8 1 mark · MCQ
Report issue

Pointers & Memory

Question 8, GATE 2017: The printed C fragment is:
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; }
Assume m and n point to valid NULL-terminated linked lists. A always appends m to n; B either null-pointer dereference or appends m to n; C always null-pointer dereference; D always appends n to m. The source typedef is retained literally, not silently repaired.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Our answer: B under intended corrected C declaration

1. Trace the pointer, not the data fields: p starts at n, and each iteration follows the next link. For a nonempty terminating n list, the loop stops at its final node.

2. The assignment p->next=m connects that final node to the head of m. It therefore appends m to n, including the case m is empty.

3. An empty list can be represented by n=NULL; then even the first test p->next dereferences NULL. This prevents the “for all inputs” claim A. Intended B assumes a valid declaration and disjoint lists for an acyclic concatenation.

node* appears inside the typedef before node is declared as a typedef name. Standalone C needs struct node* next or an earlier forward typedef. This source defect is not repaired in the displayed code.

Original paper, page 5 · Answer key