A struct is a way to group related data together into one unit, like putting different pieces of information about a person into one folder.
Click to reveal answer
beginner
How do you define a struct in Solidity?
You use the struct keyword followed by the struct name and curly braces containing the variables, for example: struct Person { string name; uint age; }
Click to reveal answer
intermediate
Can structs contain other structs inside them?
Yes, structs can have other structs as members, allowing you to build complex data models by nesting structs.
Click to reveal answer
beginner
How do you create and use a struct variable in Solidity?
You declare a variable of the struct type and assign values to its fields, like: Person memory p = Person("Alice", 30);
Click to reveal answer
beginner
Why are structs useful in blockchain smart contracts?
Structs help organize data clearly and efficiently, making contracts easier to read and manage, especially when handling complex information.
Click to reveal answer
What keyword is used to define a struct in Solidity?
Astruct
Bclass
Cobject
Drecord
✗ Incorrect
The keyword struct is used to define a struct in Solidity.
Which of these can a struct contain?
AOther structs
BOnly simple data types like uint and string
CFunctions
DEvents
✗ Incorrect
Structs can contain other structs as members, allowing nested data structures.
How do you create a struct variable in Solidity?
ABy calling a constructor function
BBy importing from another contract
CBy declaring a variable of the struct type and assigning values
DBy using the new keyword
✗ Incorrect
You declare a variable of the struct type and assign values directly.
Why use structs in smart contracts?
ATo speed up contract execution
BTo organize related data together
CTo create user interfaces
DTo store contract code
✗ Incorrect
Structs organize related data clearly and efficiently.
Which of these is NOT a valid member type inside a struct?
Auint
Bstring
Caddress
Dmapping
✗ Incorrect
Mappings cannot be members of structs in Solidity.
Explain what a struct is and why it is useful in blockchain smart contracts.
Think about how you keep related information together in real life.
You got /4 concepts.
Describe how to define and use a struct in Solidity with an example.
Imagine creating a new type to hold multiple pieces of information.
You got /5 concepts.
Practice
(1/5)
1.
What is the main purpose of using structs in blockchain programming?
easy
A. To perform mathematical calculations faster
B. To group related data together under one name
C. To create loops for repeating tasks
D. To store only single values like integers
Solution
Step 1: Understand what structs do
Structs are used to group different pieces of related data into one unit.
Step 2: Compare options
Only To group related data together under one name correctly describes grouping related data. Other options describe unrelated tasks.
Final Answer:
To group related data together under one name -> Option B
Quick Check:
Structs = Group related data [OK]
Hint: Think of structs as folders holding related info [OK]
Common Mistakes:
Confusing structs with loops or calculations
Thinking structs store only single values
Assuming structs speed up math operations
2.
Which of the following is the correct syntax to define a struct named Person with fields name (string) and age (uint)?
?
easy
A. struct Person(string name, uint age);
B. struct Person = (string name, uint age);
C. Person struct { string name; uint age; }
D. struct Person { string name; uint age; }
Solution
Step 1: Recall struct syntax in blockchain languages
Structs are defined with the keyword struct, followed by the name and curly braces containing fields with types and names.
Step 2: Check each option
struct Person { string name; uint age; } matches the correct syntax. Options B, C, and D use incorrect punctuation or order.
Final Answer:
struct Person { string name; uint age; } -> Option D