Skip to content

Instantly share code, notes, and snippets.

@maxp-edcast
Forked from MaxPleaner/quiz.md
Last active July 17, 2018 02:21
Show Gist options
  • Save maxp-edcast/1b5465a1c7ef9a5e6eb45087ab0b5e94 to your computer and use it in GitHub Desktop.
Save maxp-edcast/1b5465a1c7ef9a5e6eb45087ab0b5e94 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
  9. re-implement Proc#new
  10. index_by vs group_by
  11. break vs return
  12. safe navigation

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

re-implement Proc#new

Proc#new is used like this:

fn = Proc.new { |arg| arg + 1 }
fn.call(1) # => 2

How would you implement the Proc class to have this behaviour?

answer
    class Proc
      def initiailize(&blk)
        @blk = blk
      end
      def call
        @blk.call
      end
    end
  

index_by vs group_by

What is the output of each of the following?

1

[1,2,3,4].index_by { |x| x % 2 == 0 }

2

[1,2,3,4].group_by { |x| x % 2 == 0 }
answer
  1. { false => 3, true => 4 }
  2. { false => [1,3], true => [2,4] }

break vs returnn

What will be printed by the following?

1

def foo
  loop { puts "1"; loop { puts "2"; break }; puts "3"; break }
end

2

def foo
  loop { print "1"; loop { print "2"; return }; print "3"; break }
end
answer
  1. 123
  2. 12

safe navigation

What will be printed by the following?

1

nil&.puts(puts(1).class)
answer Nothing


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