Select Page
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
Filter by Categories
nmims post
Objective Type Set
Online MCQ Assignment
Question Solution
Solved Question
Uncategorized

1. Syntax for unless conditional statement is

```unless conditional [then]
code
else
code
end```

a) True
b) False

Answer: a [Reason:] Executes code if condition is false. If the condition is true, code specified in the else clause is executed.

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

Answer: a [Reason:] The unless conditional statement is true so the unless clause is not executed.

```Output:
x is greater than 2```

3. What is the output of the given code?

```var =  1
print "1 -- Value is setn" if var
print "2 -- Value is setn" unless var
var = false
print "3 -- Value is setn" 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

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

```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

Answer: a [Reason:] 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

Answer: a [Reason:] 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

Answer: b [Reason:] 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

Answer: b [Reason:] 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

Answer: b [Reason:] 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

Answer: c [Reason:] 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

Answer: b [Reason:] 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

```Output: