Unit 5 - Solidity Basics Practical
Unit 5 - Solidity Basics Practical
Question:
Write a contract Parent that initializes a public state variable owner to the deployer’s address using a
constructor. Then, write a contract Child that inherits from Parent and adds a constructor that accepts
an integer age as a parameter and stores it in a public variable age .
Answer:
Question:
Write two contracts, Caller and Callee . In Callee , write a public function multiply(uint x, uint
y) that returns the product of x and y . In Caller , write a function callMultiply that calls multiply
from Callee and returns the result.
Answer:
Question:
Write a function withdraw(uint amount) inside a contract Bank that allows the caller to withdraw ether
only if their balance is greater than or equal to amount . If not, it should revert the transaction with a
custom error message “Insufficient balance”.
Answer:
Question:
Write a contract Voting where an enum State has values Created , Voting , and Ended . Implement a
modifier onlyOwner that restricts access to the contract owner, and use it in the function startVoting
which transitions the state from Created to Voting .
Answer:
Question:
Write a contract Token that allows users to transfer tokens. Emit an event Transfer with parameters
from , to , and amount whenever a transfer occurs.
Answer:
Question:
Write a contract Calculator with two public functions: add(uint a, uint b) and subtract(int a,
int b) . The add function should return the sum, and the subtract function should return the difference.
Answer:
Question:
Write a contract Animal with a virtual function makeSound that returns "Animal sound" . Then, create a
contract Dog that overrides makeSound and returns "Bark!" .
Answer:
Question:
Write a contract Store where only the owner can set the price of a product using the setPrice(uint
_price) function. Use the onlyOwner modifier to restrict access.
Answer:
Question:
Write a contract SafeBank where users can deposit and withdraw Ether. Implement a reentrancy guard on
the withdraw function to prevent reentrancy attacks.
Answer:
Question:
Write a contract Counter that has a public function increment which increases a counter by 1. Emit an
event Incremented every time the function is called with the new counter value.
Answer: