wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Unions in C Programming

Total questions: 25

Worksheet time: 25mins

Name
Class
Date
1.

What is the syntax for declaring a union in C programming?

a)

union union_name { data_type member1; data_type member2; ...; }

b)

union union_name { data_type member1, member2, ...; }

c)

union union_name { data_type member1; data_type member2; ... };

d)

union union_name { data_type member1, member2; ...; }

2.

How do you access members of a union in C programming?

a)

Using the dot (.) operator

b)

Using the exclamation (!) operator

c)

Using the arrow (->) operator

d)

Using the semicolon (;) operator

3.

What is the size of a union in C programming?

a)

Size of the largest member of the union

b)

Size of the sum of all members of the union

c)

Size of the smallest member of the union

d)

Size of the average of all members of the union

4.

What is the difference between a union and a structure in C programming?

a)

A union and a structure have the same memory allocation in C programming

b)

A union and a structure both allow for the same type of data storage in C programming

c)

A union and a structure are interchangeable in C programming

d)

The main difference between a union and a structure is that a structure allocates enough memory to store each of its members, while a union allocates enough memory to store the largest member.

5.

Can a union be used as a function argument in C programming? If yes, how?

a)

Yes, a union can be used as a function argument in C programming.

b)

Yes, but only if the union is passed by reference.

c)

Yes, but only if the union is declared as a global variable.

d)

No, a union cannot be used as a function argument in C programming.

6.

How can a union be used within a structure in C programming?

a)

By declaring the union inside the structure definition

b)

By declaring the union outside the structure definition

c)

By using the keyword 'struct' before the union name

d)

By using the keyword 'union' before the structure name

7.

Explain the concept of padding in unions in C programming.

a)

Padding in unions is used to ensure that each member of the union is properly aligned in memory, according to the largest member's size.

b)

Padding in unions is used to minimize memory usage by removing unnecessary space between members

c)

Padding in unions is used to prevent data corruption during memory allocation

d)

Padding in unions is used to enforce a specific order of member access

8.

What are the advantages of using unions in C programming?

a)

Unions in C programming are only used for mathematical operations

b)

Unions in C programming are not compatible with other programming languages

c)

The advantages of using unions in C programming include saving memory and representing different types of data in a single variable.

d)

Unions in C programming can only store one type of data

9.

What are the limitations of unions in C programming?

a)

The limitations of unions in C programming include the ability to store only one value at a time and the potential for undefined behavior when accessing the wrong member.

b)

Accessing the wrong member of a union in C programming always results in a compilation error

c)

Unions in C programming can store multiple values at the same time

d)

Unions in C programming have no limitations

10.

Write a sample code to demonstrate the usage of unions in C programming.

a)

union SampleUnion { int intValue; char charValue; };

b)

int main() { union SampleUnion u; u.charValue = 'a'; printf("Value of charValue: %c\n", u.charValue); return 0; }

c)

union SampleUnion { int intValue; double doubleValue; };

d)

Here is a sample code to demonstrate the usage of unions in C programming: #include union SampleUnion { int intValue; float floatValue; }; int main() { union SampleUnion u; u.intValue = 10; printf("Value of intValue: %d\n", u.intValue); u.floatValue = 3.14; printf("Value of floatValue: %f\n", u.floatValue); return 0; }

11.

What are the potential risks of using unions in C programming?

a)

Unions in C programming can lead to memory corruption if not used carefully

b)

Unions in C programming are completely risk-free

c)

Unions in C programming always result in a compilation error if used incorrectly

d)

Unions in C programming have no potential risks

12.

Explain the process of accessing a specific member of a union in C programming.

a)

Using the dot (.) operator followed by the member name

b)

Using the exclamation (!) operator followed by the member name

c)

Using the arrow (->) operator followed by the member name

d)

Using the semicolon (;) operator followed by the member name

13.

What is the purpose of using unions in C programming?

a)

Unions in C programming are only used for storing single data types

b)

Unions in C programming are used to represent different types of data in a single variable

c)

Unions in C programming are used for mathematical operations only

d)

Unions in C programming have no specific purpose

14.

Explain the concept of memory allocation for unions in C programming.

a)

Memory allocation for unions in C programming is based on the size of the largest member.

b)

Memory allocation for unions in C programming is based on the sum of all members' sizes.

c)

Memory allocation for unions in C programming is based on the size of the smallest member.

d)

Memory allocation for unions in C programming is based on the average size of all members.

15.

What is the syntax for accessing a union member within a structure in C programming?

a)

Using the dot (.) operator followed by the member name

b)

Using the exclamation (!) operator followed by the member name

c)

Using the arrow (->) operator followed by the member name

d)

Using the semicolon (;) operator followed by the member name

16.

How can unions be used to optimize memory usage in C programming?

a)

Unions can be used to minimize memory usage by removing unnecessary space between members

b)

Unions can be used to enforce a specific order of member access

c)

Unions can be used to store multiple values at the same time

d)

Unions can be used to represent different types of data in a single variable

17.

Explain the concept of memory allocation for unions in C programming.

a)

Memory allocation for unions in C programming is based on the size of the largest member.

b)

Memory allocation for unions in C programming is based on the sum of all members' sizes.

c)

Memory allocation for unions in C programming is based on the size of the smallest member.

d)

Memory allocation for unions in C programming is based on the average size of all members.

18.

What are the potential risks of using unions in C programming?

a)

Unions in C programming can lead to memory corruption if not used carefully

b)

Unions in C programming are completely risk-free

c)

Unions in C programming always result in a compilation error if used incorrectly

d)

Unions in C programming have no potential risks

19.

What is the syntax for accessing a union member within a structure in C programming?

a)

Using the dot (.) operator followed by the member name

b)

Using the exclamation (!) operator followed by the member name

c)

Using the arrow (->) operator followed by the member name

d)

Using the semicolon (;) operator followed by the member name

20.

_____________ is a special type that represents a group of constants (unchangeable values).

a)

enum

b)

structure

c)

array

d)

variable

e)

union

21.

________________ are a way to group several related variables into one place. Each variable is known as a member

a)

enum

b)

structure

c)

array

d)

variable

e)

union

22.

_____________ is a collection User-Defined Data type stored at same memory location.

a)

enum

b)

structure

c)

array

d)

variable

e)

union

23.

_____________ is a variable that stores the memory address of another variable as its value.

a)

structures

b)

pointer

c)

array

d)

variable

e)

union

24.

_____________ is used to store unknown values

a)

structures

b)

pointer

c)

array

d)

variable

e)

union

25.

_____________ is a variable that stores values of the same datatype

a)

structures

b)

pointer

c)

array

d)

variable

e)

union