This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Pointer to Structures – 2”.
Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.
1. What will be the output of the following C code?
#include <stdio.h>
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
View Answer
Explanation: None.
2. What will be the output of the following C code?
#include <stdio.h>
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
View Answer
Explanation: None.
3. What will be the output of the following C code?
#include <stdio.h>
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
View Answer
Explanation: None.
4. What will be the output of the following C code?
#include <stdio.h>
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
View Answer
Explanation: None.
5. What will be the output of the following C code?
#include <stdio.h>
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
View Answer
Explanation: None.
6. What will be the output of the following C code?
#include <stdio.h>
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
View Answer
Explanation: None.
7. What will be the output of the following C code?
#include <stdio.h>
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
View Answer
Explanation: None.
8. What will be the output of the following C code?
#include <stdio.h>
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
View Answer
Explanation: None.
9. What will be the output of the following C code?
#include <stdio.h>
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
View Answer
Explanation: None.
Sanfoundry Global Education & Learning Series – C Programming Language.
To practice all areas of C language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check C Books
- Apply for C Internship
- Watch Advanced C Programming Videos
- Check Computer Science Books
- Practice Computer Science MCQs