C Programming Questions and Answers on Pointer to Structures for Freshers

https://www.computersprofessor.com/2017/12/c-programming-questions-and-answers-on_27.html
1. What is the output of this C code?
#include
struct p
{
int x;
char y;
};
int main()
{
struct p p1[] = {1, 92, 3, 94, 5, 96};
struct p *ptr1 = p1;
int x = (sizeof(p1) / 3);
if (x == sizeof(int) + sizeof(char))
printf("%d\n", ptr1->x);
else
printf("falsen");
}
a) Compile time error
b) 1
c) Undefined behaviour
d) false
b) 1
c) Undefined behaviour
d) false
Answer: d
2. What is the output of this C code?
#include
struct p
{
int x;
char y;
};
int main()
{
struct p p1[] = {1, 92, 3, 94, 5, 96};
struct p *ptr1 = p1;
int x = (sizeof(p1) / sizeof(ptr1));
if (x == 1)
printf("%d\n", ptr1->x);
else
printf("false\n");
}
a) Compile time error
b) 1
c) false
d) Undefined behaviour
b) 1
c) false
d) Undefined behaviour
Answer: c
3. What is the output of this C code?
#include
struct p
{
int x;
char y;
};
typedef struct p* q*;
int main()
{
struct p p1[] = {1, 92, 3, 94, 5, 96};
q ptr1 = p1;
printf("%d\n", ptr1->x);
}
a) Compile time error
b) 1
c) Undefined behaviour
d) Segmentation fault
b) 1
c) Undefined behaviour
d) Segmentation fault
Answer: a
4. What is the output of this C code?
#include
struct p
{
int x;
char y;
};
void foo(struct p* );
int main()
{
typedef struct p* q;
struct p p1[] = {1, 92, 3, 94, 5, 96};
foo(p1);
}
void foo(struct p* p1)
{
q ptr1 = p1;
printf("%d\n", ptr1->x);
}
a) Compile time error
b) 1
c) Segmentation fault
d) Undefined behaviour
b) 1
c) Segmentation fault
d) Undefined behaviour
Answer: a
5. Which of the following are incorrect syntax for pointer to structure?
(Assuming struct temp{int b;}*my_struct;)
(Assuming struct temp{int b;}*my_struct;)
a) *my_struct.b = 10;
b) (*my_struct).b = 10;
c) my_struct->b = 10;
d) Both *my_struct.b = 10; and (*my_struct).b = 10;
Answer: a
6. Which of the following is an incorrect syntax to pass by reference a member of a structure in a function?
(Assume: struct temp{int a;}s;)
(Assume: struct temp{int a;}s;)
a) func(&s.a);
b) func(&(s).a);
c) func(&(s.a));
d) none of the mentioned
Answer: d
7. Which of the following structure declaration doesn’t require pass-by-reference?
a) struct{int a;}s;
main(){}
b) struct temp{int a;};
main(){
struct temp s;
}
c) struct temp{int a;};
main(){}
struct temp s;
d) none of the mentioned
Answer: d
8. For the following function call which option is not possible?
func(&s.a); //where s is a variable of type struct and a is the member of the struct.
a) Compiler can access entire structure from the function
b) Individual member’s address can be displayed in structure
c) Individual member can be passed by reference in a function
d) None of the mentioned
Answer: a
9. Comment on the output of this C code?
#include
struct temp
{
int a;
} s;
void change(struct temp);
main()
{
s.a = 10;
change(s);
printf("%d\n", s.a);
}
void change(struct temp s)
{
s.a = 1;
}
a) Output will be 1
b) Output will be 10
c) Output varies with machine
d) Compile time error
b) Output will be 10
c) Output varies with machine
d) Compile time error
Answer: b
10. What is the output of this C code?
#include
struct p
{
int x;
int y;
};
int main()
{
struct p p1[] = {1, 92, 3, 94, 5, 96};
struct p *ptr1 = p1;
int x = (sizeof(p1) / 5);
if (x == 3)
printf("%d %d\n", ptr1->x, (ptr1 + x - 1)->x);
else
printf("false\n");
}
a) Compile time error
b) 1 5
c) Undefined behaviour
d) false
b) 1 5
c) Undefined behaviour
d) false
Answer: d
11. What is the output of this C code?
#include
struct student
{
char *c;
};
void main()
{
struct student m;
struct student *s = &m;
s->c = "hello";
printf("%s", s->c);
}
a) hello
b) Run time error
c) Nothing
d) Depends on compiler
b) Run time error
c) Nothing
d) Depends on compiler
Answer: a
12. What is the output of this C code?
#include
struct student
{
char *c;
};
void main()
{
struct student *s;
s->c = "hello";
printf("%s", s->c);
}
a) hello
b) Segmentation fault
c) Run time error
d) Nothing
b) Segmentation fault
c) Run time error
d) Nothing
Answer: b
13. What is the output of this C code?
#include
struct student
{
char *c;
};
void main()
{
struct student m;
struct student *s = &m;
s->c = "hello";
printf("%s", m.c);
}
a) Run time error
b) Nothing
c) hello
d) Varies
b) Nothing
c) hello
d) Varies
Answer: c
14. What is the output of this C code?
#include
struct student
{
char *c;
};
void main()
{
struct student m;
struct student *s = &m;
(*s).c = "hello";
printf("%s", m.c);
}
a) Run time error
b) Nothing
c) Varies
d) hello
b) Nothing
c) Varies
d) hello
Answer: d
15. What is the output of this C code?
#include
struct student
{
char *c;
};
void main()
{
struct student n;
struct student *s = &n;
(*s).c = "hello";
printf("%p\n%p\n", s, &n);
}
a) Different address
b) Run time error
c) Nothing
d) Same address
b) Run time error
c) Nothing
d) Same address
Answer: d
16. What is the output of this C code?
#include
struct p
{
int x[2];
};
struct q
{
int *x;
};
int main()
{
struct p p1 = {1, 2};
struct q *ptr1;
ptr1->x = (struct q*)&p1.x;
printf("%d\n", ptr1->x[1]);
}
a) Compile time error
b) Segmentation fault/code crash
c) 2
d) 1
b) Segmentation fault/code crash
c) 2
d) 1
Answer: b
17. What is the output of this C code?
#include
struct p
{
int x[2];
};
struct q
{
int *x;
};
int main()
{
struct p p1 = {1, 2};
struct q *ptr1 = (struct q*)&p1;
ptr1->x = (struct q*)&p1.x;
printf("%d\n", ptr1->x[0]);
}
a) Compile time error
b) Undefined behaviour
c) Segmentation fault/code crash
d) 1
b) Undefined behaviour
c) Segmentation fault/code crash
d) 1
Answer: b
18. What is the output of this C code?
#include
struct p
{
int x;
int y;
};
int main()
{
struct p p1[] = {1, 2, 3, 4, 5, 6};
struct p *ptr1 = p1;
printf("%d %d\n", ptr1->x, (ptr1 + 2)->x);
}
a) 1 5
b) 1 3
c) Compile time error
d) 1 4
b) 1 3
c) Compile time error
d) 1 4
Answer: a
19. What is the output of this C code?
#include
struct p
{
int x;
char y;
};
int main(){
struct p p1[] = {1, 92, 3, 94, 5, 96};
struct p *ptr1 = p1;
int x = (sizeof(p1) / sizeof(struct p));
printf("%d %d\n", ptr1->x, (ptr1 + x - 1)->x);
}
a) Compile time error
b) Undefined behaviour
c) 1 3
d) 1 5
b) Undefined behaviour
c) 1 3
d) 1 5
Answer: d