Structures and Unions MCQs in C Language
1.
Consider the structure:
struct Point {
int x;
int y;
};
What is the size of struct Point on a 32-bit system (int=4 bytes)?
A) 4
B) 8
C) 2
D) 16
✅ Answer: B
Explanation:
int x → 4 bytes, int y → 4 bytes → total = 8 bytes.
2.
Which of the following statements correctly declares a variable p1 of type struct Point?
A) struct Point p1;
B) Point p1;
C) struct p1 Point;
D) Point struct p1;
✅ Answer: A
Explanation:struct keyword is required unless typedef is used.
3.
Output:
#include <stdio.h>
struct Point {
int x;
int y;
};
int main() {
struct Point p1 = {5,10};
printf("%d", p1.x);
}
A) 0
B) 5
C) 10
D) Compiler error
✅ Answer: B
Explanation:
p1.x = 5 → prints 5.
4.
Output:
#include <stdio.h>
struct Point {
int x;
int y;
};
int main() {
struct Point p1 = {5,10};
p1.y = p1.x + 5;
printf("%d", p1.y);
}
A) 5
B) 10
C) 10
D) 15
✅ Answer: D
Explanation:
p1.y = p1.x + 5 → 5 + 10? Wait carefully.
p1.x = 5 → p1.y = 5 + 5 = 10 ✅
Correct Answer: 10
5.
Size of the union:
union Data {
int a;
float b;
char c;
};
A) 1
B) 4
C) 8
D) Compiler dependent
✅ Answer: B
Explanation:
Union size = size of largest member → int or float = 4 bytes.
6.
Which of the following correctly initializes a union Data with integer 10?
A) union Data d = {10};
B) union Data d = {a:10};
C) union Data d = {b:10};
D) Both A & B
✅ Answer: D
Explanation:
C99 allows both designated and positional initialization.
7.
Output:
#include <stdio.h>
union Data {
int a;
char b;
};
int main() {
union Data d;
d.a = 257;
printf("%d", d.b);
}
A) 1
B) 257
C) 0
D) Undefined
✅ Answer: A
Explanation:
Union shares memory → d.a=257 → binary 00000001 00000001 → first byte stored in d.b → 1.
8.
Size of the following struct:
struct S {
char c;
int i;
};
A) 5
B) 8
C) 9
D) 4
✅ Answer: B
Explanation:
Padding added after char to align int → total = 8 bytes.
9.
Which member of the following struct is at offset 0?
struct Example {
int a;
char b;
float c;
};
A) a
B) b
C) c
D) Compiler dependent
✅ Answer: A
Explanation:
Struct members stored in order, first member at offset 0.
10.
Output:
#include <stdio.h>
struct Point {
int x;
int y;
} p1 = {1,2}, p2 = {3,4};
int main() {
p2 = p1;
printf("%d", p2.y);
}
A) 1
B) 2
C) 3
D) 4
✅ Answer: B
Explanation:
Struct assignment copies all members → p2.y = p1.y = 2.
11.
Output:
#include <stdio.h>
union Data {
int a;
char b;
};
int main() {
union Data d1, d2;
d1.a = 5;
d2 = d1;
printf("%d", d2.a);
}
A) 0
B) 5
C) Undefined
D) Compiler error
✅ Answer: B
Explanation:
Union assignment copies memory → d2.a = 5.
12.
Which of the following statements is invalid?
A) struct S { int a; }; struct S s;
B) union U { int a; float b; }; union U u;
C) struct { int a; } s1, s2;
D) union { int a; float b; } u1, u2;
✅ Answer: None, all valid
Explanation:
Anonymous structs/unions can declare variables immediately.
13.
Output:
#include <stdio.h>
struct Point {
int x;
int y;
};
struct Point p = {1,2};
int main() {
printf("%d", sizeof(p));
}
A) 4
B) 8
C) 2
D) 16
✅ Answer: B
Explanation:
2 integers → 4+4 = 8 bytes.
14.
Output:
#include <stdio.h>
union Data {
int a;
double b;
};
int main() {
printf("%lu", sizeof(union Data));
}
A) 4
B) 8
C) 16
D) Compiler dependent
✅ Answer: B (on most 64-bit compilers)
Explanation:
Union size = largest member → double = 8 bytes.
15.
Which keyword allows defining a type name for a struct?
A) typedef
B) struct
C) union
D) enum
✅ Answer: A
Explanation:
typedef allows typedef struct Point Point; to avoid using struct keyword repeatedly.
16.
Output:
#include <stdio.h>
struct Point { int x; int y; };
int main() {
struct Point arr[2] = {{1,2},{3,4}};
printf("%d", arr[1].x);
}
A) 1
B) 2
C) 3
D) 4
✅ Answer: C
Explanation:
arr[1].x = 3.
17.
Output:
#include <stdio.h>
union Data {
int a;
char b[2];
};
int main() {
union Data d;
d.a = 258;
printf("%d", d.b[1]);
}
A) 1
B) 2
C) 0
D) Compiler dependent
✅ Answer: A
Explanation:
Little-endian → d.a=258 → 0x0102 → second byte = 1.
18.
Which of the following is true for unions?
A) All members occupy separate memory
B) Members share same memory
C) Cannot assign value to union
D) Union size = sum of members
✅ Answer: B
Explanation:
All union members share memory → only one active at a time.
19.
Size of the following struct on 64-bit system:
struct S {
char c1;
int a;
char c2;
};
A) 6
B) 8
C) 12
D) 16
✅ Answer: C
Explanation:
Padding for alignment → 1+3(padding)+4+1+3(padding)=12 bytes.
20.
Output:
#include <stdio.h>
struct S { int a; int b; };
int main() {
struct S s1 = {1,2};
struct S s2 = {3,4};
s1 = s2;
printf("%d", s1.a);
}
A) 1
B) 2
C) 3
D) 4
✅ Answer: C
Explanation:
Struct assignment copies all members → s1.a = s2.a = 3.
21.
Output:
#include <stdio.h>
struct S { int a; char b; };
int main() {
struct S s = {10,'A'};
printf("%c", s.b);
}
A) 10
B) ‘A’
C) Compiler error
D) Garbage
✅ Answer: B
Explanation:
s.b = ‘A’, prints character → ‘A’.
22.
Size of union:
union U {
char c;
int i;
double d;
};
A) 1
B) 4
C) 8
D) 16
✅ Answer: C
Explanation:
Union size = largest member → double = 8 bytes (64-bit system).
23.
Output:
#include <stdio.h>
union Data { int a; char b; };
int main() {
union Data d;
d.b = 65;
printf("%d", d.a);
}
A) 65
B) 0
C) Undefined
D) Compiler error
✅ Answer: C
Explanation:
Setting one union member may change other members → value of d.a is undefined.
24.
Which is correct to access a struct member using pointer p?
struct S { int a; };
struct S s = {5};
struct S *p = &s;
A) p.a
B) *p.a
C) p->a
D) &p.a
✅ Answer: C
Explanation:
Pointer to struct uses arrow operator → p->a = 5.
25.
Output:
#include <stdio.h>
struct S { int a; int b; } s1={1,2};
int main() {
struct S s2 = s1;
s2.a = 5;
printf("%d", s1.a);
}
A) 1
B) 2
C) 5
D) Compiler error
✅ Answer: A
Explanation:
Struct assignment copies values → changing s2 does not affect s1 → s1.a=1.
26.
Output:
#include <stdio.h>
union U { int a; int b; };
int main() {
union U u;
u.a = 10;
u.b = 20;
printf("%d", u.a);
}
A) 10
B) 20
C) Compiler error
D) Undefined
✅ Answer: B
Explanation:
Union members share memory → last assignment u.b = 20 → u.a = 20.
27.
Output:
#include <stdio.h>
struct Point { int x; int y; };
struct Point p = {1,2};
int main() {
printf("%p", &p);
}
A) Address of p
B) Value of p
C) Compiler error
D) 0
✅ Answer: A
Explanation:
&p → prints memory address of struct variable.
28.
Size of:
struct S { char c1; char c2; int i; };
A) 6
B) 8
C) 12
D) 4
✅ Answer: C
Explanation:
Padding → c1+c2(2)+padding(2)+int(4) = 8? Wait carefully.
char c1=1, char c2=1 → padding 2 → int i=4 → total = 8 bytes ✅
Answer: 8
29.
Which of the following is valid union declaration?
A) union { int a; float b; } u1, u2;
B) union U { int a; float b; } u;
C) Both A and B
D) None
✅ Answer: C
Explanation:
Both anonymous and named union declarations are valid.
30.
Output:
#include <stdio.h>
union Data { int a; float b; };
int main() {
union Data d;
d.a = 5;
printf("%d", d.a);
}
A) 0
B) 5
C) Compiler error
D) Garbage
✅ Answer: B
Explanation:
Assigned integer → prints 5.
31.
Which of the following increases memory efficiency?
A) Struct
B) Union
C) Both
D) None
✅ Answer: B
Explanation:
Union shares memory → smaller size than struct with same members.
32.
Output:
#include <stdio.h>
struct S { int a; char b; };
int main() {
struct S s = {5,'A'};
printf("%d", s.a);
}
A) 5
B) ‘A’
C) Compiler error
D) Garbage
✅ Answer: A
Explanation:
s.a = 5 → prints 5.
33.
Size of struct with nested union:
struct S {
char c;
union U { int a; double b; } u;
};
A) 8
B) 12
C) 16
D) 24
✅ Answer: C
Explanation:
char c=1 + padding 7 → union size=largest member double=8 → total 16? Wait carefully.
char=1 + padding 7 → 8, union=8 → total=16 ✅
Answer: 16
34.
Output:
#include <stdio.h>
union U { int a; int b; };
int main() {
union U u;
u.a = 100;
printf("%d", u.b);
}
A) 0
B) 100
C) Compiler error
D) Undefined
✅ Answer: B
Explanation:
int members share memory → same value stored → 100.
35.
Output:
#include <stdio.h>
struct S { int a; int b; } s = {1,2};
int main() {
struct S *p = &s;
printf("%d", p->b);
}
A) 1
B) 2
C) Compiler error
D) Garbage
✅ Answer: B
Explanation:
Arrow operator used to access struct via pointer → p->b=2.
36.
Which is true about unions?
A) Cannot initialize union
B) Only one member stores value at a time
C) All members updated simultaneously
D) Union size = sum of member sizes
✅ Answer: B
Explanation:
Union shares memory → one active member at a time.
37.
Output:
#include <stdio.h>
struct S { char c; int a; };
int main() {
struct S s = {'A',5};
printf("%c", s.c);
}
A) 5
B) ‘A’
C) Compiler error
D) Garbage
✅ Answer: B
Explanation:
s.c=’A’, prints character.
38.
Size of:
struct S { char c; double d; };
A) 8
B) 12
C) 16
D) 24
✅ Answer: C
Explanation:
char=1 + padding 7 → double=8 → total=16.
39.
Output:
#include <stdio.h>
union U { int a; char b; };
int main() {
union U u;
u.a = 512;
printf("%d", u.b);
}
A) 0
B) 2
C) 512
D) Compiler error
✅ Answer: B
Explanation:
Little-endian → LSB stored in u.b → 512=0x0200 → u.b=2.
40.
Which statement correctly copies a struct variable?
struct S { int a; int b; };
struct S s1 = {1,2}, s2;
A) s1 = s2;
B) s2 = s1;
C) *s2 = s1;
D) s2->s1;
✅ Answer: B
Explanation:
Struct assignment → copies all members.
41.
Output:
#include <stdio.h>
struct S { int a; float b; };
int main() {
struct S s = {1,2.5};
printf("%.1f", s.b);
}
A) 1.0
B) 2.5
C) Compiler error
D) 0
✅ Answer: B
Explanation:
s.b = 2.5 → prints 2.5.
42.
Size of struct:
struct S { int a; char b; float c; };
A) 8
B) 12
C) 16
D) 10
✅ Answer: B
Explanation:
int=4, char=1+padding=3, float=4 → total 12 bytes.
43.
Output:
#include <stdio.h>
union U { int a; float b; };
int main() {
union U u = {5};
printf("%d", u.a);
}
A) 0
B) 5
C) Compiler error
D) Garbage
✅ Answer: B
Explanation:
Union initialized → u.a=5 → prints 5.
44.
Output:
#include <stdio.h>
struct S { char c; int i; };
int main() {
struct S s = {'A',10};
printf("%d", s.i);
}
A) 10
B) ‘A’
C) Compiler error
D) Garbage
✅ Answer: A
Explanation:
s.i=10 → prints 10.
45.
Output:
#include <stdio.h>
union Data { int a; char b[4]; };
int main() {
union Data d;
d.a = 66051; // 0x00010203
printf("%d", d.b[2]);
}
A) 1
B) 2
C) 3
D) Undefined
✅ Answer: B
Explanation:
Little-endian → 0x03 0x02 0x01 0x00 → d.b[2]=2.
46.
Output:
#include <stdio.h>
struct S { int a; int b; };
int main() {
struct S s = {1,2};
struct S *p = &s;
p->b = 10;
printf("%d", s.b);
}
A) 2
B) 10
C) Compiler error
D) Garbage
✅ Answer: B
Explanation:
Pointer modifies the struct member → s.b = 10.
47.
Which is true about nested structs?
A) Structs cannot be nested
B) Inner struct increases memory of outer struct
C) Inner struct decreases memory of outer struct
D) Nested struct always occupies 0 bytes
✅ Answer: B
Explanation:
Nested struct members contribute to total memory of outer struct.
48.
Output:
#include <stdio.h>
struct Point {
int x;
int y;
};
int main() {
struct Point arr[2] = {{1,2},{3,4}};
printf("%d", arr[1].y);
}
A) 1
B) 2
C) 3
D) 4
✅ Answer: D
Explanation:
arr[1].y = 4.
49.
Size of:
struct S { char c; char arr[3]; };
A) 3
B) 4
C) 5
D) 6
✅ Answer: B
Explanation:
char=1 + char[3]=3 → total 4 bytes, properly aligned → no extra padding needed.
50.
Output:
#include <stdio.h>
union U { int a; char b[4]; };
int main() {
union U u;
u.b[0] = 1;
u.b[1] = 2;
printf("%d", u.a);
}
A) 513
B) 258
C) 1
D) Compiler error
✅ Answer: A
Explanation:
Little-endian: 0x0201 → 513 in decimal.
51.
Which of the following statements is true?
A) Struct assignment is not allowed
B) Union assignment is allowed
C) Struct can be assigned directly
D) None
✅ Answer: C
Explanation:
Struct assignment copies all members → valid.
52.
Output:
#include <stdio.h>
struct S { int a; int b; };
int main() {
struct S s1 = {1,2};
struct S s2 = s1;
s2.a = 100;
printf("%d", s1.a);
}
A) 1
B) 2
C) 100
D) Compiler error
✅ Answer: A
Explanation:
Struct assignment copies values → modifying s2 does not affect s1.
53.
Size of struct with padding:
struct S { char c; int i; char d; };
A) 6
B) 8
C) 12
D) 16
✅ Answer: C
Explanation:
c=1 + padding 3 → int=4 → d=1 + padding 3 → total=12 bytes.
54.
Output:
#include <stdio.h>
union Data { int a; float b; };
int main() {
union Data d;
d.b = 2.5;
printf("%f", d.b);
}
A) 2.5
B) Compiler error
C) Undefined
D) 0
✅ Answer: A
Explanation:
Assigned float → prints correctly.
55.
Output:
#include <stdio.h>
struct S { int a; };
struct S s1 = {5};
int main() {
struct S *p = &s1;
printf("%d", (*p).a);
}
A) 5
B) Compiler error
C) 0
D) Garbage
✅ Answer: A
Explanation:
(*p).a = 5 → prints 5.
56.
Output:
#include <stdio.h>
struct S { int a; int b; };
int main() {
struct S arr[2] = {{1,2},{3,4}};
printf("%d", arr[0].b + arr[1].a);
}
A) 3
B) 4
C) 5
D) 6
✅ Answer: C
Explanation:
arr[0].b=2, arr[1].a=3 → sum=5.
57.
Which is true about memory layout of unions?
A) Each member has separate memory
B) All members share same memory
C) Union size = sum of all members
D) Union cannot contain arrays
✅ Answer: B
Explanation:
Union shares memory → only one active member at a time.
58.
Output:
#include <stdio.h>
struct S { char c1; char c2; int i; };
int main() {
struct S s = {'A','B',10};
printf("%c", s.c2);
}
A) ‘A’
B) ‘B’
C) 10
D) Compiler error
✅ Answer: B
Explanation:
s.c2 = ‘B’ → prints ‘B’.
59.
Size of union containing array:
union U { int a; char b[5]; };
A) 4
B) 5
C) 8
D) 9
✅ Answer: C
Explanation:
Union size = largest member → char b[5] may be padded → next multiple of 4 → 8 bytes.
60.
Output:
#include <stdio.h>
struct Point { int x; int y; };
int main() {
struct Point p = {1,2};
printf("%d", sizeof(p));
}
A) 4
B) 8
C) 2
D) 16
✅ Answer: B
Explanation:
int x=4, int y=4 → total=8 bytes.
61.
Output:
#include <stdio.h>
union U { int a; double b; };
int main() {
union U u;
u.b = 3.14;
printf("%f", u.b);
}
A) 3.14
B) 0
C) Compiler error
D) Undefined
✅ Answer: A
Explanation:
Assigned double → prints correctly.
62.
Which statement about nested structs is false?
A) Struct can have another struct as member
B) Inner struct increases total size
C) Inner struct cannot be pointer
D) Valid in GATE-level C
✅ Answer: C
Explanation:
Inner struct can be pointer → statement C is false.
63.
Output:
#include <stdio.h>
struct S { int a; };
int main() {
struct S s = {5};
struct S *p = &s;
p->a += 10;
printf("%d", s.a);
}
A) 5
B) 10
C) 15
D) Compiler error
✅ Answer: C
Explanation:
Pointer modifies member → 5+10=15.
64.
Size of struct with 3 chars:
struct S { char a; char b; char c; };
A) 3
B) 4
C) 5
D) 6
✅ Answer: B
Explanation:
3 bytes + 1 byte padding → total 4 bytes.
65.
Output:
#include <stdio.h>
union Data { int a; char b; };
int main() {
union Data d;
d.b = 'A';
printf("%c", d.b);
}
A) ‘A’
B) 65
C) Compiler error
D) Undefined
✅ Answer: A
Explanation:
Assigned char → prints ‘A’.
66.
Which is true about struct arrays?
A) Cannot declare array of structs
B) Array of structs allows member access by index
C) Array of structs cannot be passed to functions
D) Struct array size = sum of elements + 1
✅ Answer: B
Explanation:
Array of structs → access like arr[i].member.
67.
Output:
#include <stdio.h>
struct S { int a; int b; };
int main() {
struct S arr[2] = {{1,2},{3,4}};
printf("%d", arr[1].b);
}
A) 2
B) 3
C) 4
D) Compiler error
✅ Answer: C
Explanation:
arr[1].b = 4.
68.
Size of union with char array[10] and int:
union U { int a; char arr[10]; };
A) 4
B) 10
C) 12
D) 16
✅ Answer: C
Explanation:
Largest member arr[10]=10 → padded to multiple of 4 → 12 bytes.
69.
Output:
#include <stdio.h>
struct S { int a; int b; };
int main() {
struct S s1 = {1,2}, s2;
s2 = s1;
printf("%d", s2.a);
}
A) 1
B) 2
C) Compiler error
D) 0
✅ Answer: A
Explanation:
Struct assignment → s2.a = s1.a =1.
70.
Output:
#include <stdio.h>
union U { int a; char b[4]; };
int main() {
union U u;
u.a = 1025; // 0x0401
printf("%d", u.b[1]);
}
A) 1
B) 4
C) 1025
D) Undefined
✅ Answer: B
Explanation:
Little-endian: u.b[1]=0x04 → 4 decimal.
71.
Output:
#include <stdio.h>
struct S { char c; int i; };
int main() {
struct S s = {'A', 100};
printf("%d", sizeof(s));
}
A) 5
B) 8
C) 12
D) 16
✅ Answer: B
Explanation:
char=1 + padding 3 → int=4 → total=8 bytes.
72.
Output:
#include <stdio.h>
union U { int a; int b; };
int main() {
union U u;
u.a = 5;
u.b = 10;
printf("%d", u.a);
}
A) 5
B) 10
C) Compiler error
D) Undefined
✅ Answer: B
Explanation:
Union shares memory → last assignment u.b = 10 → u.a = 10.
73.
Which statement is true about unions?
A) Union members occupy separate memory
B) Only one member stores value at a time
C) Union size = sum of all members
D) Union cannot contain arrays
✅ Answer: B
Explanation:
Union memory is shared → only one active member at a time.
74.
Output:
#include <stdio.h>
struct S { int a; float b; } s = {1,2.5};
int main() {
printf("%.1f", s.b);
}
A) 1.0
B) 2.5
C) Compiler error
D) 0
✅ Answer: B
Explanation:
s.b = 2.5 → prints 2.5.
75.
Output:
#include <stdio.h>
struct S { int a; char b; };
int main() {
struct S s = {1,'A'};
printf("%c", s.b);
}
A) 1
B) ‘A’
C) Compiler error
D) Garbage
✅ Answer: B
Explanation:
s.b = ‘A’.
76.
Size of struct with nested union:
struct S { char c; union U { int a; double b; } u; };
A) 8
B) 12
C) 16
D) 24
✅ Answer: C
Explanation:
char=1 + padding 7 → union=8 → total=16.
77.
Output:
#include <stdio.h>
struct Point { int x; int y; };
int main() {
struct Point p1 = {1,2}, p2;
p2 = p1;
printf("%d", p2.x);
}
A) 1
B) 2
C) Compiler error
D) 0
✅ Answer: A
Explanation:
Struct assignment copies all members → p2.x = 1.
78.
Output:
#include <stdio.h>
union U { int a; char b; };
int main() {
union U u;
u.a = 1024;
printf("%d", u.b);
}
A) 0
B) 1
C) 1024
D) Compiler error
✅ Answer: A
Explanation:
Little-endian → least significant byte=0 → u.b=0.
79.
Which is true for arrays of structs?
A) Not allowed in C
B) Can access members by index → arr[i].member
C) Must use pointer to access
D) Array size = struct size × index + 1
✅ Answer: B
Explanation:
Array of structs supports arr[i].member.
80.
Output:
#include <stdio.h>
struct S { int a; int b; };
int main() {
struct S s = {1,2};
struct S *p = &s;
printf("%d", p->b);
}
A) 1
B) 2
C) Compiler error
D) Garbage
✅ Answer: B
Explanation:
Arrow operator → p->b=2.
81.
Output:
#include <stdio.h>
struct S { char c1; char c2; int i; };
int main() {
struct S s = {'A','B',10};
printf("%d", sizeof(s));
}
A) 6
B) 8
C) 12
D) 16
✅ Answer: C
Explanation:
c1+c2=2 + padding 2 + int=4 → total=12 bytes.
82.
Output:
#include <stdio.h>
union Data { int a; float b; };
int main() {
union Data d;
d.a = 5;
printf("%d", d.a);
}
A) 0
B) 5
C) Compiler error
D) Garbage
✅ Answer: B
Explanation:
Union assigned → prints 5.
83.
Output:
#include <stdio.h>
struct S { int a; int b; } s1 = {1,2}, s2;
int main() {
s2 = s1;
s2.b = 20;
printf("%d", s1.b);
}
A) 2
B) 20
C) Compiler error
D) 0
✅ Answer: A
Explanation:
Struct assignment copies → changing s2 does not affect s1.
84.
Output:
#include <stdio.h>
union U { int a; char b[2]; };
int main() {
union U u;
u.a = 513;
printf("%d", u.b[1]);
}
A) 1
B) 2
C) 513
D) Undefined
✅ Answer: B
Explanation:
Little-endian: 513 → 0x0201 → u.b[1]=2.
85.
Output:
#include <stdio.h>
struct S { int a; char b; };
int main() {
struct S s = {5,'A'};
printf("%d", sizeof(s));
}
A) 5
B) 8
C) 6
D) 4
✅ Answer: B
Explanation:
Padding added after char → total size = 8 bytes.
86.
Output:
#include <stdio.h>
union U { int a; double b; };
int main() {
printf("%lu", sizeof(union U));
}
A) 4
B) 8
C) 16
D) Compiler dependent
✅ Answer: C
Explanation:
Largest member double=8 → padded to alignment → 16 bytes on 64-bit system.
87.
Output:
#include <stdio.h>
struct S { int a; struct S *p; };
int main() {
struct S s = {5,NULL};
printf("%p", s.p);
}
A) 5
B) NULL
C) Compiler error
D) Garbage
✅ Answer: B
Explanation:
Pointer initialized as NULL → prints 0x0.
88.
Output:
#include <stdio.h>
struct S { char c; int i; } s = {'A', 10};
int main() {
printf("%c", s.c);
}
A) 10
B) ‘A’
C) Compiler error
D) Garbage
✅ Answer: B
89.
Output:
#include <stdio.h>
union U { char c; int i; };
int main() {
union U u;
u.c = 'A';
printf("%c", u.c);
}
A) ‘A’
B) 65
C) Compiler error
D) Undefined
✅ Answer: A
90.
Output:
#include <stdio.h>
struct S { char c1; char c2; int i; };
int main() {
struct S s = {'A','B',5};
printf("%d", sizeof(s));
}
A) 6
B) 8
C) 12
D) 16
✅ Answer: C
91.
Output:
#include <stdio.h>
union Data { int a; char b[4]; };
int main() {
union Data d;
d.a = 1025;
printf("%d", d.b[1]);
}
A) 1
B) 4
C) 1025
D) Undefined
✅ Answer: B
92.
Output:
#include <stdio.h>
struct S { int a; int b; };
int main() {
struct S s1 = {1,2}, s2 = s1;
s2.a = 10;
printf("%d", s1.a);
}
A) 1
B) 10
C) Compiler error
D) 0
✅ Answer: A
93.
Output:
#include <stdio.h>
struct S { int a; char b; };
int main() {
struct S s = {5,'A'};
printf("%d", sizeof(s));
}
A) 5
B) 8
C) 6
D) 4
✅ Answer: B
94.
Output:
#include <stdio.h>
union U { int a; float b; };
int main() {
union U u;
u.a = 5;
printf("%d", u.a);
}
A) 0
B) 5
C) Compiler error
D) Garbage
✅ Answer: B
95.
Output:
#include <stdio.h>
struct Point { int x; int y; } p1 = {1,2}, p2;
int main() {
p2 = p1;
printf("%d", p2.y);
}
A) 1
B) 2
C) Compiler error
D) 0
✅ Answer: B
96.
Output:
#include <stdio.h>
union U { char c; int a; };
int main() {
union U u;
u.a = 513;
printf("%d", u.c);
}
A) 1
B) 2
C) 513
D) Undefined
✅ Answer: A
97.
Output:
#include <stdio.h>
struct S { char c1; char c2; int i; };
int main() {
struct S s = {'A','B',10};
printf("%c", s.c1);
}
A) ‘A’
B) ‘B’
C) 10
D) Compiler error
✅ Answer: A
98.
Size of:
struct S { int a; struct S *p; };
A) 4
B) 8
C) 12
D) 16
✅ Answer: B
Explanation:
int=4 + pointer=4 (on 32-bit) → total=8 bytes.
99.
Output:
#include <stdio.h>
union U { int a; char b[2]; };
int main() {
union U u;
u.a = 258;
printf("%d", u.b[1]);
}
A) 1
B) 2
C) 258
D) Undefined
✅ Answer: A
100.
Output:
#include <stdio.h>
struct S { int a; int b; };
int main() {
struct S s = {1,2};
struct S *p = &s;
p->a += 5;
printf("%d", s.a);
}
A) 1
B) 5
C) 6
D) 10
✅ Answer: D
Explanation:
p->a +=5 → 1+5=6 Wait carefully → s.a=1+5=6 ✅
Answer: 6