Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Event declaration in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Event Declaration Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Solidity event emission?

Consider the following Solidity contract snippet. What will be the output in the transaction logs after calling setValue(42)?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Test {
    event ValueChanged(address indexed author, uint oldValue, uint newValue);
    uint public value;

    function setValue(uint _value) public {
        uint old = value;
        value = _value;
        emit ValueChanged(msg.sender, old, _value);
    }
}
AAn event ValueChanged is logged with the caller's address, oldValue as 0, and newValue as 42
BAn event ValueChanged is logged with the caller's address, oldValue as 42, and newValue as 0
CNo event is logged because events require explicit subscription
DA compilation error occurs because events cannot have indexed parameters
Attempts:
2 left
💡 Hint

Remember that emit triggers the event and oldValue is the previous stored value.

🧠 Conceptual
intermediate
1:30remaining
Which statement about Solidity event declaration is correct?

Choose the correct statement about declaring events in Solidity.

AEvents cannot have parameters marked as indexed
BEvents are only used for internal contract state changes and not visible externally
CEvents are stored permanently on-chain and can be accessed by smart contracts
DEvents can have up to three indexed parameters to allow filtering in logs
Attempts:
2 left
💡 Hint

Think about how events help external tools filter logs.

🔧 Debug
advanced
2:00remaining
Why does this Solidity event declaration cause a compilation error?

Examine the following event declaration. Why does it fail to compile?

Blockchain / Solidity
event Transfer(address from, address to, uint amount) indexed;
AThe event name cannot be <code>Transfer</code> as it is reserved
BThe <code>indexed</code> keyword cannot be applied to the entire event declaration
CThe event must have at least one indexed parameter
DThe event parameters must be declared as <code>memory</code> or <code>calldata</code>
Attempts:
2 left
💡 Hint

Check where the indexed keyword is allowed in event declarations.

❓ Predict Output
advanced
2:00remaining
What is the output of this event emission with multiple indexed parameters?

Given this Solidity contract, what will be the content of the event log after calling recordAction(7, 3)?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Logger {
    event ActionRecorded(address indexed user, uint indexed actionId, uint value);

    function recordAction(uint actionId, uint value) public {
        emit ActionRecorded(msg.sender, actionId, value);
    }
}
AThe event log contains user address, actionId as 7, and value as 3 with user and actionId indexed
BThe event log contains user address, actionId as 3, and value as 7 with no indexed parameters
CThe event log contains only the value 3 because indexed parameters are not stored
DThe event log is empty because events with multiple indexed parameters are not allowed
Attempts:
2 left
💡 Hint

Remember that indexed parameters appear in topics and non-indexed in data.

🚀 Application
expert
2:30remaining
How many indexed parameters can this Solidity event legally have?

Consider this event declaration:

event DataEvent(uint indexed id, address indexed sender, string message, uint indexed timestamp);

How many parameters are indexed, and is this declaration valid?

ATwo parameters are indexed; Solidity limits indexed parameters to two per event
BFour parameters are indexed; all parameters can be indexed in events
CThree parameters are indexed; this is valid because Solidity allows up to three indexed parameters per event
DThis declaration is invalid because string parameters cannot be indexed
Attempts:
2 left
💡 Hint

Recall the maximum number of indexed parameters allowed and restrictions on data types.

Practice

(1/5)
1. What is the purpose of declaring an event in a blockchain smart contract?
easy
A. To create a new token within the contract
B. To store user data permanently on the blockchain
C. To log important actions so external apps can track them
D. To execute a function automatically on contract deployment

Solution

  1. Step 1: Understand event declaration purpose

    Events are declared to notify external apps about important contract actions.
  2. Step 2: Compare options with event role

    Only To log important actions so external apps can track them matches the role of events as logs for external tracking.
  3. Final Answer:

    To log important actions so external apps can track them -> Option C
  4. Quick Check:

    Events = Logs for external apps [OK]
Hint: Events are for logging actions, not storing data [OK]
Common Mistakes:
  • Confusing events with storage variables
  • Thinking events execute code automatically
  • Assuming events create tokens
2. Which of the following is the correct syntax to declare an event named Transfer with two indexed address parameters from and to in Solidity?
easy
A. event Transfer(address from indexed, address to indexed);
B. event Transfer(address indexed from, address indexed to);
C. event Transfer(address from, address to) indexed;
D. event Transfer(indexed address from, indexed address to);

Solution

  1. Step 1: Recall Solidity event syntax

    Indexed parameters are declared as type indexed name inside parentheses.
  2. Step 2: Check each option's syntax

    Only event Transfer(address indexed from, address indexed to); correctly places indexed after the type and before the parameter name.
  3. Final Answer:

    event Transfer(address indexed from, address indexed to); -> Option B
  4. Quick Check:

    Indexed parameters = type indexed name [OK]
Hint: Put 'indexed' right after the type, before the name [OK]
Common Mistakes:
  • Placing 'indexed' after the parameter name
  • Adding 'indexed' outside parentheses
  • Swapping order of 'indexed' and type
3. Given the Solidity code below, what will be the output when the function sendTokens is called?
contract Token {
    event Sent(address indexed from, address indexed to, uint amount);

    function sendTokens(address receiver, uint amount) public {
        emit Sent(msg.sender, receiver, amount);
    }
}
medium
A. An event 'Sent' is emitted with sender, receiver, and amount data
B. A compilation error due to missing return statement
C. Nothing happens because events do not emit automatically
D. The function returns the amount sent

Solution

  1. Step 1: Understand event emission in function

    The function calls emit Sent(...) which triggers the event with given parameters.
  2. Step 2: Check function behavior

    The function does not return anything but emits the event, so An event 'Sent' is emitted with sender, receiver, and amount data is correct.
  3. Final Answer:

    An event 'Sent' is emitted with sender, receiver, and amount data -> Option A
  4. Quick Check:

    emit triggers event with data [OK]
Hint: emit keyword triggers event logs during function execution [OK]
Common Mistakes:
  • Expecting a return value from event emission
  • Thinking events execute code or change state
  • Confusing event emission with function return
4. Identify the error in the following Solidity event declaration:
contract Example {
    event LogMessage(string message);

    function log(string memory msg) public {
        emit LogMessage(msg);
    }
}
medium
A. There is no error; the code is correct
B. The event declaration is missing the indexed keyword
C. The event cannot have string type parameters
D. The function parameter name conflicts with a reserved keyword

Solution

  1. Step 1: Review event declaration and usage

    The event LogMessage with a string parameter is valid and emitted correctly.
  2. Step 2: Check for reserved keyword conflicts

    The parameter name msg is allowed as a variable name; it does not conflict with reserved keywords.
  3. Final Answer:

    There is no error; the code is correct -> Option A
  4. Quick Check:

    Valid event with string parameter and emit usage [OK]
Hint: Strings are allowed in events; 'msg' is not reserved as variable name [OK]
Common Mistakes:
  • Assuming all event parameters must be indexed
  • Thinking 'msg' is a reserved keyword variable
  • Believing strings cannot be event parameters
5. You want to declare an event Approval that logs the owner, spender, and amount approved. You also want to filter logs by owner and spender efficiently. Which is the best event declaration?
hard
A. event Approval(indexed address owner, indexed address spender, uint amount);
B. event Approval(address owner, address spender, uint indexed amount);
C. event Approval(address owner indexed, address spender indexed, uint amount);
D. event Approval(address indexed owner, address indexed spender, uint amount);

Solution

  1. Step 1: Understand indexed parameters for filtering

    Indexed parameters allow efficient filtering in event logs by those fields.
  2. Step 2: Check correct syntax and indexing

    event Approval(address indexed owner, address indexed spender, uint amount); correctly indexes owner and spender addresses, allowing filtering; amount is not indexed.
  3. Final Answer:

    event Approval(address indexed owner, address indexed spender, uint amount); -> Option D
  4. Quick Check:

    Indexed parameters = filterable fields [OK]
Hint: Index addresses to filter events; amount usually not indexed [OK]
Common Mistakes:
  • Indexing non-address parameters unnecessarily
  • Incorrect syntax for indexed keyword placement
  • Indexing parameters in wrong order
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