Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new thread that prints 'Hello from thread'.
Ruby
thread = Thread.new { puts [1] } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the string
Using print instead of puts inside the thread
✗ Incorrect
The string must be inside quotes to be printed correctly.
2fill in blank
mediumComplete the code to start two threads that increment a shared counter.
Ruby
counter = 0 threads = [] 2.times do threads << Thread.new { counter [1] 1 } end threads.each(&:join) puts counter
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong operator like '-=' or '*='
Not incrementing the counter inside the thread
✗ Incorrect
The counter should be increased by 1 using the '+=' operator.
3fill in blank
hardFix the error in the code to safely increment a shared counter using a Mutex.
Ruby
counter = 0 mutex = Mutex.new threads = [] 3.times do threads << Thread.new do mutex.synchronize { counter [1] 1 } end end threads.each(&:join) puts counter
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong operator inside the mutex
Not using mutex to protect the counter
✗ Incorrect
Inside the mutex block, use '+=' to safely increment the counter.
4fill in blank
hardFill both blanks to create a hash with word lengths only for words longer than 3 characters.
Ruby
words = ["cat", "house", "dog", "elephant"] lengths = { word: word.[1] for word in words if word.[2] 3 }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>'
Using 'size' which is valid but not the expected answer here
✗ Incorrect
Use 'length' to get word length and '>' to filter words longer than 3.
5fill in blank
hardFill all three blanks to create a hash with uppercase keys and values greater than 0.
Ruby
data = { 'a' => 1, 'b' => 0, 'c' => 3 }
result = { [1]: [2] for k, v in data.[3] if v > 0 } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'map' instead of 'select' for filtering
Not converting keys to uppercase
✗ Incorrect
Use 'select' to filter, 'k.upcase' for uppercase keys, and 'v' for values.