Challenge - 5 Problems
Structure Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of accessing structure members with dot operator
What is the output of this C++ code snippet?
C++
struct Point {
int x;
int y;
};
int main() {
Point p = {3, 4};
std::cout << p.x << "," << p.y << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Remember the dot operator accesses members directly by name.
✗ Incorrect
The structure Point has members x and y. The code initializes p.x=3 and p.y=4, so printing p.x and p.y outputs '3,4'.
❓ Predict Output
intermediate2:00remaining
Output when accessing structure members via pointer
What will this C++ program print?
C++
struct Data {
int value;
};
int main() {
Data d = {10};
Data* ptr = &d;
std::cout << ptr->value << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Use the arrow operator to access members through pointers.
✗ Incorrect
The pointer ptr points to d, which has value 10. Using ptr->value accesses that member correctly.
❓ Predict Output
advanced2:00remaining
Output of nested structure member access
What is the output of this code?
C++
struct Inner {
int a;
};
struct Outer {
Inner inner;
};
int main() {
Outer o = {{5}};
std::cout << o.inner.a << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Access nested members by chaining dot operators.
✗ Incorrect
The Outer structure contains Inner named inner. The value a is initialized to 5 and printed.
❓ Predict Output
advanced2:00remaining
Output when accessing structure members with pointer and dot
What happens when you run this code?
C++
struct Item {
int id;
};
int main() {
Item item = {7};
Item* p = &item;
std::cout << (*p).id << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Dereference pointer first, then use dot operator.
✗ Incorrect
(*p).id accesses the id member of the object pointed to by p, which is 7.
🧠 Conceptual
expert2:00remaining
Error type when accessing structure member incorrectly
What error will this code produce?
C++
struct Sample {
int x;
};
int main() {
Sample* s = nullptr;
std::cout << s->x << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Accessing member through null pointer causes crash.
✗ Incorrect
Dereferencing a null pointer to access a member causes a segmentation fault at runtime.