Conditional Statements(Engineering > Computer Science And Engineering > Ruby Programming ) Questions and Answers
Question 1. What is the output of the given code?string = gets.chompcase stringwhen string = "a"print "alphabet a"when string = "b"print "alphabet b"when string = "c"print "alphabet c"else print "some other alphabet"end
alphabet a
b
alphabet b
alphabet c
Syntax error
Explanation:-
Answer: Option B. -> b
alphabet b
After taking input from the user will check which string is entered and goes to particular case statement. Output: b alphabet b
Question 2. What is the output of the code?variable="true".reverse if variable puts "true" else puts "false" end
False
True
Syntax error
None of the mentioned
Explanation:-
Answer: Option B. -> True
Condition is satisfied and value is not changed. Output: True
Question 3. What is the output of the given code?a=true b=false if a && b puts "False" elsif a || b puts "True" else puts "neither true nor false" end
false
true
neither true nor false
none of the mentioned
Explanation:-
Answer: Option B. -> true
True or false will always evaluate to true, hence the elsif block is executed.
Question 4. What is the output of the code? if 1>2 puts "false" end else puts "True" end
False
True
Syntax error
None of the mentioned
Explanation:-
Answer: Option C. -> Syntax error
No two end statements required after if and else just one end statement is sufficient.
Question 5. What is the output of the given code?if !true print "False" elsif !true || true print "True" end