> For the complete documentation index, see [llms.txt](https://ret2basic.gitbook.io/ctfnote/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ret2basic.gitbook.io/ctfnote/web3-security-research/secureum/epoch-0/slot-2-solidity-101/quiz.md).

# Quiz

{% embed url="<https://ventral.digital/posts/2021/10/24/secureum-bootcamp-solidity-101-quiz>" %}

**Q1 Solidity language is**

* [x] A) Statically typed
* [x] B) Object-oriented
* [x] C) Supports inheritance
* [x] D) Supports inline assembly

**Q2 Which of the following is/are correct?**

* [x] A) A Solidity file with pragma solidity ^0.6.5; can be compiled with compiler version 0.6.6
* [x] B) A Solidity file with pragma solidity 0.6.5; can only be compiled with compiler version 0.6.5
* [ ] C) A Solidity file with pragma solidity ^0.6.5; can be compiled with compiler version 0.7.0
* [ ] D) A Solidity file with pragma solidity >0.6.5 <0.7.0; can be compiled with compiler version 0.7.0

**Q3 Which of the following is/are true?**

* [ ] A) Constant state variables can be initialized within a constructor
* [ ] B) Immutable state variables are allocated a storage slot
* [x] C) Gas costs for constant and immutable variables is lower
* [x] D) Only value types can be immutable

**Q4 Solidity functions**

* [ ] A) Can be declared only inside contracts
* [x] B) Can have named return variables
* [x] C) Can have unnamed parameters
* [x] D) Can be recursive

**Q5 Function visibility**

* [x] A) Goes from private-internal-external-public in decreasing restrictive order (i.e. private being the most restrictive)
* [ ] B) Goes from internal-private-external-public in decreasing restrictive order (i.e. internal being the most restrictive)
* [ ] C) May be omitted to default to internal in the latest 0.8.0+ compiler versions
* [ ] D) None of the above

**Q6 Function foo() uses block.number. Which of the following is/are always true about foo()?**

* [ ] A) It should be marked as pure
* [ ] B) It should be marked as view
* [ ] C) It should be marked as payable
* [x] D) Cannot determine mutability based only on this information

**Q7 Which of the following is/are true about events?**

* [x] A) Events are meant for off-chain applications
* [ ] B) Events can be accessed only by the emitting contract
* [x] C) Indexing event parameters creates searchable topics
* [ ] D) A maximum of three events can have indexed parameters

**Q8 A contract can receive Ether via**

* [x] A) msg.value to payable functions
* [x] B) selfdestruct destination
* [x] C) coinbase transaction
* [x] D) receive() or fallback() functions

**Q9 receive() and fallback() functions**

* [x] A) Can rely only on 2300 gas in the worst case
* [x] B) May receive Ether with payable mutability
* [ ] C) Are mandatory for all contracts
* [x] D) Must have external visibility

<mark style="color:red;">**Q10 Which of the below are value types?**</mark>

* [x] A) Address
* [x] B) Enum
* [ ] C) Struct -> Reference typef
* [x] D) Contract

**Comment:**

> Value Types: Types that are passed by value, i.e. they are always copied when they are used as function arguments or in assignments — Booleans, Integers, Fixed Point Numbers, Address, Contract, Fixed-size Byte Arrays (bytes1, bytes2, …, bytes32), Literals (Address, Rational, Integer, String, Unicode, Hexadecimal), Enums, Functions.

from point 37 of [Solidity 101 - by Secureum](https://secureum.substack.com/p/solidity-101)

> Reference Types: Types that can be modified through multiple different names. Arrays (including Dynamically-sized bytes array bytes and string), Structs, Mappings.

from point 38 of [Solidity 101 - by Secureum](https://secureum.substack.com/p/solidity-101)

**Q11 The default value of**

* [x] A) Bool is false
* [x] B) Address is 0
* [x] C) Statically-sized array depends on the underlying type
* [x] D) Enum is its first member

<mark style="color:red;">**Q12 Address types**</mark>

* [ ] A) Can always receive Ether
* [x] B) Have members for balance, call, code
* [x] C) Can be converted to uint160 or contract types
* [ ] D) Can be added and subtracted

**Comment:**

> Address Type: The address type comes in two types: (1) address: Holds a 20 byte value (size of an Ethereum address) (2) address payable: Same as address, but with the additional members transfer and send. address payable is an address you can send Ether to, while a plain address cannot be sent Ether.

from point 45 of [Solidity 101 - by Secureum](https://secureum.substack.com/p/solidity-101)

> Members of Address Type:\
> address.balance (uint256): balance of the Address in Wei\
> address.code (bytes memory): code at the Address (can be empty)\
> address.call(bytes memory) returns (bool, bytes memory): issue low-level CALL with the given payload, returns success condition and return data, forwards all available gas, adjustable

from point 46 of [Solidity 101 - by Secureum](https://secureum.substack.com/p/solidity-101)

> Conversions: Implicit conversions from address payable to address are allowed, whereas conversions from address to address payable must be explicit via payable(address). Explicit conversions to and from address are allowed for uint160, integer literals, bytes20 and contract types.

from point 45.2 of [Solidity 101 - by Secureum](https://secureum.substack.com/p/solidity-101)

> TypeError: Operator - not compatible with types address and int\_const 1. Arithmetic operations on addresses are not supported. Convert to integer first before using them.

from playing around with remix [Remix](https://remix.ethereum.org/)

**Q13 transfer and send primitives**

* [x] A) Are used for Ether transfers
* [x] B) Trigger the receive() or fallback() functions of address
* [ ] C) Always return a value to be checked
* [x] D) Provide only 2300 gas

**Q14 Which of the following is/are true for call/delegatecall/staticcall primitives?**

* [x] A) They are used to call contracts
* [ ] B) They only revert without returning success/failure
* [x] C) Delegatecall retains the msg.sender and msg.value of caller contract
* [ ] D) Staticcall reverts if the called contract reads contract state of caller

**Q15 If we have an array then its data location can be**

* [x] A) memory and its persistence/scope will be the function of declaration
* [x] B) storage and its persistence/scope will be the entire contract
* [x] C) calldata and it will only be readable
* [ ] D) None of the above

**Q16 The impact of data location of reference types on assignments is**

* [ ] A) storage assigned to storage (local variable) makes a copy
* [ ] B) memory assigned to memory makes a copy
* [ ] C) memory assigned to storage creates a reference
* [x] D) None of the above

**Q17 Which of the following is/are valid control structure(s) in Solidity (excluding YUL)?**

* [x] A) if
* [x] B) else
* [ ] C) elif
* [ ] D) switch

**Q18 The gas left in the current transaction can be obtained with**

* [ ] A) tx.gas()
* [x] B) gasleft()
* [ ] C) msg.gas()
* [ ] D) block.gaslimit()

**Q19 Which of the following is/are valid function specifier(s)?**

* [x] A) internal
* [x] B) pure
* [x] C) payable
* [ ] D) immutable

**Q20 Integer overflows/underflows in Solidity**

* [ ] A) Are never possible because of the language design
* [x] B) Are possible but prevented by compiler added checks (version dependent)
* [x] C) Are possible but prevented by correctly using certain safe math libraries
* [ ] D) Are possible without any mitigation whatsoever

**Q21 Arrays in Solidity**

* [x] A) Can be fixed size or dynamic
* [x] B) Are zero indexed
* [x] C) Have push, pop and length members
* [ ] D) None of the above

**Q22 Structs in Solidity**

* [x] A) Are user-defined type
* [x] B) Are reference types
* [x] C) Can contain or be contained in arrays and mappings
* [ ] D) None of the above

<mark style="color:red;">**Q23 Which of the following is true about mapping types in mapping(\_KeyType => \_ValueType)?**</mark>

* [ ] A) \_KeyType can be any value or reference type
* [x] B) \_ValueType can be any value or reference type
* [x] C) Can only have storage (not memory) as data location
* [ ] D) Can be iterated over natively (i.e. without implementing another data structure)

**Comment:**

> The \_KeyType can be any built-in value type, bytes, string, or any contract or enum type. Other user-defined or complex types, such as mappings, structs or array types are not allowed. \_ValueType can be any type, including mappings, arrays and structs. They can only have a data location of storage and thus are allowed for state variables, as storage reference types in functions, or as parameters for library functions. You cannot iterate over mappings, i.e. you cannot enumerate their keys. It is possible, though, to implement a data structure on top of them and iterate over that.

from point 65 of [Solidity 101 - by Secureum](https://secureum.substack.com/p/solidity-101)

**Q24 if a = 1 then which of the following is/are true?**

* [x] A) a += 1 makes the value of a = 2
* [ ] B) b = ++a makes the value of b = 1
* [ ] C) a -= 1 makes the value of a = 1
* [x] D) b = a-- makes the value of b = 1

<mark style="color:red;">**Q25 delete varName; has which of the following effects?**</mark>

* [x] A) varName becomes 0 if varName is an integer
* [ ] B) varName becomes true if varName is a boolean
* [x] C) No effect if varName is a mapping
* [ ] D) Resets all struct members to their default values irrespective of their types

**Comment:**

> delete a assigns the initial value for the type to a\
> For integers it is equivalent to a = 0\
> For structs, it assigns a struct with all members reset\
> delete has no effect on mappings. So if you delete a struct, it will reset all members that are not mappings and also recurse into the members unless they are mappings.

from point 67 of [Solidity 101 - by Secureum](https://secureum.substack.com/p/solidity-101)

> Default Values: A variable which is declared will have an initial default value whose byte-representation is all zeros. The “default values” of variables are the typical “zero-state” of whatever the type is. For example, the default value for a bool is false.

from point 39 of [Solidity 101 - by Secureum](https://secureum.substack.com/p/solidity-101)

**Q26 Conversions in Solidity have the following behavior**

* [ ] A) Implicit conversions are never allowed
* [x] B) Explicit conversion of uint16 to uint8 removes higher-order bits
* [ ] C) Explicit conversion of uint16 to uint32 adds lower-order padding
* [ ] D) Explicit conversions are checked by compiler for safety

<mark style="color:red;">**Q27 If the previous block number was 1000 on Ethereum mainnet, which of the following is/are true?**</mark>

* [x] A) block.number is 1001
* [x] B) blockhash(1) returns 0 -> Only 256 most recent block hashes can be queried
* [x] C) block.chainID returns 1 -> Ethereum mainnet has ID 1
* [ ] D) block.timestamp returns the number of seconds since last block

**Comment:**

Block number is the number of the block that is currently being mined, the next one. Block number 1 was too long ago and its hash can no longer be accessed due to scaling reasons. [Mainnet ID Chain is 1.](https://besu.hyperledger.org/en/stable/Concepts/NetworkID-And-ChainID/)

> Block and Transaction Properties:\
> blockhash(uint blockNumber) returns (bytes32): hash of the given block - only works for 256 most recent, excluding current, blocks\
> block.chainid (uint): current chain id\
> block.number (uint): current block number\
> block.timestamp (uint): current block timestamp as seconds since unix epoch

from point 73 of [Solidity 101 - by Secureum](https://secureum.substack.com/p/solidity-101)

**Q28 User from EOA A calls Contract C1 which makes an external call (CALL opcode) to Contract C2. Which of the following is/are true?**

* [x] A) tx.origin in C2 returns A’s address
* [ ] B) msg.sender in C2 returns A’s address
* [x] C) msg.sender in C1 returns A’s address
* [ ] D) msg.value in C2 returns amount of Wei sent from A

<mark style="color:red;">**Q29 For error handling**</mark>

* [x] A) require() is meant to be used for input validation
* [ ] B) require() has a mandatory error message string
* [x] C) assert() is meant to be used to check invariants
* [x] D) revert() will abort and revert state changes

**Comment:**

> assert(bool condition): causes a Panic error and thus state change reversion if the condition is not met - to be used for internal errors.\
> require(bool condition): reverts if the condition is not met - to be used for errors in inputs or external components.\
> require(bool condition, string memory message): reverts if the condition is not met - to be used for errors in inputs or external components. Also provides an error message.\
> revert(): abort execution and revert state changes\
> revert(string memory reason): abort execution and revert state changes, providing an explanatory string

from point 78 of [Solidity 101 - by Secureum](https://secureum.substack.com/p/solidity-101)

> The assert function creates an error of type Panic(uint256). Assert should only be used to test for internal errors, and to check invariants. Properly functioning code should never create a Panic, not even on invalid external input.

from point 88 of [Solidity 101 - by Secureum](https://secureum.substack.com/p/solidity-101)

<mark style="color:red;">**Q30 The following is/are true about ecrecover primitive**</mark>

* [x] A) Takes a message hash and ECDSA signature values as inputs
* [ ] B) Recovers and returns the public key of the signature -> Return signer's address
* [x] C) Is susceptible to malleable signatures
* [ ] D) None of the above

**Comment:**

Although internally it first recovers the public key from the signature, it actually returns the address derived from the public key.

> 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. ecrecover returns an address, and not an address payable.

from point 79.6 of [Solidity 101 - by Secureum](https://secureum.substack.com/p/solidity-101)

> If you use ecrecover, be aware that a valid signature can be turned into a different valid signature without requiring knowledge of the corresponding private key. This is usually not a problem unless you require signatures to be unique or use them to identify items. OpenZeppelin has a ECDSA helper library that you can use as a wrapper for ecrecover without this issue.

from point 80 of [Solidity 101 - by Secureum](https://secureum.substack.com/p/solidity-101)

<mark style="color:red;">**Q31 When Contract A attempts to make a delegatecall to Contract B but a prior transaction to Contract B has executed a selfdestruct**</mark>

* [ ] A) The delegatecall reverts
* [ ] B) The delegatecall returns a failure
* [x] C) The delegatecall returns a success
* [ ] D) This scenario is not practically possible

**Comment:**

> The low-level functions call, delegatecall and staticcall return true as their first return value if the account called is non-existent, as part of the design of the EVM. Account existence must be checked prior to calling if needed.

from point 87 of [Solidity 101 - by Secureum](https://secureum.substack.com/p/solidity-101)

**Q32 In Solidity, selfdestruct(address)**

* [ ] A) Destroys the contract whose address is given as argument
* [x] B) Destroys the contract executing the selfdestruct
* [ ] C) Sends address’s balance to the calling contract
* [x] D) Sends executing contract’s balance to the address
