Here is a listing of C Objective Questions on “Self-Referential Structures” along with answers, explanations and/or solutions:
1. What will be the output of the following C code?
-
#include -
typedef struct p *q;
-
int main()
-
{ -
struct p -
{ -
int x;
-
char y;
-
q ptr; -
};
-
struct p p = {1, 2, &p};
-
printf("%dn", p.ptr->x);
-
return 0;
-
}
a) Compile time error
b) 1
c) Depends on the compiler
d) None of the mentioned
Answer: a
Clarification: None.
2. What will be the output of the following C code?
-
#include -
int main()
-
{ -
typedef struct p *q;
-
struct p -
{ -
int x;
-
char y;
-
q ptr; -
};
-
struct p p = {1, 2, &p};
-
printf("%dn", p.ptr->x);
-
return 0;
-
}
a) Compile time error
b) 1
c) Depends on the compiler
d) Depends on the standard
Answer: b
Clarification: None.
3. What will be the output of the following C code?
-
#include -
typedef struct p *q;
-
struct p -
{ -
int x;
-
char y;
-
q ptr; -
};
-
int main()
-
{ -
struct p p = {1, 2, &p};
-
printf("%dn", p.ptr->ptr->x);
-
return 0;
-
}
a) Compile time error
b) Segmenation fault
c) Undefined behaviour
d) 1
Answer: d
Clarification: None.
4. The number of distinct nodes the following struct declaration can point to is _____________
-
struct node -
{ -
struct node *left;
-
struct node *centre;
-
struct node *right;
-
};
a) 1
b) 2
c) 3
d) All of the mentioned
Answer: d
Clarification: None.
5. Which of the following is not possible regarding the structure variable?
a) A structure variable pointing to itself
b) A structure variable pointing to another structure variable of same type
c) 2 different type of structure variable pointing at each other
d) None of the mentioned
Answer: d
Clarification: None.
6. Which of the following technique is faster for travelling in binary trees?
a) Iteration
b) Recursion
c) Both Iteration and Recursion
d) Depends from compiler to compiler
Answer: b
Clarification: None.
7. Which of the following will stop the loop at the last node of a linked list in the following C code snippet?
-
struct node -
{ -
struct node *next;
-
};
a)
while (p != NULL) { p = p->next; }
b)
while (p->next != NULL) { p = p->next; }
c)
while (1) { p = p->next; if (p == NULL) break; }
d) All of the mentioned
Answer: b
Clarification: None.
