Ruby Questions and Answers Part-9

1. What is the output of the given code?
print "2 is less than 3" unless 2>3
a) 2>3
b) 2 is less than 3
c) Syntax error
d) None of the mentioned

Answer: b
Explanation: As 2<3 for unless statement to execute the condition given is false.

2. What is the output of the given code?
x=8
y=10
unless x>y
puts "x is less than y"
end
unless x>y+1
puts "x is less than y+1"
end
a) x is less than y
b) x is less than y+1
c) x is less than y
x is less than y+1
d) None of the mentioned

Answer: c
Explanation: x is always less than y and y+1 hence both the condition will evaluate to false and corresponding code is executed.

3. What is the output of the given code?
x="ruby".length
y="language".length
puts x,y
unless x>y
print "length of x is less than that of y"
end
a) 4
8
b) 4
8
length of x is less than that of y
c) Syntax error
d) None of the mentioned

Answer: b
Explanation: The .length method will give the length of the string.

4. What is the output of the given code?
x=8
y=10
unless x<y
puts "x is less than y"
end
unless x>y+1
puts "x is less than y+1"
end
a) x is less than y
b) x is less than y+1
c) x is less than y
x is less than y+1
d) None of the mentioned

Answer: b
Explanation: x is always less than y and y+1 hence only the second condition will evaluate to false and corresponding code is executed.

5. The following syntax is used for the ruby case statement.
case expression
when expression , expression ... then
code ...
else
code
end
a) True
b) False

Answer: a
Explanation: The following syntax is used for ruby case statement.

6. What is the output of the given code?
age = 4
case age
puts "baby" when 0 .. 2
puts "little child" when 3 .. 6
puts "child" when 7 .. 12
puts "youth" when 13 .. 18
puts "adult" else
end
a) adult
b) youth
c) child
d) syntax error

Answer: d
Explanation: The following code will give the error because the syntax is not similar to case statements syntax.

7. What is the output of the given code?
string = gets.chomp
case string
when string = "a"
print "alphabet a"
when string = "b"
print "alphabet b"
when string = "c"
print "alphabet c"
else
print "some other alphabet"
end
a) alphabet a
b) b
alphabet b
c) alphabet c
d) Syntax error

Answer: b
Explanation: After taking input from the user will check which string is entered and goes to particular case statement.
Output:
b
alphabet b

8. The expression specified by the when clause is evaluated as the left operand. If no when clauses match, case executes the code of the else clause.
a) True
b) False

Answer: a
Explanation: Else clause is executed only when no ‘when’ clauses match.

9. What is the output of the given code?
length=gets.chomp
case length.length
when length=4
print "length is 4"
when length=5
print "length is 5"
end
a) ruby
length is 4
b) ruby
length is 5
c) abc
length is 4
d) syntax error

Answer: a
Explanation: Input string length is taken into consideration and then the case statement is executed.
Output:
ruby
length is 4

10. What is the output of the given code?
length=gets.chomp
case length.reverse.length
when length=4
print "length is 4"
when length=5
print "length is 5"
end
a) ruby
length is 4
b) ruby
length is 5
c) abc
length is 4
d) syntax error

Answer: a
Explanation: The reverse method does not effect the length of the string.