C Programming Questions and Answers – Self-Referential Structures
C Programming Questions and Answers – Self-Referential Structures
1.What is the output of this C code?
#include <stdio.h>
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
View Answer
Answer:a
2.What is the output of this C code?
#include <stdio.h>
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
View Answer
Answer:b
3.What is the output of this C code?
#include <stdio.h>
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
View Answer
Answer:d
4.What is the output of this C code?
#include <stdio.h>
struct student
{
char *c;
struct student *point;
};
void main()
{
struct student s;
printf(“%d”, sizeof(s));
}
a) 5
b) 9
c) 8
d) 16
View Answer
Answer:c
5.What is the output of this C code?
#include <stdio.h>
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
View Answer
Answer:a
6.What is the output of this C code?
#include <stdio.h>
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
View Answer
Answer:c
7.What is the output of this C code?
#include <stdio.h>
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
View Answer
Answer:a
8.Presence of loop in a linked list can be tested by the compiler by.
a) Traveling the list, if NULL is encountered no loop exists
b) Comparing the address of nodes by address of every other node
c) Comparing the the value stored in a node by a value in every other node
d) Both (a) and (b).
View Answer
Answer:b
9.What is the output of this C code?
#include <stdio.h>
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
View Answer
Answer:a
10.What is the output of this C code?
#include <stdio.h>
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
View Answer
Answer:b
11.What is the output of this C code?
#include <stdio.h>
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
View Answer
Answer:d
12.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
View Answer
Answer:d
13.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 these
View Answer
Answer:d
14.Which of the following techinique is faster for travelling in binary trees?
a) Iteration
b) Recursion
c) Both (a) and (b)
d) Depends from compiler to compiler
View Answer
Answer:b
15.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
View Answer
Answer:b