State variables are declared by specifying the type followed by the variable name and a semicolon.
Step 2: Check each option
uint count; correctly declares uint count;. uint count = 0 misses the semicolon. Options B and D use incorrect syntax.
Final Answer:
uint count; -> Option C
Quick Check:
Variable declaration = type name; [OK]
Hint: Remember: type then variable name, end with semicolon [OK]
Common Mistakes:
Omitting semicolon at the end
Using 'var' keyword which is not valid in Solidity
Reversing type and variable name
3.
What will be the output of the following Solidity function when called?
contract Test {
uint number = 5;
function getNumber() public view returns (uint) {
return number + 3;
}
}
medium
A. 5
B. 8
C. 3
D. Compilation error
Solution
Step 1: Understand the variable and function
The state variable number is set to 5. The function returns number + 3.
Step 2: Calculate the return value
5 + 3 equals 8, so the function returns 8.
Final Answer:
8 -> Option B
Quick Check:
5 + 3 = 8 [OK]
Hint: Add variable value and constant inside return [OK]
Common Mistakes:
Confusing variable value with function output
Assuming function returns variable directly without addition
Thinking there is a syntax error
4.
Identify the error in the following Solidity contract snippet:
contract Counter {
uint count
function increment() public {
count += 1;
}
}
medium
A. Contract name should be lowercase
B. Function increment should be private
C. Variable count should be declared inside the function
D. Missing semicolon after uint count
Solution
Step 1: Check variable declaration syntax
The line uint count is missing a semicolon at the end, which is required in Solidity.
Step 2: Verify other parts
Function visibility as public is valid. Variables are correctly declared outside functions. Contract names can be uppercase.
Final Answer:
Missing semicolon after uint count -> Option D
Quick Check:
Semicolon missing = syntax error [OK]
Hint: Check for missing semicolons after variable declarations [OK]
Common Mistakes:
Forgetting semicolon after variable declaration
Thinking function visibility must be private
Assuming contract names must be lowercase
5.
You want to create a contract that stores a person's name and age, and has a function to update the age. Which of the following contract structures correctly implements this?
contract Person {
string name;
uint age;
function setAge(uint newAge) public {
age = newAge;
}
}
hard
A. Correct: variables declared and function updates age properly
B. Incorrect: missing visibility for variables
C. Incorrect: function should return the new age
D. Incorrect: variables should be declared inside the function
Solution
Step 1: Check variable declarations
Variables name and age are declared at contract level, which is correct for storing data.
Step 2: Check function to update age
The function setAge takes a parameter and assigns it to age, correctly updating the state variable.
Final Answer:
Correct: variables declared and function updates age properly -> Option A
Quick Check:
State variables + update function = correct contract [OK]
Hint: State variables outside, update functions inside contract [OK]
Common Mistakes:
Declaring variables inside functions instead of contract
Expecting update functions to return values unnecessarily
Forgetting to declare variables at contract level
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.