0% found this document useful (0 votes)
16 views7 pages

Structure MCQ (Giridhar)

The document contains a series of programming exercises focused on structures, unions, and bitfields in C. Each exercise presents a code snippet and asks the reader to identify errors or predict outputs based on the provided code. The problems range from basic pointer usage to understanding the behavior of unions and structure sizes in memory.

Uploaded by

k.s.yogeswar3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views7 pages

Structure MCQ (Giridhar)

The document contains a series of programming exercises focused on structures, unions, and bitfields in C. Each exercise presents a code snippet and asks the reader to identify errors or predict outputs based on the provided code. The problems range from basic pointer usage to understanding the behavior of unions and structure sizes in memory.

Uploaded by

k.s.yogeswar3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

PROBLEM SOLVING

Exercise No. :

Topics Covered : Structures, Union and

Bitfields Date :

Solve the following problems

Q. No. Question Detail Level

Spot the mistake:


struct Data {
int val;
};
int main() {
struct Data *ptr;
ptr->val = 100;
printf("%d", ptr->val);
return 0;
1 Basic
}
a. Uninitialized pointer used
b. Syntax error
c. No error
d. Wrong printf format
What will be the output of the following C code?
#include <stdio.h>
struct point
{
int x; int y;
};
void foo(struct point*);

2 int main() Basic


{
struct point p1[] = {1, 2, 3, 4};
foo(p1);
}
void foo(struct point p[])

1
PROBLEM SOLVING

{
printf("%d\n", p[1].x);
}
a. Compile time error
b. 3
c. 2
d. 1

What will be the output of the following C code on


a 64-bit system?
#include <stdio.h>
struct student
{
char *c;
};
void main()
{
3 Basic
struct student s[2];
printf("%d", sizeof(s));
}
a. 2
b. 4
c. 16
d. 8
What will be the output of the following C code?
#include <stdio.h>
struct student
{
char *c;
4 Basic
};
void main()
{

2
PROBLEM SOLVING

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

What will be the output of the following C code?


#include <stdio.h>
struct p
{
int x[2];
};
struct q
{
int *x;
};
int main()
{
5 Basic
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

What will be the output of the following C code?


6 Basic
#include <stdio.h>

3
PROBLEM SOLVING

struct point
{
int x; int y;
};
void foo(struct point*);
int main()
{
struct point p1[] = {1, 2, 3, 4, 5};
foo(p1);
}
void foo(struct point p[])
{
printf("%d %d\n", p->x, (p + 2)->y);
}

a. Compile time error


b. 1 0
c. 1 somegarbagevalue
d. undefined behaviour

What will be the output of the following C code?


#include <stdio.h>
int main()
{
struct p
{
char *name;
7 struct p *next; Basic
};
struct p *ptrary[10];
struct p p, q;
p.name = "xyz";
p.next = NULL;
ptrary[0] = &p;

4
PROBLEM SOLVING

q.name = (char*)malloc(sizeof(char)*3);
strcpy(q.name, p.name);
q.next = &q; ptrary[1] = &q;
printf("%s\n", ptrary[1]->next->next->name);
}

a. Compile time error


b. Depends on the compiler\
c. Undefined behaviour
d. xyz

What will be the output of the following C code?


#include <stdio.h>
struct p
{
unsigned int x : 1;
unsigned int y : 1;
};
int main()
{
struct p p;
8 p.x = 1; Basic
p.y = 2;
printf("%d\n", p.y);
}

a. 1
b. 2
c. 0
d. Depends on the compiler

What will be the output of the following code?


9 Basic
struct example {

5
PROBLEM SOLVING

int a : 2;
int b : 2;
};
int main() {
struct example e = {3, 2};
printf("%d %d\n", e.a, e.b);
return 0;
}
a)3 2
b)-1 -2
c)1 0
d)0 1
What will be the output of the following code?
union data {
int i;
float f;
char str[20];
};

int main() {
union data d;
d.i = 10;
10 printf("%d\n", d.i); Basic
d.f = 220.5;
printf("%d\n", d.i);
return 0;
}
a) 10 and 0
b) 10 and 220
c) 10 and some undefined value
d) Compilation error
.

What is the size of the following union on a


11 system where int is 4 bytes and double is 8 bytes? Basic

6
PROBLEM SOLVING

union mix {
int a;
double b;
char c[9];
};
a)9 bytes
b)12 bytes
c)16 bytes
d)18 bytes

What will be the size of the following union


assuming int is 4 bytes and char is 1 byte?
union data {
struct {
char a;
int b;
} s;
int x;
};
12 a)4 bytes Basic

b)5 bytes
c)8 bytes
d)16 bytes

You might also like