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?
!
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?
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?