0% found this document useful (0 votes)
30 views5 pages

DPP 01

Uploaded by

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

DPP 01

Uploaded by

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

1

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

The output is: int main()


(a) C B {
(b) A C struct days *p=s;
(c) Compilation error p=p+3;
(d) Garbage values printf("%c", *p++->q);
printf("%c", *++p->q);
[MCQ] p=p-2;
5. #include <stdio.h> printf("%s",p->q);
struct s{ return 0;
char a, b; }
}; The output string printed is-
void f(struct s s1){ (a) WhWednesday
s1.a+=32; (b) WTTuesday
s1.b+=32; (c) WTWednesday
} (d) WhTuesday
void g(struct s *p){
static count=2;
[MSQ]
p->a+=count++;
7. Which of the following statements are INCORRECT?
p->b+=++count;
(a) Functions cannot be defined inside the
}
structure.
int main()
{ (b) Structure variable of the same structure type
struct s s1, s2; can be defined inside a structure.
s1.a='A'; s1.b='C'; (c) A function may not contain a structure
s2.a='B'; s2.b='D'; defined in it.
f(s1); (d) Existing structure cannot be contained in
for(int i=0;i<2;i++)g(&s2); another structure.
printf("%c\t%c",s1.a, s1.b);
printf("\t%c\t%c",s2.a, s2.b); [NAT]
return 0; 8. #include<stdio.h>
} #include<string.h>
The output is: struct t
(a) a c B D {
(b) A C B D char sname[20];
(c) A C H N };
(d) a c B N int main ()
{
[MCQ] struct t t1, t2;
6. #include <stdio.h> strcpy(t1.sname, "GATEWallah"); //line a
struct days{ t2.sname="GATE2023"; //line b
char *q; printf("%s", t1.sname); //line c
}s[]={"Sunday", "Monday", "Tuesday", "Wednesday", printf("%s", t2.sname); //line d
"Thursday", "Friday", "Saturday"}; return 0;
}
The number of lines with error among lines a,b,c,d are
___________.
3

Answer Key
1. (8) 5. (c)
2. (28) 6. (d)
3. (a) 7. (b, c, d)
4. (b) 8. (2)
4

Hints and Solutions

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)

entire structure is passed


a b
s1 A D C B
s1.a + = 3
s2.a – = 1
formal parameters are changed.
Actual parameters haven’t changed
 Output: A C
5

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.

700 701 702 703 704 705 706 707 708


S a t u r d a y \0

s 100 200 300 400 500 600 700



array of structures

p 800 812 816 808


printf(“%c”, *p++  q);
*812  400  w
printf(“%c”, *++p  q);
*++812  500  *++500
 *++501
h

Any issue with DPP, please report by clicking here:- https://forms.gle/t2SzQVvQcs638c4r5


For more questions, kindly visit the library section: Link for web: https://smart.link/sdfez8ejd80if

PW Mobile APP: https://smart.link/7wwosivoicgd4

You might also like