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 structure member access
What is the output of this C++ code?
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
Add the values of x and y members of the structure.
✗ Incorrect
The structure Point has members x=3 and y=4. Adding them gives 7.
❓ Predict Output
intermediate2:00remaining
Size of structure with padding
What is the size of this structure on a typical 64-bit system?
C++
struct Data {
char a;
int b;
char c;
};
int main() {
std::cout << sizeof(Data) << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Consider padding added for alignment between members.
✗ Incorrect
The compiler adds padding to align int on 4 bytes. The total size becomes 12 bytes.
🔧 Debug
advanced2:00remaining
Identify the error in structure initialization
What error will this code produce?
C++
#include <iostream>
#include <string>
struct Person {
std::string name;
int age;
};
int main() {
Person p = {"Alice"};
std::cout << p.name << " " << p.age << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
All members must be initialized or have default values.
✗ Incorrect
The code misses initializing 'age', causing a compilation error in strict initialization.
🧠 Conceptual
advanced2:00remaining
Effect of 'typedef' with structures
What does the following code do?
C++
#include <iostream>
typedef struct {
int id;
float score;
} Record;
int main() {
Record r = {101, 95.5};
std::cout << r.id << " " << r.score << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
typedef creates a new name for the unnamed structure.
✗ Incorrect
The typedef creates an alias 'Record' for the unnamed struct, allowing easy use.
❓ Predict Output
expert2:00remaining
Output of nested structure member access
What is the output of this code?
C++
struct Engine {
int horsepower;
};
struct Car {
Engine engine;
int year;
};
int main() {
Car c = {{250}, 2020};
std::cout << c.engine.horsepower << " " << c.year << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Nested structures can be initialized with nested braces.
✗ Incorrect
The nested structure Engine is initialized with 250 horsepower, and year is 2020.