Control Flow Quiz

if ["a", "b", "c"] == ("a".."c").to_a
  if "that was a guess,".length > 5
    "let's hope you know your " + (1..3).to_a.join + "'s."
  else
    "You know your " << ("a".."c").to_a.join + "'s."
  end
elsif 1 == 1 || 1 < 0
  # this statement is erroneous, but will its error be thrown?
  # recall that arrays cannot be compared except for equality.
  # (this wasn't a dogs > cats joke)
  ["dog"] < ["cat"]
else
  "That this statement is executed is " + ("reverse" == "reverse".reverse).to_s
end

Which subordinate block is executed in the above conditional statement?

"let's hope you know your" + (1..3).to_a.join + "'s." "You know your" << ("a".."c").to_a.join + "'s." "That this statement is executed is " + ("reverse" == "reverse".reverse).to_s ["dog"] < ["cat"] ["a", "b", "c"] == ("a".."c").to_a is true. The Ruby interpreter therefore evaluates the conditional statement nested under the if statement. "that was a guess,".length > 5 is also true. The Ruby interpreter therefore executes the subordinate block: "let's hope you know your" + (1..3).to_a.join + "'s." It executes no other code in the conditional statement.
if !(5 > 4 && 0 < 4)
  puts "I'm having an existential crisis."
end

Will puts "I'm having an existential crisis" be executed in the above block?

Yes No The ! operator reverses the boolean value of (5 > 4 && 0 < 4), which is true. The conditional expression is therefore false, and puts "I'm having an existential crisis." is never executed.
while true
  "Homer composed the Odyssey; given infinite time, with infinite circumstances and changes, it is impossible that the Odyssey should not be composed at least once."
end

Is the above code snippet an infinite loop?

Yes No The while keyword directs the interpreter to loop until a false condition is met. Because a false condition is never met, the loop is infinite.
counter = 0
while counter < 10
  counter = counter + 1
end

For how many iterations will the above loop execute?

10 9 11 None The loop executes ten times (from counter values 0 to 9).
(0..1000).each {|el| puts "How long will this last...?" }

How many iterations does the Ruby interpreter perform in the above code snippet?

1 Infinite 1000 1001 The Ruby interpreter performs an iteration once for each element in the receiver. There are 1001 elements in the range, so it performs 1001 iterations.
["a", "b", "c"].each_index {|i| puts "i am one #{i} older." }

What's the last statement printed (excluding the return value) in the above code snippet?

"i am one i older" "i am one c older" "i am one 2 older" "i am one 3 older" The value of i in the last iteration is the last index (2) in the receiver. The last statement printed is "i am one 2 older".
  "To strive, to seek, to find, and not to yield.".each_char do |ch|
    puts "Who's talking about yield?"
    puts "That topic's for a later date!"
  end

What does the above code snippet return?

"." "To strive, to seek, to find, and not to yield." nil "That topic's for a later date!" The return value of each, each_char, and each_index is the receiver, hence "To strive, to seek, to find, and not to yield." is the return value.

results matching ""

    No results matching ""