Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

4. Unions: Choose the correct answer from the following multi-choice questions I

ID: 3575482 • Letter: 4

Question

4. Unions:

Choose the correct answer from the following multi-choice questions

I. Size of a union is determined by size of the.

a) First member in the union

b) Last member in the union

c) Biggest member in the union

d) Sum of the sizes of all members

II. Comment on the following union declaration?

1. #include <stdio.h>

2. union temp

3. {

4. int a;

5. float b;

6. char c;

7. };

union temp s = {1,2.5,’A’}; //REF LINE

Which member of the union will be active after REF LINE?

a) a

b) b

c) c

d) Such declaration are illegal

III. What would be the size of the following union declaration?

1. #include <stdio.h>

2. union uTemp

3. {

4. double a;

5. int b[10];

6. char c;

7. }u;

(Assuming size of double = 8, size of int = 4, size of char = 1)

a) 4

b) 8

c) 40

d) 80

IV. What type of data is holded by variable u int this C code?

1. #include <stdio.h>

2. union u_tag

3. {

4. int ival;

5. float fval;

6. char *sval;

7. } u;

The variable u here

a) Will be large enough to hold the largest of the three types;

b) Will be large enough to hold the smallest of the three types;

c) Will be large enough to hold the all of the three types;

d) None of the mentioned

V. Members of a union are accessed as________________.

a) union-name.member

b) union-pointer->member

c) Both a & b

d) None of the mentioned

VI. What is the output of this C code?

1. #include <stdio.h>

2. struct

3. {

4. char *name;

5. union

6. {

7. char *sval;

8. } u;

9. } symtab[10];

the first character of the string sval by either of

a) *symtab[i].u.sval

b) symtab[i].u.sval[0]

c) You cannot have union inside structure

d) Both a & b

VII. What is the output of this C code(size of int and float is 4)?

1. #include <stdio.h>

2. union

3. {

4. int ival;

5. float fval;

6. } u;

7. void main()

8. {

9. printf("%d", sizeof(u));

10. }

a) 16

b) 8

c) 4

d) 32

VIII. What is the output of this C code?

1. #include <stdio.h>

2. union stu

3. {

4. int ival;

5. float fval;

6. };

7. void main()

8. {

9. union stu r;

10. r.ival = 5;

11. printf("%d", r.ival);

12. }

a) 9

b) Compile time error

c) 16

d) 5

Explanation / Answer

1)ans is C:

Explanation:Size of union is size of leargest element in union.All members share same memory

============================================================================

III. What would be the size of the following union declaration?

1. #include <stdio.h>

2. union uTemp

3. {

4. double a;

5. int b[10];

6. char c;

7. }u;

Ans is c:40

Union has size of largest member here.Int array size 40 byte

===========================================================================

V. Members of a union are accessed as________________.

a) union-name.member

b) union-pointer->member

c) Both a & b

d) None of the mentioned

ans is C)both a&b

union-name.member is used when we create object.union-pointer->member is used when we create pointer to object

==========================================================================

VII. What is the output of this C code(size of int and float is 4)?

1. #include <stdio.h>

2. union

3. {

4. int ival;

5. float fval;

6. } u;

7. void main()

8. {

9. printf("%d", sizeof(u));

10. }

a) 16

b) 8

c) 4

d) 32

Ans is C)4

Because here largest datatype is 4 which can be of int or float

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote