1. It’s a good habit to give two spaces between if statement and condition.
a) True
b) False
Explanation: Not necessary but good coders follow it.
2. What is the use of else statement?
a) When the if condition is false then the next else condition will get executed
b) When the if condition is false then the elsif condition will get executed
c) When the if condition is false and if else condition is true then only it will get executed
d) none of the mentioned
Explanation: When the if expression gives false value and the else condition is true then only it will get executed.
3. Is the following syntax correct?
if conditional
code...
elsif conditional
code..
else
code
end
a) True
b) False
Explanation: All the three statements execute only when it’s true.
4. What is the output of the given code?
if 1>2
puts "false"
else
puts "True"
a) False
b) True
c) Syntax error
d) None of the mentioned
Explanation: End statement is always must after the completion of else statement.
5. What is the output of the code?
if 1>2
puts "false"
end
else
puts "True"
end
a) False
b) True
c) Syntax error
d) None of the mentioned
Explanation: No two end statements required after if and else just one end statement is sufficient.
6. What is the output of the code?
variable=true
if variable
puts "true"
else
puts "false"
end
a) False
b) True
c) Syntax error
d) None of the mentioned
Explanation: The condition is satisfied hence the if block code is executed.
7. What is the output of the code?
variable="true".reverse
if variable
puts "true"
else
puts "false"
end
a) False
b) True
c) Syntax error
d) None of the mentioned
Explanation: Condition is satisfied and value is not changed.
8. What is the output of the code?
variable=true
if !variable
puts "true"
else
puts "false"
end
a) False
b) True
c) Syntax error
d) None of the mentioned
Explanation: Condition is not satisfied so else part is executed.
9. What is the output of the code?
variable="true".length
if variable
puts "true"
else
puts "false"
end
a) False
b) True
c) Syntax error
d) 4
Explanation: Condition is satisfied, and it is not effected by length method.
10. Which of the following is valid conditional statement?
a) else
b) els
c) if else
d) None of the mentioned
Explanation: Else is valid conditional statement.