DPP 01
DPP 01
CSE/IT Batch:Hinglish
Programming in C
Structures and Unions DPP-01
[NAT] [MCQ]
1. #include <stdio.h> 3. #include <stdio.h>
union u{ struct s{
int a; char a, b;
char b; };
double d[2]; void f(struct s *p){
}; p->a+=2;
int main() p->b-=1;
{ }
union u u1; int main()
printf("%d",(int)sizeof(u1)); {
return 0; struct s s1, s2, *q;
} s1.a='A'; s1.b='C';
Assume that objects of the type int, char and double q=&s1;
occupy 2 bytes, 1 bytes and 4 bytes, respectively. f(q);
The memory requirement for variable u1 is printf("%c\t%c",s1.a, s1.b);
_________(in bytes). return 0;
}
[NAT] The output is:
2. Consider the following C declaration: (a) C B
struct (b) A C
{ (c) Compilation error
long a[3]; (d) Garbage values
union
{ [MCQ]
int y; 4. #include <stdio.h>
float z; struct s{
}u; char a, b;
} s; };
Assume that objects of the type int, float and long void f(struct s s1){
occupy 2 bytes, 4 bytes and 8 bytes, respectively. s1.a+=3;
The memory requirement for variable s is s1.b-=1;
_________(in bytes). }
int main()
{
struct s s1;
s1.a='A'; s1.b='C';
f(s1);
printf("%c\t%c",s1.a, s1.b);
return 0;
}
2
Answer Key
1. (8) 5. (c)
2. (28) 6. (d)
3. (a) 7. (b, c, d)
4. (b) 8. (2)
4
1. (8) 5. (c)
The size of the union is equal to the maximum size of a b
the member variables s1 A C
Here, double d[2] has the maximum size 100 101
size of union = (2 × 4) bytes = 8 bytes
a b
s2 B D H D H N
2. (28)
200 201
The size of the structure variable is the sum of the sizes
g(&s2); g(&s2);
of all its member variables
Size of structure = size of long a[3] + size of union f(s1)
= 8 × 3 + max (4, 4)
= 24 + 4
entire structure is passed.
= 28
It will change the formal parameters but the actual
3. (a) parameters in main() won’t change
a b g(200)
s1 'A' C 'C' B q 100 // q &s1
count 2 3 4
100 101
f(100)
p 100 static
100 a + = 2; // 100 a = ‘C’ 200 a + = 2; // D
100 b – = 1; // 100 b = ‘B’ 200 b + = 4; // H
printf() prints ‘C B’; g(200)
count 4 5 6
4. (b)
200 a + = 4; // H
main()
a b
200 b + = 6; // N
s1 A C Output-
printf(“%c\t%c”, s1.a, s1.b);
print(“%c\t%c”; s1.a, s1.b); printf(“%c\t%c”, s2.a, s2.b);
AC A C H N
f(s1)
6. (d) p=p–2;//p=808
100 101 102 103 104 105 106 printf(“%s”, p q);
S u n d a y \0 808 300 Tuesday
Output: WhTuesday
200 201 202 203 204 205 206
7. (b, c, d)
M o n d a y \0
(a) CORRECT. Functions cannot be defined
inside the structure
300 301 302 303 304 305 306 307
(b) INCORRECT. Structure variable of the same
T u e s d a y \0 structure type cannot be defined inside structure.
(c) INCORRECT. A function can contain a structure
400 401 402 403 404 405 406 407 409 410 defined in it.
W e d n e s d a y \0 (d) INCORRECT. Existing structure can be
contained in another structure
500 501 502 503 504 505 506 507 508
8. (2)
T h u r s d a y \0 line b: ERROR. Constant base address cannot be
changed
600 601 602 603 604 605 606 line d: ERROR. As line 2 has error, line 4 cannot be
F r i d a y \0 executed.