Bird
Raised Fist0
C++programming~20 mins

Nested structures in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Nested Structures Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of nested struct member access
What is the output of this C++ code?
C++
struct Point {
    int x;
    int y;
};

struct Rectangle {
    Point topLeft;
    Point bottomRight;
};

#include <iostream>
int main() {
    Rectangle rect = {{1, 4}, {5, 1}};
    std::cout << rect.topLeft.x << "," << rect.bottomRight.y << std::endl;
    return 0;
}
A1,4
B5,4
C5,1
D1,1
Attempts:
2 left
💡 Hint
Look at how the nested points are initialized and which members are accessed.
❓ Predict Output
intermediate
2:00remaining
Output of nested struct with array member
What will this program print?
C++
struct Team {
    struct Player {
        int id;
        int score;
    } players[3];
};

#include <iostream>
int main() {
    Team team = {{{1, 10}, {2, 20}, {3, 30}}};
    std::cout << team.players[1].score << std::endl;
    return 0;
}
A10
B20
C30
D1
Attempts:
2 left
💡 Hint
Check which player index is accessed and which member is printed.
🔧 Debug
advanced
2:00remaining
Identify the error in nested struct initialization
This code tries to initialize nested structs but causes a compilation error. What is the error?
C++
struct Date {
    int day;
    int month;
    int year;
};

struct Event {
    char name[20];
    Date date;
};

int main() {
    Event e = {"Meeting", {12, 5}};
    return 0;
}
AStruct Event cannot contain another struct
BCannot initialize char array with string literal
CMissing year value in nested Date initialization
DMissing semicolon after struct definitions
Attempts:
2 left
💡 Hint
Check how many values are needed to initialize Date.
📝 Syntax
advanced
2:00remaining
Correct syntax to access nested struct member
Which option correctly accesses the 'z' member inside nested structs?
C++
struct Inner {
    int z;
};

struct Outer {
    Inner inner;
};

Outer o = {{5}};
Ao.inner.z
Bo->inner.z
Co.inner->z
Do->inner->z
Attempts:
2 left
💡 Hint
Remember the difference between dot (.) and arrow (->) operators.
🚀 Application
expert
3:00remaining
Calculate total price from nested structs
Given the structs below, what is the total price printed by the program?
C++
struct Item {
    char name[10];
    int quantity;
    double price_per_unit;
};

struct Order {
    Item items[2];
};

#include <iostream>
int main() {
    Order order = {{{"Pen", 10, 1.5}, {"Notebook", 5, 3.0}}};
    double total = 0;
    for (int i = 0; i < 2; ++i) {
        total += order.items[i].quantity * order.items[i].price_per_unit;
    }
    std::cout << total << std::endl;
    return 0;
}
A30
B25
C20
D35
Attempts:
2 left
💡 Hint
Multiply quantity by price for each item and add them up.

Practice

(1/5)
1. What is the main purpose of using nested structures in C++?
easy
A. To allow structures to inherit functions
B. To create multiple inheritance between classes
C. To increase the size of a structure unnecessarily
D. To group related data logically inside another structure

Solution

  1. Step 1: Understand nested structures concept

    Nested structures let you put one structure inside another to group related data logically.
  2. Step 2: Compare options

    Only To group related data logically inside another structure correctly describes grouping data logically. Other options describe unrelated concepts.
  3. Final Answer:

    To group related data logically inside another structure -> Option D
  4. Quick Check:

    Nested structures = group data logically [OK]
Hint: Think of nested structures as folders inside folders [OK]
Common Mistakes:
  • Confusing nested structures with inheritance
  • Thinking nested structures increase structure size unnecessarily
  • Assuming nested structures add functions
2. Which of the following is the correct syntax to access member city inside a nested structure address within person?
easy
A. person->address.city
B. person.address.city
C. person.address->city
D. person->address->city

Solution

  1. Step 1: Identify access operator for nested structures

    When accessing members of nested structures by variable (not pointer), use dot operator for each level.
  2. Step 2: Apply to given example

    Since person is a variable, access address with dot, then city with dot: person.address.city.
  3. Final Answer:

    person.address.city -> Option B
  4. Quick Check:

    Use dot for nested struct members when variable, not pointer [OK]
Hint: Use dot operator for nested struct members if not pointers [OK]
Common Mistakes:
  • Using arrow operator (->) when variable is not a pointer
  • Mixing dot and arrow operators incorrectly
  • Trying to access nested member directly without parent
3. What will be the output of the following code?
struct Date {
  int day;
  int month;
};
struct Event {
  char name[10];
  Date date;
};

int main() {
  Event e = {"Party", {25, 12}};
  std::cout << e.name << " on " << e.date.day << "/" << e.date.month << std::endl;
  return 0;
}
medium
A. Party on 25/12
B. Party on 12/25
C. Compilation error
D. Party on 0/0

Solution

  1. Step 1: Understand structure initialization

    Event e is initialized with name "Party" and Date with day=25, month=12.
  2. Step 2: Trace output statement

    Output prints e.name, then e.date.day (25), then e.date.month (12) separated by slashes.
  3. Final Answer:

    Party on 25/12 -> Option A
  4. Quick Check:

    Nested struct initialized correctly = Party on 25/12 [OK]
Hint: Match initialization order with nested structure fields [OK]
Common Mistakes:
  • Swapping day and month values
  • Assuming compilation error due to nested struct
  • Forgetting to use dot operator to access nested members
4. Identify the error in the following code snippet:
struct Point {
  int x, y;
};
struct Rectangle {
  Point topLeft;
  Point bottomRight;
};

int main() {
  Rectangle rect;
  rect.topLeft->x = 0;
  rect.topLeft->y = 10;
  return 0;
}
medium
A. Using arrow operator (->) on a non-pointer structure variable
B. Missing semicolon after struct definitions
C. Incorrect member names in Rectangle
D. Rectangle struct cannot contain Point structs

Solution

  1. Step 1: Check member access operators

    rect.topLeft is a structure variable, not a pointer, so dot operator (.) must be used, not arrow (->).
  2. Step 2: Identify error cause

    Using -> on non-pointer causes compilation error.
  3. Final Answer:

    Using arrow operator (->) on a non-pointer structure variable -> Option A
  4. Quick Check:

    Use dot for struct variables, arrow for pointers [OK]
Hint: Use dot for struct variables, arrow only for pointers [OK]
Common Mistakes:
  • Confusing dot and arrow operators
  • Assuming arrow works for all struct members
  • Ignoring compiler error messages
5. Given the following nested structures, how would you write a function printBookInfo that prints the book title and the author's city?
struct Author {
  char name[50];
  char city[30];
};
struct Book {
  char title[100];
  Author author;
};
hard
A. void printBookInfo(Book b) { std::cout << b.title << ", " << b.author->city << std::endl; }
B. void printBookInfo(Book b) { std::cout << b.title << ", " << b.city << std::endl; }
C. void printBookInfo(Book b) { std::cout << b.title << ", " << b.author.city << std::endl; }
D. void printBookInfo(Book b) { std::cout << b->title << ", " << b->author.city << std::endl; }

Solution

  1. Step 1: Understand structure member access

    Book variable b has member title and nested structure author. Access author.city with dot operator.
  2. Step 2: Check each option

    void printBookInfo(Book b) { std::cout << b.title << ", " << b.author.city << std::endl; } correctly uses b.title and b.author.city with dot operators. Others misuse arrow or omit author.
  3. Final Answer:

    void printBookInfo(Book b) { std::cout << b.title << ", " << b.author.city << std::endl; } -> Option C
  4. Quick Check:

    Access nested struct members with dot: b.author.city [OK]
Hint: Use dot operator for nested struct members in function parameters [OK]
Common Mistakes:
  • Using arrow operator on non-pointer parameter
  • Skipping nested struct name when accessing member
  • Using pointer syntax incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard