C Programming – Pointer to Structures

C Programming - Pointer to Structures

Question 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
Question 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
Question 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
Question 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
Question 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 (a) and (b)
Question 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
Question 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
Question 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
Both (b) and (c).
Question 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
Question 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
Question 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
Question 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
Question 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
Question 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
Question 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
Question 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
Question 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
Question 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
Question 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


Check Your Answers:

  1. D
  2. C
  3. A
  4. A
  5. A
  6. D
  7. D
  8. A
  9. B
10. D
11. A
12. B
13. C
14. D
15. D
16. B
17. B
18. A
19. D


SHARE

Unknown

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment