Method Quiz

2.5.round is an example of ____.

Variable assignment Variable definition Method definition Method invocation 2.5.round invokes the round method upon the receiver 2.5.
def what_am_i
  "When you look into an abyss, the abyss also looks into you."
end

The above code snippet is an example of ____.

Variable assignment Variable definition Method definition Method invocation The snippet defines the what_am_i method but doesn't invoke it.

What is the receiver of 2.5.round?

2 round 2.5 There is no receiver. 2.5 is the receiver, the object upon which the method is called. 3 is the return value of 2.5.round. round is the method itself.

What is the argument of 2.gcd(4)?

2 gcd 4 2 and 4 are both arguments. 4 is the argument. 2 is the receiver. gcd is the method itself.

What is the return value of 2.even??

true false 2 even? 2 is the receiver, even? is the method, and invoking the even? method with the receiver 2 returns true.
def pick_me #A
  "pretty please"

def the_perfect_definition(arg_one, arg_two) #B
  "look no further than my name"
end

def seriously_valid(arg_one arg_two) #C
  "I'm seriously"
end

def def(arg_one, arg_two) #D
  "I'm DEFinitely valid"
end

Which of the above method definitions uses valid syntax?

A B C D The second choice is correct. The first lacks an end. The third lacks a comma between arguments. The fourth uses a Ruby keyword (def) as a method name, which is invalid.
def return_this(arg)
  arg
end

return_this("return me, please")

What is the return value of the above method invocation?

"return me, please" There is no return value. nil "arg" The snippet invokes the return_this method with the argument "return me, please". In the method body, arg is therefore assigned to "return me, please" upon invocation. The value of arg is then implicitly returned.
def pitter_putter(arg)
  puts arg
end

pitter_putter("patter")

What is the return value of the above method invocation?

"patter" There is no return value. nil "arg" The last line of the method (puts arg) is implicitly returned. Because the return value of puts is always nil, the return value of pitter_putter("patter") is itself nil.
def nihilist(arg)
  return nil
  arg
end

nihilist("Nietzsche")

What is the return value of the above method invocation?

"Nietzsche" There is no return value. nil "arg" The method definition explicitly returns nil. The method's subsequent code (arg) is unreachable and is therefore never executed.
def nihilist(arg)
  return nil
  arg
end

What code in the above method definition is unreachable?

arg end nil return nil See the prior explanation.
def nihilist(arg)
  return nil
  arg
end

nihilist("Nietzsche").to_s

What does the above method chain return?

"Nietzsche" The interpreter throws an error nil "" The return value of nihilist("Nietzsche") is nil. nil.to_s is an empty string ("").
def cat
  "Cat"
end

def dog
  "Dog"
end

def catdog
  cat + dog + " was cartoon about a conjoined felnine"
end

catdog

Does the above code snippet throw an error?

Yes No cat and dog are valid helper methods of catdog.

results matching ""

    No results matching ""