📖 Scenario: You are creating a simple smart contract to store and display different types of data commonly used in blockchain development.
🎯 Goal: Build a smart contract that declares variables of types uint, int, bool, address, and string, assigns values to them, and then outputs these values.
📋 What You'll Learn
Declare a uint variable named myUint with value 100
Declare an int variable named myInt with value -50
Declare a bool variable named myBool with value true
Declare an address variable named myAddress with value 0x1234567890123456789012345678901234567890
Declare a string variable named myString with value "Hello Blockchain"
Create a function getData that returns all these variables
💡 Why This Matters
🌍 Real World
Smart contracts use these basic data types to store and manage blockchain data like balances, flags, addresses, and messages.
💼 Career
Understanding these data types is essential for blockchain developers to write secure and efficient smart contracts.
Progress0 / 4 steps
1
Declare basic data type variables
Declare a uint variable called myUint and set it to 100. Declare an int variable called myInt and set it to -50 inside a Solidity contract named DataTypesDemo.
Blockchain / Solidity
Hint
Use uint for positive numbers and int for numbers that can be negative.
2
Add bool, address, and string variables
Inside the DataTypesDemo contract, declare a bool variable called myBool set to true, an address variable called myAddress set to 0x1234567890123456789012345678901234567890, and a string variable called myString set to "Hello Blockchain".
Blockchain / Solidity
Hint
Remember to use double quotes for strings and the exact address format for address.
3
Create a function to return all variables
Add a public view function called getData inside DataTypesDemo that returns myUint, myInt, myBool, myAddress, and myString in that order.
Blockchain / Solidity
Hint
Use public view and specify the return types in the function signature.
4
Output the stored data
Write a simple script or test code that calls the getData function and prints the returned values in the order: myUint, myInt, myBool, myAddress, myString. For this step, just write a console.log statement inside a JavaScript test function that logs the returned tuple.
Blockchain / Solidity
Hint
Use console.log and convert numbers to strings if needed before printing.
Practice
(1/5)
1. Which data type should you use in Solidity to store a number that can only be zero or positive?
easy
A. bool
B. uint
C. int
D. address
Solution
Step 1: Understand the difference between uint and int
uint stands for unsigned integer, meaning it can only hold zero or positive numbers. int can hold both positive and negative numbers.
Step 2: Identify the correct type for only zero or positive numbers
Since the question asks for numbers that are zero or positive only, uint is the correct choice.
Final Answer:
uint -> Option B
Quick Check:
Positive numbers = uint [OK]
Hint: Use uint for non-negative numbers only [OK]
Common Mistakes:
Choosing int for positive-only numbers
Confusing bool with numeric types
Using address for numbers
2. Which of the following is the correct way to declare a boolean variable named isActive in Solidity?
easy
A. bool isActive = 0;
B. bool isActive = 1;
C. bool isActive = "true";
D. bool isActive = true;
Solution
Step 1: Recall the correct boolean values in Solidity
Boolean variables can only be assigned true or false, not numbers or strings.
Step 2: Check each option for correct boolean assignment
bool isActive = true; assigns true correctly. Options B and D use numbers, and C uses a string, which are invalid for bool.
Final Answer:
bool isActive = true; -> Option D
Quick Check:
Boolean = true or false [OK]
Hint: Assign true or false to bool variables [OK]
Common Mistakes:
Using 1 or 0 instead of true/false
Assigning strings like "true"
Missing semicolon at the end
3. What will be the output of this Solidity code snippet?
int x = -5;
uint y = 10;
bool flag = (x < 0);
string memory text = "Hello";
address addr = 0x1234567890123456789012345678901234567890;
// Output values
x
flag
text
addr
medium
A. Compilation error due to address format
B. 5, false, Hello, 0x1234567890123456789012345678901234567890
C. -5, true, Hello, 0x1234567890123456789012345678901234567890
D. -5, false, "Hello", 0x0
Solution
Step 1: Analyze each variable's value
x is -5 (signed int), flag is true because x < 0, text is "Hello", and addr is a valid address.
Step 2: Confirm output matches variable values
-5, true, Hello, 0x1234567890123456789012345678901234567890 correctly lists all values as expected. 5, false, Hello, 0x1234567890123456789012345678901234567890 incorrectly changes x and flag. -5, false, "Hello", 0x0 has wrong flag and addr. Compilation error due to address format is wrong because the address format is valid.
Final Answer:
-5, true, Hello, 0x1234567890123456789012345678901234567890 -> Option C
Quick Check:
Variable values printed as is [OK]
Hint: Check variable types and values carefully [OK]
Common Mistakes:
Assuming uint can hold negative values
Confusing string quotes in output
Thinking address format is invalid
4. Identify the error in this Solidity variable declaration:
address myAddress = "0xAbC1234567890";
medium
A. Address must be assigned without quotes
B. Address must be declared as string
C. Address cannot start with 0x
D. Address must be declared as bool
Solution
Step 1: Understand address assignment rules
In Solidity, addresses are assigned as hexadecimal literals without quotes, not as strings.
Step 2: Identify the error in the code
The code uses quotes around the address, making it a string, which is invalid for address type.
Final Answer:
Address must be assigned without quotes -> Option A
Quick Check:
Address literals have no quotes [OK]
Hint: Assign addresses without quotes to avoid errors [OK]
Common Mistakes:
Using quotes around addresses
Declaring address as string
Removing 0x prefix from address
5. You want to store user information in a Solidity contract with these fields: user ID (positive number), username (text), active status (true/false), and wallet address. Which data types should you use for these fields?
hard
A. uint for user ID, string for username, bool for active status, address for wallet
B. int for user ID, string for username, uint for active status, address for wallet
C. uint for user ID, address for username, bool for active status, string for wallet
D. string for user ID, string for username, bool for active status, address for wallet
Solution
Step 1: Match each field to the correct data type
User ID is a positive number, so uint fits. Username is text, so string. Active status is true/false, so bool. Wallet address is a blockchain address, so address.
Step 2: Verify options against these matches
uint for user ID, string for username, bool for active status, address for wallet matches all fields correctly. Other options mix types incorrectly, like using int for user ID or swapping string and address.
Final Answer:
uint for user ID, string for username, bool for active status, address for wallet -> Option A
Quick Check:
Match field types carefully [OK]
Hint: Match data types to field meaning exactly [OK]
Common Mistakes:
Using int for positive-only IDs
Confusing string and address
Using uint for boolean values
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.