Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a union named Data.
C
union [1] {
int i;
float f;
}; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'struct' instead of 'union' keyword.
Forgetting to name the union.
Using variable names instead of the union name.
✗ Incorrect
The keyword union is followed by the union name. Here, Data is the correct union name.
2fill in blank
mediumComplete the code to access the integer member of the union variable d.
C
union Data d; d.[1] = 10;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the float member name instead of the integer one.
Using undefined member names.
✗ Incorrect
To access the integer member of the union, use the member name i.
3fill in blank
hardFix the error in the union initialization.
C
union Data d = [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using assignment inside braces without dot notation.
Initializing with the wrong member name.
✗ Incorrect
In C, to initialize a union member by name, use the designated initializer syntax like {.i = 3}.
4fill in blank
hardFill both blanks to create a union variable and assign a float value.
C
union Data [1]; [2].f = 5.5;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for declaration and access.
Using undefined variable names.
✗ Incorrect
The variable name must be consistent. Here, d is used both to declare and access the union variable.
5fill in blank
hardFill all three blanks to create a union, declare a variable, and assign an integer value.
C
union [1] { int i; float f; } [2]; [3].i = 42;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in declaration and assignment.
Confusing union name with variable name.
✗ Incorrect
The union is named Data, the variable declared is d, and we assign to d.i.