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
  1.    alphabet a
  2.    b alphabet b
  3.    alphabet c
  4.    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
  1.    False
  2.    True
  3.    Syntax error
  4.    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
  1.    false
  2.    true
  3.    neither true nor false
  4.    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
  1.    False
  2.    True
  3.    Syntax error
  4.    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
  1.    True
  2.    False
  3.    Syntax eroor
  4.    None of the mentioned
Explanation:-
Answer: Option A. -> True


!true || true evaluates to true, hence elsif block gets executed.
Output:
True



Question 6. What error does the if condition gives if not terminated with end statement?
  1.    Syntax error
  2.    Unexpected end
  3.    Expecting keyword end
  4.    All of the mentioned
Explanation:-
Answer: Option D. -> All of the mentioned





Question 7. What is the output of the given code?hungry=falseunless hungry print "Not hungry"else print "Hungry"end
  1.    Not hungry
  2.    Hungry
  3.    Syntax error
  4.    None of the mentioned
Explanation:-
Answer: Option D. -> None of the mentioned


As hungry is initialized to false hence the unless condition is executed.
Output:
Not hungry



Question 8. What is the output of the given code?if 11>2   puts "Eleven is greater than two"   end   print "You'r right"
  1.    Eleven is greater than two
  2.    You'r right
  3.    Eleven is greater than two You'r right
  4.    None of the mentioned
Explanation:-
Answer: Option C. -> Eleven is greater than two You'r right


If condition is true then code is executed and then it comes out and then execute the remaining code.
Output:
Eleven is greater than two
You'r right