Consider this simple smart contract that emits an event when deployed. What will be the value of the event's message when the contract is deployed?
pragma solidity ^0.8.0; contract HelloWorld { event Greet(string message); constructor() { emit Greet("Hello, Blockchain!"); } }
Look at the string inside the emit Greet(...) statement in the constructor.
The constructor emits the Greet event with the message "Hello, Blockchain!" immediately upon deployment.
This contract stores a number passed during deployment. What will be the value of storedNumber after deployment with the argument 42?
pragma solidity ^0.8.0;
contract StoreNumber {
uint public storedNumber;
constructor(uint _num) {
storedNumber = _num;
}
}The constructor sets storedNumber to the input parameter _num.
The constructor assigns the input value 42 to the public variable storedNumber. So after deployment, storedNumber equals 42.
Examine the contract below. When trying to deploy it, the deployment fails with a compilation error. What is the cause?
pragma solidity ^0.8.0; contract Counter { uint count; constructor() { count = 0; } }
Check the syntax of the assignment line inside the constructor.
In Solidity, each statement must end with a semicolon. The line count = 0 is missing a semicolon, causing a compilation error.
Choose the correct statement about deploying a smart contract on Ethereum.
Think about what happens exactly once during deployment.
When a smart contract is deployed, its code is stored on the blockchain and the constructor function runs once to initialize state.
Given this contract, what will be the output of calling getMessage() immediately after deployment?
pragma solidity ^0.8.0; contract Message { string private message; constructor() { setMessage("Welcome!"); } function setMessage(string memory _msg) public { message = _msg; } function getMessage() public view returns (string memory) { return message; } }
The constructor calls setMessage with "Welcome!" before deployment finishes.
Calling a public function from the constructor is allowed. The message variable is set to "Welcome!" during deployment, so getMessage() returns "Welcome!".