C Programming Questions and Answers on Self-Referential Structures for Freshers

https://www.computersprofessor.com/2017/12/c-programming-questions-and-answers-on_28.html
1. What is the output of this C code?
#include
typedef struct p *q;
int main()
{
struct p
{
int x;
char y;
q ptr;
};
struct p p = {1, 2, &p};
printf("%d\n", p.ptr->x);
return 0;
}
a) Compile time error
b) 1
c) Depends on the compiler
d) None of the mentioned
b) 1
c) Depends on the compiler
d) None of the mentioned
Answer: a
2. What is the output of this C code?
#include
int main()
{
typedef struct p *q;
struct p
{
int x;
char y;
q ptr;
};
struct p p = {1, 2, &p};
printf("%d\n", p.ptr->x);
return 0;
}
a) Compile time error
b) 1
c) Depends on the compiler
d) Depends on the standard
b) 1
c) Depends on the compiler
d) Depends on the standard
Answer: b
3. What is the output of this C code?
#include
typedef struct p *q;
struct p
{
int x;
char y;
q ptr;
};
int main()
{
struct p p = {1, 2, &p};
printf("%d\n", p.ptr->ptr->x);
return 0;
}
a) Compile time error
b) Segmenation fault
c) Undefined behaviour
d) 1
b) Segmenation fault
c) Undefined behaviour
d) 1
Answer: d
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
b) 2
c) 3
d) All of the mentioned
Answer: d
5. Which of the following is not possible?
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
6. Which of the following techinique is faster for travelling in binary trees?
a) Iteration
b) Recursion
c) Both Iteration and Recursion
d) Depends from compiler to compiler
Answer: b
7. For the following declaration of structure, which of the following will stop the loop at the last node of a linked list?
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
8. What is the output of this C code?
#include
struct student
{
char *c;
struct student *point;
};
void main()
{
struct student s;
struct student m;
s.c = m.c = "hi";
m.point = &s;
(m.point)->c = "hey";
printf("%s\t%s\t", s.c, m.c);
}
a) hey hi
b) hi hey
c) Run time error
d) hey hey
b) hi hey
c) Run time error
d) hey hey
Answer: a
9. What is the output of this C code?
#include
struct student
{
char *c;
struct student *point;
};
void main()
{
struct student s;
struct student m;
m.point = s;
(m.point)->c = "hey";
printf("%s", s.c);
}
a) Nothing
b) Compile time error
c) hey
d) Varies
b) Compile time error
c) hey
d) Varies
Answer: b
10. What is the output of this C code?
#include
struct student
{
char *c;
struct student point;
};
void main()
{
struct student s;
s.c = "hello";
printf("%s", s.c);
}
a) hello
b) Nothing
c) Varies
d) Compile time error
b) Nothing
c) Varies
d) Compile time error
Answer: d
11. What is the output of this C code?
#include
struct student
{
char *c;
struct student *point;
};
void main()
{
struct student s;
printf("%d", sizeof(s));
}
a) 5
b) 9
c) 8
d) 16
b) 9
c) 8
d) 16
Answer: c
12. What is the output of this C code?
#include
struct student
{
char *c;
struct student *point;
};
void main()
{
struct student s;
struct student *m = &s;
printf("%d", sizeof(student));
}
a) Compile time error
b) 8
c) 5
d) 16
b) 8
c) 5
d) 16
Answer: a
13. What is the output of this C code?
#include
struct p
{
int x;
char y;
struct p *ptr;
};
int main()
{
struct p p = {1, 2, &p};
printf("%d\n", p.ptr->x);
return 0;
}
a) Compile time error
b) Undefined behaviour
c) 1
d) 2
b) Undefined behaviour
c) 1
d) 2
Answer: c
14. What is the output of this C code?
#include
typedef struct p *q;
struct p
{
int x;
char y;
q ptr;
};
typedef struct p *q;
int main()
{
struct p p = {1, 2, &p};
printf("%d\n", p.ptr->x);
return 0;
}
a) Compile time error
b) 1
c) Undefined behaviour
d) Address of p
b) 1
c) Undefined behaviour
d) Address of p
Answer: a