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 code using structures for grouping data
What is the output of this C code that uses a structure to group related data?
C
#include <stdio.h>
struct Point {
int x;
int y;
};
int main() {
struct Point p1 = {3, 4};
printf("x = %d, y = %d\n", p1.x, p1.y);
return 0;
}Attempts:
2 left
💡 Hint
Look at how the structure fields are initialized and accessed.
✗ Incorrect
The structure Point groups two integers x and y. The variable p1 is initialized with x=3 and y=4. Printing p1.x and p1.y outputs 'x = 3, y = 4'.
🧠 Conceptual
intermediate1:30remaining
Why use structures instead of separate variables?
Why are structures useful in C programming compared to using separate variables for related data?
Attempts:
2 left
💡 Hint
Think about how grouping data helps in real life, like putting all your keys on one keyring.
✗ Incorrect
Structures group related data together under one name, which helps keep code organized and easier to read and maintain. They do not automatically sort data or prevent changes.
🔧 Debug
advanced2:00remaining
Identify the error in structure usage
What error will this code produce when compiled?
C
#include <stdio.h>
#include <string.h>
struct Person {
char name[20];
int age;
};
int main() {
struct Person p1;
p1.name = "Alice";
p1.age = 30;
printf("Name: %s, Age: %d\n", p1.name, p1.age);
return 0;
}Attempts:
2 left
💡 Hint
In C, arrays cannot be assigned directly with = operator.
✗ Incorrect
Arrays in C cannot be assigned using = after declaration. The line p1.name = "Alice" causes a compilation error. To copy a string into an array, functions like strcpy() must be used.
❓ Predict Output
advanced2:00remaining
Output of nested structures
What is the output of this code using nested structures?
C
#include <stdio.h>
struct Date {
int day;
int month;
int year;
};
struct Event {
char title[20];
struct Date date;
};
int main() {
struct Event e = {"Meeting", {15, 8, 2024}};
printf("%s on %d/%d/%d\n", e.title, e.date.day, e.date.month, e.date.year);
return 0;
}Attempts:
2 left
💡 Hint
Look at how the nested structure is initialized inside the outer structure.
✗ Incorrect
The Event structure contains a Date structure. The initialization uses nested braces to set the date fields correctly. The output prints the title and date in day/month/year format.
🧠 Conceptual
expert2:30remaining
Why structures improve program design
Which statement best explains why structures are important for program design in C?
Attempts:
2 left
💡 Hint
Think about how grouping related data helps programmers keep code clear and organized.
✗ Incorrect
Structures group different data types into one unit, which helps programmers organize data logically and maintain code more easily. They do not optimize memory automatically, enforce privacy, or replace functions.