0
0
Blockchain / Solidityprogramming~10 mins

Why functions define contract behavior in Blockchain / Solidity - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a function named setOwner.

Blockchain / Solidity
function [1](address newOwner) public {
    owner = newOwner;
}
Drag options to blanks, or click blank then click option'
AsetOwner
BgetOwner
CownerSet
DchangeOwner
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function name that does not match the action, like 'getOwner' which suggests reading, not setting.
2fill in blank
medium

Complete the code to declare a function that returns the owner address.

Blockchain / Solidity
function [1]() public view returns (address) {
    return owner;
}
Drag options to blanks, or click blank then click option'
AownerAddress
BsetOwner
Cowner
DgetOwner
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function name that implies changing state, like 'setOwner', when the function only returns data.
3fill in blank
hard

Fix the error in the function declaration to make it a valid Solidity function.

Blockchain / Solidity
function setOwner(address newOwner) [1] {
    owner = newOwner;
}
Drag options to blanks, or click blank then click option'
Apublic
Breturns (address)
Cprivate
Dview
Attempts:
3 left
💡 Hint
Common Mistakes
Using view or returns incorrectly in a function that modifies state.
4fill in blank
hard

Fill both blanks to complete a function that checks if the caller is the owner before changing it.

Blockchain / Solidity
function [1](address newOwner) public {
    require(msg.sender [2] owner, "Not owner");
    owner = newOwner;
}
Drag options to blanks, or click blank then click option'
AsetOwner
B==
C!=
DtransferOwner
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' in the require condition, which would reject the owner.
5fill in blank
hard

Fill all three blanks to create a function that emits an event when the owner changes.

Blockchain / Solidity
event OwnerChanged(address indexed oldOwner, address indexed newOwner);

function [1](address newOwner) public {
    require(msg.sender == owner, "Not owner");
    address [2] = owner;
    owner = newOwner;
    emit [3](oldOwner, newOwner);
}
Drag options to blanks, or click blank then click option'
AchangeOwner
BoldOwner
COwnerChanged
DsetOwner
Attempts:
3 left
💡 Hint
Common Mistakes
Not storing the old owner before changing it, causing incorrect event data.