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
1
NoMethodError
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
{ 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
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
{ 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 &:print
3
[[]].map { |elem| elem.class }.each { |elem| print elem }
answer
Array
NoMethodError
Array
--
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
NameError
NameErrr
Bar
What will the following return?
1
false if true && true
2
nil if true && true
answer
false
nil
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
NoMethodError
:bar
NoMethodError
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
0
false
false
:a
What is the result of each of the following (coffeescript):
1
NaN == NaN
2
typeof NaN
3
isNaN NaN
answer
false
'number'
true
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
'object'
'object'
error (constructor is not a function)
Object
true
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
true
true
false
true
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
true
false
false
false
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)