Skip to content

Instantly share code, notes, and snippets.

@MaxPleaner
Last active April 16, 2018 21:17
Show Gist options
  • Save MaxPleaner/b5b090340b69756f7aa99decf435a73e to your computer and use it in GitHub Desktop.
Save MaxPleaner/b5b090340b69756f7aa99decf435a73e to your computer and use it in GitHub Desktop.
Quiz

Questions

ruby

  1. inheritance vs nested classes
  2. the tap function
  3. the reduce function
  4. iterator shorthands
  5. constant lookup
  6. operator precedence
  7. requiring files
  8. Default hash values

javascript/coffeescript

  1. NaN
  2. more bullshit
  3. classes and arrow functions
  4. objects and arrow functions
  5. the 'return' keyword
  6. async loops

inheritance vs nested classes

What is the return value or error when calling Foo::Bar.b for each of the following:

1

class Foo
  def self.a; 1; end
  class Bar < Foo
    def self.b; a; end
  end  
end  

2

class Foo
  def self.a; 1; end
  class Bar
    def self.b; a; end
  end  
end  
answer
  1. 1
  2. NoMethodError

the tap function

What are the return values for each of the following:

1

1.tap { |num| num + 1 }

2

1.tap { |num| num += 1 }

3

{}.tap { |hash| hash[:foo] = :bar }

4

{}.tap { |hash| hash.merge foo: :bar }
answer
  1. 1
  2. 1
  3. { foo: :bar }
  4. { }

the reduce function

What are the return values when calling map_keys({1 => 2}) { |key| key + 1 } with each of the following:

1

  def map_keys(hash, &blk)
    hash.reduce({}) do |memo, (key, val)|
      memo.tap { memo[blk.call key] = val }
    end
  end

2

  def map_keys(hash, &blk)
    hash.reduce({}) do |memo, (key, val)|
      memo[blk.call key] = val
    end
  end

3

  def map_keys(hash, &blk)
    hash.each_with_object({}) do |(key, val), memo|
      memo[blk.call key] = val
    end
  end
answer
  1. { 2 => 2 }
  2. 2
  3. { 2 => 2 }

iterator shorthands

What will be printed by each of the following (ignore the return vals), or will there be an error

1

[[]].map(&:class).each &method(:print)

2

[[]].map(&:class).each &:print

3

 [[]].map { |elem| elem.class }.each { |elem| print elem }
answer
  1. Array
  2. NoMethodError
  3. Array

--

Constant lookup

Given class Foo; class Bar; end; end, what is the result or error of each of the following:

1

Foo.class_exec { Bar }

2

Foo.class_exec { ::Bar }

3

Foo.class_exec { self::Bar }
answer
  1. NameError
  2. NameErrr
  3. Bar

operator precedence

What will the following return?

1

false if true && true

2

nil if true && true
answer
  1. false
  2. nil

requiring files

Say there are three files, each containing one of the following code snippets. For each of the files, what is the result or error of requiring it from pry and entering foo?

1

foo = :bar

2

def foo; :bar; end

3

foo = ->{ :bar }
answer
  1. NoMethodError
  2. :bar
  3. NoMethodError

Default hash values

What is the result of each of the following?

1

Hash.new(0)[:a]

2

Hash.new(0).has_key? :a

3

Hash.new(:a).has_key? :a

4

Hash.new { |h,k| h[k] = :a }[:a]
answer
  1. 0
  2. false
  3. false
  4. :a

NaN

What is the result of each of the following (coffeescript):

1

NaN == NaN

2

typeof NaN

3

isNaN NaN
answer
  1. false
  2. 'number'
  3. true

more bullshit

What is the result (or error) of each of the following (coffeescript):

1

typeof null

2

typeof undefined

3

{}.constructor()

4

x = {}
x.constructor()

5

!0
answer
  1. 'object'
  2. 'object'
  3. error (constructor is not a function)
  4. Object
  5. true

classes and arrow functions

Given the following coffeescript:

class A
  b: -> @d
  c: => @d
  d: "ok"

a = new A()

What will each of the following return:

1

a.b() == "ok"

2

a.c() == "ok"

3

a.b.apply({}) == "ok"

4

a.c.apply({}) == "ok"
answer
  1. true
  2. true
  3. false
  4. true

objects and arrow functions

Given the following coffeescript:

a = 
  b: -> @d
  c: => @d
  d: "ok"

What will each of the following return:

1

a.b() == "ok"

2

a.c() == "ok"

3

a.b.apply({}) == "ok"

4

a.c.apply({}) == "ok"
answer
  1. true
  2. false
  3. false
  4. false

the 'return' keyword

What is the return value of x() for the following function?

x = ->
  [1,2,3].map (i) ->
    return i if i % 2 == 0
    null
answer [null, 2, null]

async loops

Give the following sleep function:

sleep_1_sec = ->
  new Promise (resolve) ->
    setTimeout resolve, 1000

Which of the following countdown functions will work correctly?

1

countdown = ->
  for i in [1,2,3]
    await sleep_1_sec()
    console.log i
countdown()

2

countdown = ->
  [1,2,3].forEach (i) ->
    await sleep_1_sec()
    console.log(i)
countdown()
answer
  1. correct (prints numbers with 1-second sleep in between)
  2. incorrect (prints all numbers at once after 1 second)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment