A structure in C++ is a user-defined data type that groups related variables of different types under one name. It helps organize data like a real-life object with multiple properties.
Click to reveal answer
beginner
How do you define a simple structure named Person with name and age?
You define it like this:
struct Person { std::string name; int age; };
Click to reveal answer
beginner
How do you create a variable of a structure type and access its members?
First, declare a variable of the structure type, then use the dot . operator to access members. Example:
Person p; p.name = "Alice"; p.age = 30;
Click to reveal answer
intermediate
Can structures in C++ have functions inside them?
Yes! Structures in C++ can have functions (called member functions) just like classes. This lets you group data and behavior together.
Click to reveal answer
intermediate
What is the difference between a structure and a class in C++?
The main difference is default access: - In a structure, members are public by default. - In a class, members are private by default. Otherwise, they are very similar.
Click to reveal answer
How do you define a structure in C++?
AUsing the keyword <code>class</code> only
BUsing the keyword <code>function</code>
CUsing the keyword <code>define</code>
DUsing the keyword <code>struct</code> followed by the structure name and members
✗ Incorrect
Structures are defined using the struct keyword followed by the name and member variables.
What operator is used to access members of a structure variable?
AColon (:) operator
BDot (.) operator
CComma (,) operator
DArrow (->) operator
✗ Incorrect
The dot operator is used to access members of a structure variable.
What is the default access level of members in a C++ structure?
APublic
BProtected
CPrivate
DPackage
✗ Incorrect
Members of a structure are public by default.
Can a structure in C++ contain functions?
AOnly if declared as <code>class</code>
BNo, structures can only have variables
CYes, structures can have member functions
DOnly in C, not in C++
✗ Incorrect
In C++, structures can have member functions just like classes.
Which of the following is a correct way to declare a structure variable?
ABoth A and B
Bstruct Person p;
CPerson p;
DNone of the above
✗ Incorrect
Both Person p; and struct Person p; are valid in C++.
Explain how to define a structure in C++ and create a variable of that structure.
Think about grouping related data under one name and how to use it.
You got /5 concepts.
Describe the differences and similarities between structures and classes in C++.
Focus on access and behavior.
You got /4 concepts.
Practice
(1/5)
1. What is the purpose of a struct in C++?
easy
A. To group related variables under one name
B. To create a loop structure
C. To define a function
D. To allocate memory dynamically
Solution
Step 1: Understand the role of struct
A struct is used to group different variables together under one name, making it easier to manage related data.
Step 2: Compare with other options
Options A, C, and D describe loops, functions, and memory allocation, which are unrelated to struct.
Final Answer:
To group related variables under one name -> Option A
Quick Check:
Struct groups data = B [OK]
Hint: Remember: struct bundles variables like a box [OK]
Common Mistakes:
Confusing struct with functions
Thinking struct creates loops
Mixing struct with dynamic memory
2. Which of the following is the correct way to define a structure named Person with members name (string) and age (int)?
easy
A. struct Person = { string name; int age; };
B. struct Person ( string name; int age; );
C. struct Person { string name; int age; };
D. struct Person: string name; int age;
Solution
Step 1: Recall correct struct syntax
In C++, a struct is defined using curly braces { } with members inside, ending with a semicolon.
Step 2: Check each option
struct Person { string name; int age; }; uses correct syntax with braces and semicolon. Options B, C, and D use incorrect symbols or missing braces.
Final Answer:
struct Person { string name; int age; }; -> Option C
Quick Check:
Struct uses braces and semicolon = A [OK]
Hint: Use braces { } and end with semicolon ; [OK]
Common Mistakes:
Using parentheses instead of braces
Omitting the semicolon after struct
Using equal sign or colon wrongly
3. What will be the output of this code?
struct Point { int x; int y; };
int main() {
Point p = {3, 4};
std::cout << p.x << "," << p.y << std::endl;
return 0;
}
medium
A. 3,4
B. 4,3
C. x,y
D. Compilation error
Solution
Step 1: Understand struct initialization
The struct Point is initialized with {3, 4}, so p.x = 3 and p.y = 4.
Step 2: Check output statement
The code prints p.x then a comma, then p.y, so output is "3,4".
Final Answer:
3,4 -> Option A
Quick Check:
p.x=3 and p.y=4 prints 3,4 [OK]
Hint: Struct members print in order assigned [OK]
Common Mistakes:
Swapping member values
Expecting member names to print
Thinking initialization causes error
4. Identify the error in this structure definition:
struct Car {
string model
int year;
};
medium
A. Cannot have string type in struct
B. Missing semicolon after Car struct
C. Wrong keyword used instead of struct
D. Missing semicolon after model member
Solution
Step 1: Check member declarations
Each member must end with a semicolon. The model member is missing a semicolon.
Step 2: Verify struct ending
The struct ends with a semicolon, which is correct. The keyword struct is correct. String type is allowed.
Final Answer:
Missing semicolon after model member -> Option D
Quick Check:
Each member needs ; = A [OK]
Hint: Check each member line ends with ; [OK]
Common Mistakes:
Forgetting semicolon after members
Confusing struct end semicolon with member semicolon
Thinking string is invalid type
5. Given the structure:
struct Employee {
string name;
int id;
double salary;
};
Which code correctly creates an Employee variable and sets the salary to 50000.0?
hard
A. Employee e; e.salary = 50000.0;
B. All of the above
C. Employee e = {"John", 101, 50000.0};
D. Employee e = {"John", 101}; e.salary = 50000.0;
Solution
Step 1: Check each option for valid struct usage
Employee e; e.salary = 50000.0; creates an Employee and sets salary directly, which is valid.
Step 2: Analyze options B and C
Employee e = {"John", 101}; e.salary = 50000.0; initializes name and id, then sets salary separately, valid. Employee e = {"John", 101, 50000.0}; initializes all members at once, also valid.
Step 3: Confirm all options are correct
All three ways correctly create and set salary.
Final Answer:
All of the above -> Option B
Quick Check:
Multiple valid ways to set struct members = D [OK]
Hint: Struct members can be set during or after creation [OK]
Common Mistakes:
Thinking partial initialization is invalid
Assuming only one correct way to assign members
Forgetting to set salary after partial init
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.