Frequently asked: Solidity Interview Questions and Answers

Vigo Webs
6 min readMay 29, 2022

--

Q1. What is Ethereum Virtual Machine (EVM)?

The Ethereum Virtual Machine or EVM is the runtime environment for smart contracts in Ethereum. It is not only sandboxed but actually completely isolated, which means that code running inside the EVM has no access to network, filesystem or other processes. Smart contracts even have limited access to other smart contracts.

Q2. What makes an Ethereum smart contract so special compared to other programs?

Once an Ethereum smart contract is deployed to the blockchain, it cannot:
• be hacked (as long as the code of the contract is correct)
• be stopped
• be modified (the code is immutable, however, the data is)

Q3. What is a smart contract?

A smart contract (crypto-contract) is a program with a set of codes used to directly control cryptocurrency transfers and assets between parties dependent upon certain conditions. A blockchain user creates the code and later executes it by the blockchain node. The contract executes itself only if the conditions are met. SC can be created on platforms like Ethereum, NXT, and Chain, with automated transactions and scripts in Bitcoin.

Q4. What is gas price?

Gas refers to the cost necessary to perform a transaction on the network. Miners set the price of gas based on supply and demand for the computational power of the network needed to process smart contracts and other transactions.

Q5. How do gas limit and gas price influence the mining of transactions?

As Ethereum transactions run on gas instead of Ethers, the execution of certain commands and transactions costs gas. This type of execution is presented by the amount of gas you send (gas price) and a total block gas limit. The gas limit is modified by the user who sends the transaction; it is the amount of gas sent along. The gas price is the actual transaction cost, which combines the cost of the transaction and the cost of execution. The transaction price equals gas used multiplied by the gas price. The gas limit of a transaction can put the time of mining into a block. Transactions with high gas limits can be less prioritized and take more time to get into the block.

Q6. How can you protect yourself from a re-entry attack?

Solution 1: Lower balances and update other state variables before invoking the other contract.
Solution 2: Implement a reentrancy guard that uses a variable to determine when a call is second in the stack.
Solution 3: Limit the amount of gas available to the called contract. This is automatically done if you use transfer().

Q7. What is the method of payment for gas?

Gas is paid in ether using the formula: ether cost = gasPrice * gas. In this formula, the gas represents the gas cost of executing a transaction. gasPrice is in wei / gas, usually expressed in Gwei. A transaction also shows a gasLimit parameter- it specifies the maximum number of gas that a transaction can pay. A transaction without this could potentially deplete an account’s Ether.

Q8. What is Solidity Structs?

Structures are custom data types. Struct types are used to represent a record. Suppose we want to keep track of our movies in a movie library. we might want to track the following attributes of each movie:

• Title
• Director
• Category
• Movie ID

Example:

Q9. What is Solidity Conversions?

Solidity allows implicit as well as explicit conversion. Solidity compiler allows implicit conversion between two data types provided no implicit conversion is possible and there is no loss of information. For example, uint8 is convertible to uint16 but int8 is convertible to uint256 as int8 can contain negative value not allowed in uint256

Q10. What Is a Wei?

Wei is the smallest denomination of ether — the cryptocurrency coin used on the Ethereum network. One ether = 1,000,000,000,000,000,000 wei (1018). The other way to look at it is one wei is one quintillionth of an ether.

Q11. What do you know about hexadecimal literals in Solidity?

Hexadecimal literals are prefixed with the keyword hex and are enclosed in double or single quotes (hex"001122FF", hex'0011_22_FF'). Their content must be hexadecimal digits which can optionally use a single underscore as a separator between byte boundaries. The value of the literal will be the binary representation of the hexadecimal sequence. Multiple hexadecimal literals separated by whitespace are concatenated into a single literal: hex"00112233" hex"44556677" is equivalent to hex"0011223344556677" Hexadecimal literals behave like string literals and have the same convertibility restrictions.

Q12. What is Cryptographic or Hashing Functions in solidity?

Solidity provides inbuilt cryptographic functions as well. The following are important methods:
keccak256(bytes memory) returns (bytes32) − Computes the Keccak-256 hash of the input.
ripemd160(bytes memory) returns (bytes20) − Compute RIPEMD-160 hash of the input.
sha256(bytes memory) returns (bytes32) − Computes the SHA-256 hash of the input.
ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns (address) − Recover the address associated with the public key from elliptic curve signature or return zero on error. The function parameters correspond to ECDSA values of the signature: r — first 32 bytes of signature; s: second 32 bytes of signature; v: final 1 byte of signature. This method returns an address.

Example:

Output:

Q13. What is Restricted Access in solidity?

Restricted Access to a Contract is a common practice. By Default, a contract state is read-only unless it is specified as public. We can restrict who can modify the contract’s state or call a contract’s functions using modifiers. We will create and use multiple modifiers as explained below:
onlyBy − once used on a function then only the mentioned caller can call this function.
onlyAfter − once used on a function then that function can be called after a certain time period.
costs − once used on a function the caller can call this function only if a certain value is provided.

Q14. What is Library in Solidity?

Libraries in solidity are similar to contracts that contain reusable codes. A library has functions that can be called by other contracts. Deploying a common code by creating a library reduces the gas cost.

Q15. Explain Error Handling in Solidity?

Solidity provides various functions for error handling. Generally when an error occurs, the state is reverted back to its original state. Other checks are to prevent unauthorized code access. Following are some of the important methods used in error handling:
assert(bool condition) − In case condition is not met, this method call causes an invalid opcode and any changes done to state got reverted. This method is to be used for internal errors.
require(bool condition) − In case condition is not met, this method call reverts to original state. — This method is to be used for errors in inputs or external components.
require(bool condition, string memory message) − In case condition is not met, this method call reverts to original state. — This method is to be used for errors in inputs or external components. It provides an option to provide a custom message.
revert() − This method aborts the execution and revert any changes done to the state.
revert(string memory reason) − This method aborts the execution and revert any changes done to the state. It provides an option to provide a custom message.

To read more Solidity Interview Questions and answer check out our Android App from play store:

https://play.google.com/store/apps/details?id=com.vigowebs.interviewquestions

Our app contains 1700+ Interview Questions and answers with clear code examples from trending technologies.

--

--

No responses yet