Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1.

Why do functions define contract behavior in blockchain programming?

easy
A. They specify what actions the contract can perform
B. They store data permanently on the blockchain
C. They create new blockchain networks
D. They control the blockchain consensus mechanism

Solution

  1. Step 1: Understand contract behavior

    Contract behavior means what the contract can do or how it responds to calls.
  2. Step 2: Role of functions in contracts

    Functions define specific actions users can trigger, shaping contract behavior.
  3. Final Answer:

    They specify what actions the contract can perform -> Option A
  4. Quick Check:

    Functions = Define contract actions [OK]
Hint: Functions show what contract can do [OK]
Common Mistakes:
  • Confusing functions with data storage
  • Thinking functions create blockchains
  • Mixing contract behavior with consensus
2.

Which of the following is the correct syntax to define a function named transfer in a Solidity contract?

easy
A. function transfer[] {}
B. def transfer() {}
C. func transfer() {}
D. function transfer() public {}

Solution

  1. Step 1: Identify Solidity function syntax

    Solidity uses the keyword function followed by the name and parentheses.
  2. Step 2: Check each option

    function transfer() public {} matches Solidity syntax; others use Python or invalid forms.
  3. Final Answer:

    function transfer() public {} -> Option D
  4. Quick Check:

    Solidity functions start with 'function' [OK]
Hint: Solidity functions start with 'function' keyword [OK]
Common Mistakes:
  • Using Python or other language syntax
  • Missing parentheses after function name
  • Using brackets [] instead of parentheses ()
3.

Consider this Solidity function inside a contract:

function getBalance() public view returns (uint) {
return address(this).balance;
}

What does calling getBalance() return?

medium
A. The total supply of tokens
B. The balance of the caller's address
C. The balance of the contract's address in wei
D. The number of transactions made

Solution

  1. Step 1: Analyze the function code

    The function returns address(this).balance, which is the contract's own balance.
  2. Step 2: Understand what address(this).balance means

    It gives the amount of ether held by the contract in wei (smallest ether unit).
  3. Final Answer:

    The balance of the contract's address in wei -> Option C
  4. Quick Check:

    address(this).balance = contract's ether balance [OK]
Hint: address(this).balance returns contract's ether balance [OK]
Common Mistakes:
  • Thinking it returns caller's balance
  • Confusing balance with token supply
  • Assuming it counts transactions
4.

Identify the error in this Solidity function that should transfer ether:

function sendEther(address recipient, uint amount) public {
recipient.transfer(amount);
}

medium
A. Cannot call transfer on address type directly
B. Missing payable keyword on function
C. Amount should be in wei, not uint
D. Function must be private

Solution

  1. Step 1: Examine the transfer call

    recipient.transfer(amount) where recipient is typed as address.
  2. Step 2: Solidity ether transfer rules

    transfer is a method only on address payable. Must use payable(recipient).transfer(amount).
  3. Final Answer:

    Cannot call transfer on address type directly -> Option A
  4. Quick Check:

    transfer requires address payable [OK]
Hint: Solidity transfer requires payable address [OK]
Common Mistakes:
  • Assuming transfer works without payable
  • Confusing address types for transfer
  • Thinking function visibility causes error
5.

You want to create a function in a smart contract that only allows the owner to change a stored value. Which function behavior best defines this contract logic?

hard
A. A private function that anyone can call
B. A public function with a check that only the owner can update the value
C. A public function that updates the value without restrictions
D. A function that automatically updates value every block

Solution

  1. Step 1: Understand access control in contract functions

    To restrict actions, functions must check caller identity (e.g., owner).
  2. Step 2: Identify function behavior that enforces owner-only update

    A public function with an owner check allows controlled access while being callable.
  3. Final Answer:

    A public function with a check that only the owner can update the value -> Option B
  4. Quick Check:

    Owner check in function controls access [OK]
Hint: Use owner check inside public function [OK]
Common Mistakes:
  • Making function private but callable by anyone
  • Not restricting access in public functions
  • Assuming automatic updates enforce ownership
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard