0
0
Blockchain / Solidityprogramming~20 mins

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

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