0
0
C++programming~10 mins

Union basics in C++ - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AMyUnion
Bdata
CData
DValue
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or different names like 'data' or 'MyUnion'.
Forgetting to write the union name after the keyword.
2fill in blank
medium

Complete the code to create a variable named d of the union Data.

C++
union Data d[1];
Drag options to blanks, or click blank then click option'
A;
B()
C{}
D[]
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or braces instead of a semicolon.
Omitting the semicolon at the end.
3fill in blank
hard

Fix the error in assigning the integer 10 to the union variable d.

C++
d.[1] = 10;
Drag options to blanks, or click blank then click option'
Af
Bi
Cvalue
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning to the float member f instead of i.
Using names not declared in the union.
4fill in blank
hard

Fill both blanks to declare a union named Value with a char and a double member.

C++
union [1] {
    [2] c;
    double d;
};
Drag options to blanks, or click blank then click option'
AValue
Bint
Cchar
Dfloat
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong union names like 'value' or 'Val'.
Using wrong types like 'int' or 'float' for the char member.
5fill in blank
hard

Fill all three blanks to create a union named Data with an int member i, a float member f, and assign 3.14 to f.

C++
union [1] {
    int [2];
    float f;
};

[1] d;
d.[3] = 3.14f;
Drag options to blanks, or click blank then click option'
AData
Bi
Cf
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing member names or union names.
Assigning a float value to the int member.