What is an event declaration in blockchain smart contracts?
An event declaration is a way to define a message that the smart contract can emit to the blockchain logs. It helps external applications listen and react to specific actions inside the contract.
Click to reveal answer
beginner
How do you declare an event in Solidity?
You use the event keyword followed by the event name and parameters inside parentheses. For example: event Transfer(address indexed from, address indexed to, uint256 value);
Click to reveal answer
intermediate
What does the indexed keyword mean in event parameters?
The indexed keyword marks parameters that can be used to filter events efficiently when searching logs. You can have up to 3 indexed parameters per event.
Click to reveal answer
beginner
Why are events important in blockchain applications?
Events allow smart contracts to communicate with external apps by logging important actions. This helps frontends or other services track contract activity without reading contract storage directly.
Click to reveal answer
beginner
Can events modify the blockchain state?
No, events only log information and do not change the blockchain state. They are used for off-chain communication and do not affect contract logic or storage.
Click to reveal answer
Which keyword is used to declare an event in Solidity?
Aevent
Bemit
Clog
Dfunction
✗ Incorrect
The event keyword declares an event. emit is used to trigger it.
What does the emit keyword do in Solidity?
ADeclares an event
BModifies contract storage
CFilters events
DTriggers an event to be logged
✗ Incorrect
emit triggers the event to be recorded in the blockchain logs.
How many parameters can be marked as indexed in a single event?
A1
B2
C3
DUnlimited
✗ Incorrect
Up to 3 parameters can be indexed for efficient filtering.
Can events change the state of a smart contract?
AYes, always
BNo, events only log data
COnly if indexed
DOnly during deployment
✗ Incorrect
Events only log information; they do not modify contract state.
Why use events in blockchain smart contracts?
ATo communicate with external apps
BTo store data permanently
CTo speed up transactions
DTo encrypt contract code
✗ Incorrect
Events help external apps listen to contract actions via logs.
Explain how to declare and use an event in a blockchain smart contract.
Think about how contracts send messages to the outside world.
You got /5 concepts.
Describe the role of the indexed keyword in event parameters and why it matters.
Consider how external apps find specific events quickly.
You got /4 concepts.
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
Step 1: Understand event declaration purpose
Events are declared to notify external apps about important contract actions.
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.
Final Answer:
To log important actions so external apps can track them -> Option C
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
Step 1: Recall Solidity event syntax
Indexed parameters are declared as type indexed name inside parentheses.
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.
Final Answer:
event Transfer(address indexed from, address indexed to); -> Option B
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
Step 1: Understand event emission in function
The function calls emit Sent(...) which triggers the event with given parameters.
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.
Final Answer:
An event 'Sent' is emitted with sender, receiver, and amount data -> Option A
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
Step 1: Review event declaration and usage
The event LogMessage with a string parameter is valid and emitted correctly.
Step 2: Check for reserved keyword conflicts
The parameter name msg is allowed as a variable name; it does not conflict with reserved keywords.
Final Answer:
There is no error; the code is correct -> Option A
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
Step 1: Understand indexed parameters for filtering
Indexed parameters allow efficient filtering in event logs by those fields.
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.