Ruby Programming Questions and Answers – The Unless Conditional Statement

«
»

This set of Ruby Programming Interview Questions and Answers for freshers focuses on “The Unless Conditional Statement”.

1. Syntax for unless conditional statement is

advertisement
unless conditional [then]
   code
else
   code 
end

a) True
b) False
View Answer

Answer: a
Explanation: Executes code if condition is false. If the condition is true, code specified in the else clause is executed.
advertisement
advertisement

2. What is the output of the given code?

x=3
unless x>2
puts "x is less than 2"
else
puts "x is greater than 2"
end

a) x is greater than 2
b) x is less than 2
c) 3
d) None of the mentioned
View Answer

Answer: a
Explanation: The unless conditional statement is true so the unless clause is not executed.

advertisement
Output: 
x is greater than 2

3. What is the output of the given code?

advertisement
var =  1
print "1 -- Value is set\n" if var
print "2 -- Value is set\n" unless var
var = false
print "3 -- Value is set\n" unless var

a) 1–Value is set
b) 2–Value is set
c) 1–Value is set
2–Value is set
d) 1–Value is set
3–Value is set
View Answer

Answer: d
Explanation: if condition is evaluated to true so it is executed and the second unless condition is evaluated to false so it is also executed.

advertisement
Output:
   1--Value is set
   3--Value is set

4. What is the output of the given code?

hungry=false
unless hungry
 print "Not hungry"
else
 print "Hungry"
end

a) Not hungry
b) Hungry
c) Syntax error
d) None of the mentioned
View Answer

Answer: a
Explanation: As hungry is initialized to false hence the unless condition is executed.

Output:
Not hungry

5. The following syntax is also used for unless conditional statement.

code unless conditional

a) True
b) False
View Answer

Answer: a
Explanation: The unless condition must be false in order to execute the code.

6. What is the output of the given code?

counter=12
unless counter
 print counter+1
else
 print counter+2
end

a) 13
b) 14
c) 15
d) None of the mentioned
View Answer

Answer: b
Explanation: Counter is assigned the value 1, so the unless conditional statement is true and hence it is not executed.

Output:
14

7. What is the output of the given code?

unless true && false
print "false"
else 
    print "ruby"
end

a) True
b) False
c) Nil
d) Syntax error
View Answer

Answer: b
Explanation: true && false will evaluate to false so unless block will get executed.

Output:
false

8. 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
View Answer

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

9. 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
View Answer

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.

Output:
x is less than y
x is less than y+1

10. 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
View Answer

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

Output:
4
8
length of x is less than that of y

11. 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
View Answer

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.

Output:
x is less than y+1

Sanfoundry Global Education & Learning Series – Ruby Programming.

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

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 & technical discussions at Telegram SanfoundryClasses.