0
0
C++programming~20 mins

Why structures are needed in C++ - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Structure Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of accessing structure members
What is the output of this C++ code that uses a structure to store a person's data?
C++
#include <iostream>
#include <string>

struct Person {
    std::string name;
    int age;
};

int main() {
    Person p = {"Alice", 30};
    std::cout << p.name << " is " << p.age << " years old." << std::endl;
    return 0;
}
AAlice 30
B30 is Alice years old.
CAlice is 30 years old.
DError: cannot access members
Attempts:
2 left
💡 Hint
Look at how the structure members are accessed using the dot operator.
🧠 Conceptual
intermediate
1:30remaining
Why use structures instead of separate variables?
Why do programmers use structures in C++ instead of just separate variables for related data?
ATo group related data together for better organization and easier management.
BBecause structures make the program run faster automatically.
CTo avoid using functions in the program.
DBecause variables cannot store numbers without structures.
Attempts:
2 left
💡 Hint
Think about how grouping data helps in real life, like keeping all info about a person in one folder.
Predict Output
advanced
2:00remaining
Output of nested structures
What will this C++ program print when using nested structures?
C++
#include <iostream>
#include <string>

struct Date {
    int day;
    int month;
    int year;
};

struct Event {
    std::string name;
    Date date;
};

int main() {
    Event e = {"Meeting", {15, 8, 2024}};
    std::cout << e.name << " on " << e.date.day << "/" << e.date.month << "/" << e.date.year << std::endl;
    return 0;
}
AError: cannot nest structures
BMeeting 15 8 2024
CMeeting on 8/15/2024
DMeeting on 15/8/2024
Attempts:
2 left
💡 Hint
Look at how the nested structure Date is accessed inside Event.
🔧 Debug
advanced
2:00remaining
Identify the error in structure initialization
What error does this C++ code produce when trying to initialize a structure?
C++
#include <iostream>

struct Point {
    int x;
    int y;
};

int main() {
    Point p = (10, 20);
    std::cout << p.x << ", " << p.y << std::endl;
    return 0;
}
ASyntaxError: invalid initialization of structure
BOutput: 20, 20
CRuntime error: segmentation fault
DOutput: 10, 20
Attempts:
2 left
💡 Hint
Check how the structure is initialized with parentheses and commas.
🧠 Conceptual
expert
2:30remaining
Why structures improve code maintainability
How do structures help improve code maintainability in large C++ programs?
AThey automatically optimize memory usage without programmer effort.
BBy grouping related data, they reduce code duplication and make updates easier.
CThey replace the need for comments and documentation.
DThey allow the program to run without compiling.
Attempts:
2 left
💡 Hint
Think about how grouping data helps when you need to change or add features later.