Skip to content

Commit

Permalink
fix: make questions list items, remove playground links
Browse files Browse the repository at this point in the history
  • Loading branch information
whoabuddy committed Sep 2, 2024
1 parent 1ff8ae9 commit dbd0f72
Show file tree
Hide file tree
Showing 10 changed files with 652 additions and 664 deletions.
63 changes: 35 additions & 28 deletions exercises/quiz-01.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,53 @@
1. True or False: In Clarity, all variables must be explicitly typed.

2. Which of the following is not a valid Clarity type?
a) uint
b) principal
c) float
d) bool

- a) uint
- b) principal
- c) float
- d) bool

3. What is the correct way to define a public function in Clarity?
a) (public-function (my-function))
b) (define-public (my-function))
c) (function-public my-function)
d) (public my-function)

- a) (public-function (my-function))
- b) (define-public (my-function))
- c) (function-public my-function)
- d) (public my-function)

4. Which function is used to create a new map in Clarity?
a) create-map
b) new-map
c) define-map
d) map-define

- a) create-map
- b) new-map
- c) define-map
- d) map-define

5. What does the `tx-sender` keyword represent?
a) The contract’s address
b) The current block miner
c) The sender of the current transaction
d) The recipient of the current transaction

- a) The contract’s address
- b) The current block miner
- c) The sender of the current transaction
- d) The recipient of the current transaction

6. Which of the following is not a valid Clarity keyword?
a) get-block-height
b) block-height
c) burn-block-height
d) contract-caller

- a) get-block-height
- b) block-height
- c) burn-block-height
- d) contract-caller

7. Which of the following is a valid way to define a constant in Clarity?
a) (const MY_CONSTANT 42)
b) (define-constant MY_CONSTANT 42)
c) (define-data-const MY_CONSTANT 42)
d) (set-constant MY_CONSTANT 42)

- a) (const MY_CONSTANT 42)
- b) (define-constant MY_CONSTANT 42)
- c) (define-data-const MY_CONSTANT 42)
- d) (set-constant MY_CONSTANT 42)

8. What is the correct way to define a function argument type as a list of integers with a maximum length of 10 in Clarity?
a) (list 10 int)
b) (list int 10)
c) (define-list int 10)
d) (int-list 10)

- a) (list 10 int)
- b) (list int 10)
- c) (define-list int 10)
- d) (int-list 10)

**Bonus Coding Challenges**

Expand Down
137 changes: 68 additions & 69 deletions exercises/quiz-02.md
Original file line number Diff line number Diff line change
@@ -1,100 +1,99 @@
# Quiz 2: Control Flow and Error Handling

1. Which keyword is used for conditional branching in Clarity?
a) if-else
b) switch
c) if
d) condition

- a) if-else
- b) switch
- c) if
- d) condition

2. True or False: Clarity supports try-catch blocks for error handling.

3. In Clarity, what is a correct usage of the `asserts!` function in a public function?
a) (asserts! (is-eq 1 1) false)
b) (asserts! (is-eq 1 2))
c) (asserts! (is-eq 1 2) (err u1))
d) Both a or c

- a) (asserts! (is-eq 1 1) false)
- b) (asserts! (is-eq 1 2))
- c) (asserts! (is-eq 1 2) (err u1))
- d) Both a or c

4. Which of the following is the correct way to return an error in a public function?
a) (return-error u1)
b) (throw-error u1)
c) (err u1)
d) (error u1)

- a) (return-error u1)
- b) (throw-error u1)
- c) (err u1)
- d) (error u1)

5. What is the purpose of the `unwrap!` function in Clarity?
a) To extract a value from an option type
b) To handle errors in a try-catch block
c) To unpack a response type, returning the value or throwing an error
d) To convert a string to a number

- a) To extract a value from an option type
- b) To handle errors in a try-catch block
- c) To unpack a response type, returning the value or throwing an error
- d) To convert a string to a number

6. Which function is used to check if a response is an "ok" value?
a) is-ok
b) check-ok
c) ok?
d) response-ok

- a) is-ok
- b) check-ok
- c) ok?
- d) response-ok

7. In Clarity, what does the `match` expression do?
a) Compares two values for equality
b) Evaluate and de-structure optional and response types
c) Executes a loop until a condition is met
d) Defines a new function

- a) Compares two values for equality
- b) Evaluate and de-structure optional and response types
- c) Executes a loop until a condition is met
- d) Defines a new function

8. Which of the following is NOT a valid response type in Clarity?
a) (ok u1)
b) (err u2)
c) (some u3)
d) (none u4)

- a) (ok u1)
- b) (err u2)
- c) (some u3)
- d) (none u4)

**Bonus Coding Challenges**

9. The following code causes an indeterminate error. Resolve the issue inside of `execute` by using the `is-ok` function.

```clojure
;; This function always returns an ok response
(define-public (say-hello)
(ok "Hello")
)

;; This function attempts to match on a response with an indeterminate err type
(define-public (execute)
(match (say-hello)
success (ok success)
error (err error)
)
)

;; Test cases
(execute) ;; Should return (ok "Hello")
```
```clojure
;; This function always returns an ok response
(define-public (say-hello)
(ok "Hello")
)

[Problem](https://play.hiro.so/?epoch=2.5&snippet=OzsgVGhpcyBmdW5jdGlvbiBhbHdheXMgcmV0dXJucyBhbiBvayByZXNwb25zZQooZGVmaW5lLXB1YmxpYyAoc2F5LWhlbGxvKQogIChvayAiSGVsbG8iKQopCgo7OyBUaGlzIGZ1bmN0aW9uIGF0dGVtcHRzIHRvIG1hdGNoIG9uIGEgcmVzcG9uc2Ugd2l0aCBhbiBpbmRldGVybWluYXRlIGVyciB0eXBlCihkZWZpbmUtcHVibGljIChleGVjdXRlKQogIChtYXRjaCAoc2F5LWhlbGxvKQogICAgc3VjY2VzcyAob2sgc3VjY2VzcykKICAgIGVycm9yIChlcnIgZXJyb3IpCiAgKQopCgo7OyBUZXN0IGNhc2VzCihleGVjdXRlKSA7OyBTaG91bGQgcmV0dXJuIChvayAiSGVsbG8iKQ)
;; This function attempts to match on a response with an indeterminate err type
(define-public (execute)
(match (say-hello)
success (ok success)
error (err error)
)
)

[Solution](https://play.hiro.so/?epoch=2.5&snippet=OzsgVGhpcyBmdW5jdGlvbiBhbHdheXMgcmV0dXJucyBhbiBvayByZXNwb25zZQooZGVmaW5lLXB1YmxpYyAoc2F5LWhlbGxvKQogIChvayAiSGVsbG8iKQopCgo7OyBUaGlzIGZ1bmN0aW9uIGF0dGVtcHRzIHRvIG1hdGNoIG9uIGEgcmVzcG9uc2Ugd2l0aCBhbiBpbmRldGVybWluYXRlIGVyciB0eXBlCihkZWZpbmUtcHVibGljIChleGVjdXRlKQogIChsZXQKICAgICgKICAgICAgKHJlc3VsdCAoc2F5LWhlbGxvKSkKICAgICkKICAgIChpZiAoaXMtb2sgcmVzdWx0KQogICAgICByZXN1bHQKICAgICAgKGVyciB1MSkKICAgICkKICApCikKCjs7IFRlc3QgY2FzZXMKKGV4ZWN1dGUpIDs7IFNob3VsZCByZXR1cm4gKG9rICJIZWxsbyIp)
;; Test cases
(execute) ;; Should return (ok "Hello")
```

10. Implement a function called `process-transaction` that takes an unsigned integer amount as input. The function should:

- Check if the amount is between 10 and 1000 (inclusive).
- If it is, return an `ok` response with the message “Transaction processed”.
- If it’s less than 10, return an `err` with code `u1`.
- If it’s greater than 1000, return an `err` with code `u2`.

```clojure
(define-public (process-transaction (amount uint))
;; Your code here
)

;; Test cases
(process-transaction u5) ;; Should return (err u1)
(process-transaction u50) ;; Should return (ok "Transaction processed")
(process-transaction u1500) ;; Should return (err u2)
```
- Check if the amount is between 10 and 1000 (inclusive).
- If it is, return an `ok` response with the message “Transaction processed”.
- If it’s less than 10, return an `err` with code `u1`.
- If it’s greater than 1000, return an `err` with code `u2`.

[Problem](https://play.hiro.so/?epoch=2.5&snippet=KGRlZmluZS1wdWJsaWMgKHByb2Nlc3MtdHJhbnNhY3Rpb24gKGFtb3VudCB1aW50KSkKICA7OyBZb3VyIGNvZGUgaGVyZQopCgo7OyBUZXN0IGNhc2VzCihwcm9jZXNzLXRyYW5zYWN0aW9uIHU1KSAgICA7OyBTaG91bGQgcmV0dXJuIChlcnIgdTEpCihwcm9jZXNzLXRyYW5zYWN0aW9uIHU1MCkgICA7OyBTaG91bGQgcmV0dXJuIChvayAiVHJhbnNhY3Rpb24gcHJvY2Vzc2VkIikKKHByb2Nlc3MtdHJhbnNhY3Rpb24gdTE1MDApIDs7IFNob3VsZCByZXR1cm4gKGVyciB1Mik)
```clojure
(define-public (process-transaction (amount uint))
;; Your code here
)

[Solution](https://play.hiro.so/?epoch=2.5&snippet=KGRlZmluZS1wdWJsaWMgKHByb2Nlc3MtdHJhbnNhY3Rpb24gKGFtb3VudCB1aW50KSkKICAoaWYgKGFuZCAoPj0gYW1vdW50IHUxMCkgKDw9IGFtb3VudCB1MTAwMCkpCiAgICAob2sgIlRyYW5zYWN0aW9uIHByb2Nlc3NlZCIpCiAgICAoZXJyIChpZiAoPCBhbW91bnQgdTEwKSB1MSB1MikpCiAgKQopCgo7OyBUZXN0IGNhc2VzCihwcm9jZXNzLXRyYW5zYWN0aW9uIHU1KSAgICA7OyBTaG91bGQgcmV0dXJuIChlcnIgdTEpCihwcm9jZXNzLXRyYW5zYWN0aW9uIHU1MCkgICA7OyBTaG91bGQgcmV0dXJuIChvayAiVHJhbnNhY3Rpb24gcHJvY2Vzc2VkIikKKHByb2Nlc3MtdHJhbnNhY3Rpb24gdTE1MDApIDs7IFNob3VsZCByZXR1cm4gKGVyciB1Mik)
;; Test cases
(process-transaction u5) ;; Should return (err u1)
(process-transaction u50) ;; Should return (ok "Transaction processed")
(process-transaction u1500) ;; Should return (err u2)
```

**Hints:**
**Hints:**

- You can use `if` or `match` for conditional branching.
- Remember to use the `u` prefix for uint literals.
- The `and` function can be useful for checking multiple conditions.
- Use `ok` and `err` to construct response types.
- You can use `if` or `match` for conditional branching.
- Remember to use the `u` prefix for uint literals.
- The `and` function can be useful for checking multiple conditions.
- Use `ok` and `err` to construct response types.
98 changes: 50 additions & 48 deletions exercises/quiz-03.md
Original file line number Diff line number Diff line change
@@ -1,58 +1,67 @@
# Quiz 3: Maps and Variables

1. What is the primary purpose of `define-map` in Clarity?
a) To create a new variable
b) To define a new function
c) To create a key-value store
d) To import external libraries

- a) To create a new variable
- b) To define a new function
- c) To create a key-value store
- d) To import external libraries

2. Which function is used to retrieve an entry from a map in Clarity?
a) map-get
b) map-set
c) map-get?
d) map-retrieve

- a) map-get
- b) map-set
- c) map-get?
- d) map-retrieve

3. What is the correct way to define a new variable in Clarity?
a) (define-var counter into 0)
b) (define-data-var counter int 0)
c) (var-define counter int 0)
d) (define-data-var int 0)

- a) (define-var counter into 0)
- b) (define-data-var counter int 0)
- c) (var-define counter int 0)
- d) (define-data-var int 0)

4. Which of the following is true about `**var-set`\*\* in Clarity?
a) It creates a new variable
b) It always returns false
c) It can only be used with maps
d) It updates the value of an existing variable

- a) It creates a new variable
- b) It always returns false
- c) It can only be used with maps
- d) It updates the value of an existing variable

5. What is the difference between `**map-set`** and `**map-insert`\*\* in Clarity?
a) There is no difference, they are interchangeable
b) `map-set`overwrites existing values, while`map-insert`only adds new entries
c)`map-set`is for maps, while`map-insert`is for lists
d)`map-set`is read-only, while`map-insert` allows modifications

- a) There is no difference, they are interchangeable
- b) `map-set`overwrites existing values, while`map-insert`only adds new entries
- c)`map-set`is for maps, while`map-insert`is for lists
- d)`map-set`is read-only, while`map-insert` allows modifications

6. Which function is used to remove an entry from a map in Clarity?
a) map-remove
b) map-delete
c) map-erase
d) map-clear

- a) map-remove
- b) map-delete
- c) map-erase
- d) map-clear

7. What does the `**var-get`\*\* function do in Clarity?
a) Creates a new variable
b) Retrieves the value of a defined variable
c) Checks if a variable exists
d) Deletes a variable

- a) Creates a new variable
- b) Retrieves the value of a defined variable
- c) Checks if a variable exists
- d) Deletes a variable

8. In Clarity, what is the maximum number of entries a map can hold?
a) 100
b) 1,000
c) 4,999
d) There is no fixed limit

- a) 100
- b) 1,000
- c) 4,999
- d) There is no fixed limit

9. You're building a voting system where each user can only vote once. Which of the following approaches is the most efficient and secure way to track votes?
a) Use a data variable to store a `list` of all voters
b) Use a `map` with the voter's principal as the key and their vote as the value
c) Use a data variable to store the total vote count and check off-chain if a user has voted
d) Use a `map` with `uint` representing the Proposal ID as the key and a `list` of voters as the value

- a) Use a data variable to store a `list` of all voters
- b) Use a `map` with the voter's principal as the key and their vote as the value
- c) Use a data variable to store the total vote count and check off-chain if a user has voted
- d) Use a `map` with `uint` representing the Proposal ID as the key and a `list` of voters as the value

10. Consider the following Clarity code snippet:

Expand All @@ -64,10 +73,11 @@ d)`map-set`is read-only, while`map-insert` allows modifications
```

If I wanted a way to retrieve a specific user, what would be the most efficient way to improve this code structure?
a) Use a `list` to store all user information
b) Create separate maps for each user using a unique identifier as the key
c) Use a single `map` with a tuple to store all user information
d) Keep the current structure as it's already optimal

- a) Use a `list` to store all user information
- b) Create separate maps for each user using a unique identifier as the key
- c) Use a single `map` with a tuple to store all user information
- d) Keep the current structure as it's already optimal

**Bonus Coding Challenges**

Expand All @@ -82,10 +92,6 @@ d) Keep the current structure as it's already optimal
(get-profile 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM) ;; Should return a user
```

[Problem](https://play.hiro.so/?epoch=2.5&snippet=KGRlZmluZS1yZWFkLW9ubHkgKGdldC1wcm9maWxlICh3aG8gcHJpbmNpcGFsKSkKCTs7IFlvdXIgY29kZSBoZXJlCikKCjs7IFRlc3QgY2FzZXMKKGdldC1wcm9maWxlICdTVDFQUUhRS1YwUkpYWkZZMURHWDhNTlNOWVZFM1ZHWkpTUlRQR1pHTSkgOzsgU2hvdWxkIHJldHVybiBhIHVzZXI)

[Solution](https://play.hiro.so/?epoch=2.5&snippet=KGRlZmluZS1tYXAgVXNlclByb2ZpbGVzIHByaW5jaXBhbCB7IG5hbWU6IChzdHJpbmctYXNjaWkgNTApLCBhZ2U6IHVpbnQgfSkKCjs7IEltcGxlbWVudCB0aGUgZ2V0LWJhbGFuY2UgZnVuY3Rpb24gaGVyZQooZGVmaW5lLXJlYWQtb25seSAoZ2V0LXByb2ZpbGUgKHdobyBwcmluY2lwYWwpKQoJKG1hcC1nZXQ_IFVzZXJQcm9maWxlcyB3aG8pCikKCihkZWZpbmUtcmVhZC1vbmx5IChnZXQtbmFtZSAod2hvIHByaW5jaXBhbCkpCgkoZ2V0IG5hbWUgKGdldC1wcm9maWxlIHdobykpCikKCjs7IFRlc3QgY2FzZXMKKG1hcC1zZXQgVXNlclByb2ZpbGVzIHR4LXNlbmRlciB7IG5hbWU6ICJSeWFuIiwgYWdlOiB1Mzl9KQooZ2V0LXByb2ZpbGUgJ1NUMVBRSFFLVjBSSlhaRlkxREdYOE1OU05ZVkUzVkdaSlNSVFBHWkdNKSA7OyBTaG91bGQgcmV0dXJuIGEgdXNlcgooZ2V0LW5hbWUgJ1NUMVBRSFFLVjBSSlhaRlkxREdYOE1OU05ZVkUzVkdaSlNSVFBHWkdNKQ)

12. Implement a simple voting system using maps and variables. Create functions to register voters, cast votes, and get the current vote count for candidates. Ensure that each voter can only vote once and only registered voters can cast votes.

```clojure
Expand Down Expand Up @@ -114,7 +120,3 @@ d) Keep the current structure as it's already optimal
(cast-vote 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM) ;; Should fail (already voted)
(get-vote-count 'ST2CY5V39NHDPWSXMW9QDT3HC3GD6Q6XX4CFRK9AG) ;; Should return (ok u1)
```

[Problem](https://play.hiro.so/?epoch=2.5&snippet=KGRlZmluZS1tYXAgVm90ZXJzIHByaW5jaXBhbCBib29sKQooZGVmaW5lLW1hcCBWb3RlQ291bnRzIHByaW5jaXBhbCB1aW50KQooZGVmaW5lLWRhdGEtdmFyIFRvdGFsVm90ZXMgdWludCB1MCkKCjs7IEltcGxlbWVudCB0aGUgcmVnaXN0ZXItdm90ZXIgZnVuY3Rpb24KKGRlZmluZS1wdWJsaWMgKHJlZ2lzdGVyLXZvdGVyICh2b3RlciBwcmluY2lwYWwpKQogIDs7IFlvdXIgY29kZSBoZXJlCikKCjs7IEltcGxlbWVudCB0aGUgY2FzdC12b3RlIGZ1bmN0aW9uCihkZWZpbmUtcHVibGljIChjYXN0LXZvdGUgKHZvdGVyIHByaW5jaXBhbCkgKGNhbmRpZGF0ZSBwcmluY2lwYWwpKQogIDs7IFlvdXIgY29kZSBoZXJlCikKCjs7IEltcGxlbWVudCB0aGUgZ2V0LXZvdGUtY291bnQgZnVuY3Rpb24KKGRlZmluZS1yZWFkLW9ubHkgKGdldC12b3RlLWNvdW50IChjYW5kaWRhdGUgcHJpbmNpcGFsKSkKICA7OyBZb3VyIGNvZGUgaGVyZQopCgo7OyBUZXN0IGNhc2VzCihyZWdpc3Rlci12b3RlciAnU1QxUFFIUUtWMFJKWFpGWTFER1g4TU5TTllWRTNWR1pKU1JUUEdaR00pIDs7IFNob3VsZCByZXR1cm4gKG9rIHRydWUpCihjYXN0LXZvdGUgdHgtc2VuZGVyICdTVDJDW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