Skip to content

Instantly share code, notes, and snippets.

@g1na1011
Last active December 23, 2015 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save g1na1011/6558666 to your computer and use it in GitHub Desktop.
Save g1na1011/6558666 to your computer and use it in GitHub Desktop.
Below are the answers to our first week's quiz from Course 1 of Tealeaf Academy

Week 1 Quiz

  1. What is the value of a after the below code is executed?

    a = 1
    b = a
    b = 3
Answer: `a = 1` after the code is executed. 
  1. What's the difference between an Array and a Hash?

    Answer: An Array is an ordered list of objects organized by indices while a Hash is a collection of objects organized with key-valued pairs. Order matters in arrays and they can only use integers as indices. In hashes, you can define any type of object as its keys and values.

  2. Every Ruby expression returns a value. Say what value is returned in the below expressions:

    • arr = [1, 2, 3, 3]
      Answer: [1, 2, 3, 3]
    • [1, 2, 3, 3].uniq
      Answer: [1, 2, 3]
    • [1, 2, 3, 3].uniq!
      Answer: [1, 2, 3]
  3. What's the difference between the map and select methods for the Array class? Give an example of when you'd use one versus the other.

    Answer: Map and select are both methods that iterate through an array. Both are similar in that they return a new array. However, they are used for different situations.

    Map is used to iterate through an array of elements, returning a new array with values of the called block. This block applies a method to each of the elements, thus transforming the existing array. Select is used to iterate through an array of elements, returning a new array with values that returns true for the block of condition.

    For example,

arr = [1, 2, 3, 4, 5]
# The block is calling a method on each of the elements in the array. arr.map { |e| e + 1 } # => [2, 3, 4, 5, 6] arr.select { |e| e + 1 } # => [1, 2, 3, 4, 5]
# The block is calling a condition on each of the elements in the array. arr.select { |e| e.even? } # => [2, 4] arr.map { |e| e.even? } # => [false, true, false, true, false]
  1. Say you wanted to create a Hash. How would you do so if you wanted the hash keys to be String objects instead of symbols?

    Answer: new_hash = {"key1" => "value1", "key2" => "value2"}

  2. What is returned?

x = 1
    x.odd? ? "no way!" : "yes, sir!"
Answer: `"no way!"` is returned.
  1. What is x?
x = 1
    3.times do
      x += 1
    end
    puts x
  

Answer: x = 4

  1. What is x?
3.times do
      x += 1
    end
    puts x
  

Answer: This will produce an error, because x has not been declared as a local variable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment