Consider a smart contract function that stores and retrieves a document hash for supply chain tracking. What will be the output after calling getDocumentHash()?
contract SupplyChain {
string private documentHash;
function storeDocumentHash(string memory hash) public {
documentHash = hash;
}
function getDocumentHash() public view returns (string memory) {
return documentHash;
}
}
// After calling storeDocumentHash("abc123hash")
// What does getDocumentHash() return?Think about what value is stored and returned by the function.
The function storeDocumentHash saves the string "abc123hash" in the private variable documentHash. The getDocumentHash function returns this stored value, so the output is "abc123hash".
In healthcare, sharing patient records securely is critical. Which blockchain feature helps ensure data integrity and controlled access?
Consider how data can be protected and shared only with authorized parties.
An immutable ledger ensures records cannot be changed once stored, and permissioned access restricts who can view or add data, making it ideal for sensitive medical records.
Review the following simplified voting contract code. Why does the vote count remain zero after voting?
contract Voting {
mapping(address => bool) public voters;
uint public voteCount;
function vote() public {
if (!voters[msg.sender]) {
voters[msg.sender] = true;
voteCount = voteCount + 1;
}
}
function getVotes() public view returns (uint) {
return voteCount;
}
}
// After calling vote() from different addresses, voteCount is still 0.Think about how blockchain state changes happen and what is needed to update state variables.
State changes like incrementing voteCount require a transaction that modifies the blockchain state. If vote() is called as a view or call (not a transaction), voteCount won't update.
Identify the correct fix for the syntax error in this Solidity code snippet:
mapping(address => string) public identities
function setIdentity(string memory name) public {
identities[msg.sender] = name;
}Check the end of the mapping declaration line.
In Solidity, each statement must end with a semicolon. The mapping declaration is missing a semicolon.
A blockchain asset tracking contract stores assets by unique IDs. After these transactions, how many unique assets are stored?
storeAsset(101, "Car") storeAsset(102, "Bike") storeAsset(101, "Truck") storeAsset(103, "Boat") storeAsset(102, "Scooter")
Consider that storing an asset with an existing ID overwrites the previous one.
Asset IDs 101, 102, and 103 are used. The second storeAsset calls with IDs 101 and 102 overwrite previous entries. So, there are 3 unique assets stored.