0
0
C++programming~10 mins

Structure vs union comparison in C++ - Interactive Practice

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

Complete the code to declare a structure named MyStruct.

C++
struct [1] {
    int a;
    float b;
};
Drag options to blanks, or click blank then click option'
AMyStruct
BData
CMyUnion
DStruct1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'union' instead of 'struct' as the name.
Choosing a name that doesn't match the declaration.
2fill in blank
medium

Complete the code to declare a union named MyUnion.

C++
union [1] {
    int x;
    double y;
};
Drag options to blanks, or click blank then click option'
AMyStruct
BMyUnion
CDataUnion
DUnion1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'struct' instead of 'union' as the name.
Choosing a name that doesn't match the declaration.
3fill in blank
hard

Fix the error in the code to correctly access the union member.

C++
MyUnion u;
u.[1] = 10;
Drag options to blanks, or click blank then click option'
Ab
Ba
Cx
Dy
Attempts:
3 left
💡 Hint
Common Mistakes
Using a member name that does not exist in the union.
Assigning a value to a member of a different type.
4fill in blank
hard

Fill both blanks to create a structure and access its member.

C++
struct [1] {
    int age;
};

[2] person;
person.age = 25;
Drag options to blanks, or click blank then click option'
APerson
BMyStruct
DMyUnion
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the structure and variable type.
Confusing structure and union names.
5fill in blank
hard

Fill all three blanks to create a union, assign a value, and print the size.

C++
union [1] {
    int i;
    char c;
};

[2] u;
u.i = 100;

std::cout << sizeof([3]) << std::endl;
Drag options to blanks, or click blank then click option'
ADataUnion
Bu
DMyUnion
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent names for union and variable.
Using variable name instead of type in sizeof.