Here is a listing of C quiz on “Pointer to Structures” along with answers, explanations and/or solutions:
1. What will be the output of the following 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("%dn", ptr1->x);
-
else
-
printf("falsen");
-
}
a) Compile time error
b) 1
c) Undefined behaviour
d) false
Answer: d
Clarification: None.
2. What will be the output of the following 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("%dn", ptr1->x);
-
else
-
printf("falsen");
-
}
a) Compile time error
b) 1
c) false
d) Undefined behaviour
Answer: c
Clarification: None.
3. What will be the output of the following 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("%dn", ptr1->x);
-
}
a) Compile time error
b) 1
c) Undefined behaviour
d) Segmentation fault
Answer: a
Clarification: None.
4. What will be the output of the following 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("%dn", ptr1->x);
-
}
a) Compile time error
b) 1
c) Segmentation fault
d) Undefined behaviour
Answer: a
Clarification: None.
5. Which of the following is an incorrect syntax for pointer to structure?
(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
Clarification: None.
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;)
a) func(&s.a);
b) func(&(s).a);
c) func(&(s.a));
d) none of the mentioned
Answer: d
Clarification: None.
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
Clarification: None.
8. Which option is not possible for the following function call?
-
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
Clarification: None.
9. What will be the output of the following C code?
-
#include
-
struct temp
-
{
-
int a;
-
} s;
-
void change(struct temp);
-
main()
-
{
-
s.a = 10;
-
change(s);
-
printf("%dn", 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
Answer: b
Clarification: None.
10. What will be the output of the following 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 %dn", ptr1->x, (ptr1 + x - 1)->x);
-
else
-
printf("falsen");
-
}
a) Compile time error
b) 1 5
c) Undefined behaviour
d) false
Answer: d
Clarification: None.