ruby
- inheritance vs nested classes
- the tap function
- the reduce function
- iterator shorthands
- constant lookup
- operator precedence
- requiring files
- Default hash values
javascript/coffeescript
- NaN
- more bullshit
- classes and arrow functions
- objects and arrow functions
- the 'return' keyword
- async loops
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
1NoMethodError
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
11{ foo: :bar }{ }
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
end2
def map_keys(hash, &blk)
hash.reduce({}) do |memo, (key, val)|
memo[blk.call key] = val
end
end3
def map_keys(hash, &blk)
hash.each_with_object({}) do |(key, val), memo|
memo[blk.call key] = val
end
endanswer
{ 2 => 2 }2{ 2 => 2 }
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 &:print3
[[]].map { |elem| elem.class }.each { |elem| print elem }answer
ArrayNoMethodErrorArray
--
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
NameErrorNameErrrBar
What will the following return?
1
false if true && true2
nil if true && trueanswer
falsenil
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 = :bar2
def foo; :bar; end3
foo = ->{ :bar }answer
NoMethodError:barNoMethodError
What is the result of each of the following?
1
Hash.new(0)[:a]2
Hash.new(0).has_key? :a3
Hash.new(:a).has_key? :a4
Hash.new { |h,k| h[k] = :a }[:a]answer
0falsefalse:a
What is the result of each of the following (coffeescript):
1
NaN == NaN2
typeof NaN3
isNaN NaNanswer
false'number'true
What is the result (or error) of each of the following (coffeescript):
1
typeof null2
typeof undefined3
{}.constructor()4
x = {}
x.constructor()5
!0answer
'object''object'error (constructor is not a function)Objecttrue
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
truetruefalsetrue
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
truefalsefalsefalse
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]
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
correct (prints numbers with 1-second sleep in between)incorrect (prints all numbers at once after 1 second)