Ruby Programming Questions and Answers – The If Conditional Statement

This set of Ruby Programming Multiple Choice Questions & Answers (MCQs) focuses on “The If Conditional Statement”.

1. The following syntax is correct for if conditional statement.

 if condition
    code
   end

a) True
b) False
View Answer

Answer: a
Explanation: The condition is true then only the code inside will execute.
advertisement
advertisement

2. If expression.
The expression can be of which type?
a) True
b) Any number
c) Any string
d) All of the mentioned
View Answer

Answer: d
Explanation: The condition must be true then if statement works.

3. What error does the if condition gives if not terminated with end statement?
a) Syntax error
b) Unexpected end
c) Expecting keyword end
d) All of the mentioned
View Answer

Answer: d
Explanation: We will get all the three errors.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

4. What is the output of the following?

if 1<2
   print "one is less than two"
   end

a) One is less than two
b) Syntax error
c) 1<2
d) None of the mentioned
View Answer

Answer: a
Explanation: The given condition is true so print statement gets executed.

advertisement
Output:
One is less than two

5. What is the output of the code?

advertisement
 if 11<2
   print "Eleven is less than two"
   end
   print "11 is greater"

a) 11 is greater
b) Eleven is less than two
c) Eleven is less than two 11 is greater
d) None of the mentioned
View Answer

Answer: a
Explanation: The condition given in the if statement is not true hence it comes out of the if block.

Output:
11 is greater

6. What is the output of the given code?

if 11>2
   puts "Eleven is greater than two"
   end
   print "You'r right"

a) Eleven is greater than two
b) You’r right
c) Eleven is greater than two
You’r right
d) None of the mentioned
View Answer

Answer: c
Explanation: 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

7. What is the output of the given code?

if 79>78
   puts "True".upcase
   if 9>8
   puts "True".reverse
   if 7==7
   puts "equal".downcase
   end
   end
   end

a) True
b) True eurt
c) TRUE
eurT
equal
d) equal
View Answer

Answer: c
Explanation: String methods are used and every condition is true, hence every if block is executed.

Output:
TRUE
eurT
equal

8. What is the output of the given code?

if 79>78
   puts "True".upcase
   if 9>8
   puts "True".Upcase
   if 7==7
   puts "equal".downcase
   end
   end
   end

a) True
b) Error
c) True,error
d) TRUE
Undefined method `Upcase’ for String
View Answer

Answer: d
Explanation: Method should always be in smallcase.

Output:
TRUE
Undefined method `Upcase' for String

9. If statement inside if statement is called Nested if statements.
a) True
b) False
View Answer

Answer: a
Explanation: Nested if means statement inside statement.

10. What is the output of the given code?

x=1
    if x > 2
    puts "x is greater than 2"
    elsif x <= 2 and x!=0
    puts "x is 1"
    else
    puts "I can't guess the number"
    end

a) x is greater than 2
b) x is 1
c) I can’t guess the number
d) None of the mentioned
View Answer

Answer: b
Explanation: The given condition is false so no other statement is executed.

Output:
x is 1

11. It’s a good habit to give two spaces between if statement and condition.
a) True
b) False
View Answer

Answer: a
Explanation: Not necessary but good coders follow it.

Sanfoundry Global Education & Learning Series – Ruby Programming.

To practice all areas of Ruby Programming, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.