C Programming Questions and Answers on Pointer to Structures for Freshers

1. What is the output of this C code?
  1.     #include 
  2.     struct p
  3.     {
  4.         int x;
  5.         char y;
  6.     };
  7.     int main()
  8.     {
  9.         struct p p1[] = {1, 92, 3, 94, 5, 96};
  10.         struct p *ptr1 = p1;
  11.         int x = (sizeof(p1) / 3);
  12.         if (x == sizeof(int) + sizeof(char))
  13.             printf("%d\n", ptr1->x);
  14.         else
  15.             printf("falsen");
  16.     }
a) Compile time error
b) 1
c) Undefined behaviour
d) false
Answer: d
2. What is the output of this C code?
  1.     #include 
  2.     struct p
  3.     {
  4.         int x;
  5.         char y;
  6.     };
  7.     int main()
  8.     {
  9.         struct p p1[] = {1, 92, 3, 94, 5, 96};
  10.         struct p *ptr1 = p1;
  11.         int x = (sizeof(p1) / sizeof(ptr1));
  12.         if (x == 1)
  13.             printf("%d\n", ptr1->x);
  14.         else
  15.             printf("false\n");
  16.     }
a) Compile time error
b) 1
c) false
d) Undefined behaviour
Answer: c
3. What is the output of this C code?
  1.     #include 
  2.     struct p
  3.     {
  4.         int x;
  5.         char y;
  6.     };
  7.     typedef struct p* q*;
  8.     int main()
  9.     {
  10.         struct p p1[] = {1, 92, 3, 94, 5, 96};
  11.         q ptr1 = p1;
  12.         printf("%d\n", ptr1->x);
  13.     }
a) Compile time error
b) 1
c) Undefined behaviour
d) Segmentation fault
Answer: a
4. What is the output of this C code?
  1.     #include 
  2.     struct p
  3.     {
  4.         int x;
  5.         char y;
  6.     };
  7.     void foo(struct p* );
  8.     int main()
  9.     {
  10.         typedef struct p* q;
  11.         struct p p1[] = {1, 92, 3, 94, 5, 96};
  12.         foo(p1);
  13.     }
  14.     void foo(struct p* p1)
  15.     {
  16.         q ptr1 = p1;
  17.         printf("%d\n", ptr1->x);
  18.     }
a) Compile time error
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;)

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;)

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?
  1.     #include 
  2.     struct temp
  3.     {
  4.         int a;
  5.     } s;
  6.     void change(struct temp);
  7.     main()
  8.     {
  9.         s.a = 10;
  10.         change(s);
  11.         printf("%d\n", s.a);
  12.     }
  13.     void change(struct temp s)
  14.     {
  15.         s.a = 1;
  16.     }
a) Output will be 1
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?
  1.  #include 
  2.     struct p
  3.      {
  4.         int x;
  5.         int y;
  6.     };
  7.     int main()
  8.     {
  9.         struct p p1[] = {1, 92, 3, 94, 5, 96};
  10.         struct p *ptr1 = p1;
  11.         int x = (sizeof(p1) / 5);
  12.         if (x == 3)
  13.             printf("%d %d\n", ptr1->x, (ptr1 + x - 1)->x);
  14.         else
  15.             printf("false\n");
  16.     }
a) Compile time error
b) 1 5
c) Undefined behaviour
d) false
Answer: d
11. What is the output of this C code?
  1.     #include 
  2.     struct student
  3.     {
  4.         char *c;
  5.     };
  6.     void main()
  7.     {
  8.         struct student m;
  9.         struct student *s = &m;
  10.         s->c = "hello";
  11.         printf("%s", s->c);
  12.     }
a) hello
b) Run time error
c) Nothing
d) Depends on compiler
Answer: a
12. What is the output of this C code?
  1.     #include 
  2.     struct student
  3.     {
  4.         char *c;
  5.     };
  6.     void main()
  7.     {
  8.         struct student *s;
  9.         s->c = "hello";
  10.         printf("%s", s->c);
  11.     }
a) hello
b) Segmentation fault
c) Run time error
d) Nothing
Answer: b
13. What is the output of this C code?
  1.     #include 
  2.     struct student
  3.     {
  4.         char *c;
  5.     };
  6.     void main()
  7.     {
  8.         struct student m;
  9.         struct student *s = &m;
  10.         s->c = "hello";
  11.         printf("%s", m.c);
  12.     }
a) Run time error
b) Nothing
c) hello
d) Varies
Answer: c
14. What is the output of this C code?
  1.     #include 
  2.     struct student
  3.     {
  4.         char *c;
  5.     };
  6.     void main()
  7.     {
  8.         struct student m;
  9.         struct student *s = &m;
  10.         (*s).c = "hello";
  11.         printf("%s", m.c);
  12.     }
a) Run time error
b) Nothing
c) Varies
d) hello
Answer: d
15. What is the output of this C code?
  1.     #include 
  2.     struct student
  3.     {
  4.         char *c;
  5.     };
  6.     void main()
  7.     {
  8.         struct student n;
  9.         struct student *s = &n;
  10.         (*s).c = "hello";
  11.         printf("%p\n%p\n", s, &n);
  12.     }
a) Different address
b) Run time error
c) Nothing
d) Same address
Answer: d
16. What is the output of this C code?
  1.     #include 
  2.     struct p
  3.     {
  4.         int x[2];
  5.     };
  6.     struct q
  7.     {
  8.         int *x;
  9.     };
  10.     int main()
  11.     {
  12.         struct p p1 = {1, 2};
  13.         struct q *ptr1;
  14.         ptr1->x = (struct q*)&p1.x;
  15.         printf("%d\n", ptr1->x[1]);
  16.     }
a) Compile time error
b) Segmentation fault/code crash
c) 2
d) 1
Answer: b
17. What is the output of this C code?
  1.     #include 
  2.     struct p
  3.     {
  4.         int x[2];
  5.     };
  6.     struct q
  7.     {
  8.         int *x;
  9.     };
  10.     int main()
  11.     {
  12.         struct p p1 = {1, 2};
  13.         struct q *ptr1 = (struct q*)&p1;
  14.         ptr1->x = (struct q*)&p1.x;
  15.         printf("%d\n", ptr1->x[0]);
  16.     }
a) Compile time error
b) Undefined behaviour
c) Segmentation fault/code crash
d) 1
Answer: b
18. What is the output of this C code?
  1.     #include 
  2.     struct p
  3.     {
  4.         int x;
  5.         int y;
  6.     };
  7.     int main()
  8.     {
  9.         struct p p1[] = {1, 2, 3, 4, 5, 6};
  10.         struct p *ptr1 = p1;
  11.         printf("%d %d\n", ptr1->x, (ptr1 + 2)->x);
  12.     }
a) 1 5
b) 1 3
c) Compile time error
d) 1 4
Answer: a
19. What is the output of this C code?
  1.     #include 
  2.     struct p
  3.     {
  4.         int x;
  5.         char y;
  6.     };
  7.     int main(){
  8.         struct p p1[] = {1, 92, 3, 94, 5, 96};
  9.         struct p *ptr1 = p1;
  10.         int x = (sizeof(p1) / sizeof(struct p));
  11.         printf("%d %d\n", ptr1->x, (ptr1 + x - 1)->x);
  12.     }
a) Compile time error
b) Undefined behaviour
c) 1 3
d) 1 5
Answer: d

Related

Multiple Choice Questions 5357298995113376244

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item