Visual Basic Questions and Answers – Selection Structures – Logical Operators

This set of Visual Basic Multiple Choice Questions & Answers (MCQs) focuses on “Selection Structures – Logical Operators”.

1. __________ operator reverses the truth value of the operation.
a) AND
b) OR
c) NOT
d) NAND
View Answer

Answer: c
Explanation: NOT operator reverses the value of the operation. True becomes false and false becomes true. For example,

If NOT str Then

The above statement returns true, if the value contained in str is false. Else it returns False.

advertisement
advertisement

2. __________ operator returns true, if all the sub-conditions are true.
a) AND
b) OR
c) NOT
d) NAND
View Answer

Answer: a
Explanation: AND operator returns true if all the sub-conditions are true. It returns false, if any one of the sub-condition is false. For example,

If a AND b Then

The above statement returns true, if both a and b is true. If any one of a and b is false, it returns false.

3. __________ operator returns true, if any of the sub-condition is true.
a) AND
b) OR
c) NOT
d) NAND
View Answer

Answer: b
Explanation: OR operator returns true, if any of the sub-condition is true. It returns false if all the sub-conditions return false. For example,

advertisement
If a OR b Then

The above statement evaluates to true, if any one of a or b is true. If both a and b is false it returns false.

advertisement

4. And operator checks both the sub-conditions whereas AndAlso operator does __________
a) Short-circuit evaluation
b) Short-hand evaluation
c) Short evaluation
d) Circuit evaluation
View Answer

Answer: a
Explanation: And operator checks both the sub-conditions AndAlso operator does short-circuit evaluation which means in case of AndAlso operator, if any of the sub-condition is false it does not check the sub-conditions following it, but returns false.

5. And operator checks both the sub-conditions whereas OrElse operator does __________
a) Short-circuit evaluation
b) Short-hand evaluation
c) Short evaluation
d) Circuit evaluation
View Answer

Answer: a
Explanation: Or operator checks both the sub-conditions, OrElse operator does short-circuit evaluation which means in case of OrELse operator, if any of the sub-condition is true it does not check the sub-conditions following it, but returns true.

6. In the below statement when true is returned?

If str=”USE” Xor strln=”USE” Then

a) When both the sub-conditions are true
b) When only one sub-condition is true
c) When both the sub-conditions are false
d) It never returns true
View Answer

Answer: b
Explanation: Xor returns true when odd number of sub-conditions are true, otherwise it returns false. In the above statement, since we have two sub-conditions, Xor will return true, only when only one sub-condition is true, otherwise it will return false.

7. Which logical operator does not allow us to combine two or more conditions?
a) AND
b) OR
c) NOT
d) NAND
View Answer

Answer: c
Explanation: All logical operators except NOT allows us to combine two or more conditions or sub-conditions. NOT returns true if the condition is false, thus it works on only one condition; it cannot work on multiple more than one condition or sub-conditions.

8. What will be in Msg after the following code is evaluated?

If 5*3>3^2 AndAlso True OrElse False Then
Msg=”Hi”
Else
Msg=”Bye”
Endif

a) Hi
b) Bye
c) True
d) False
View Answer

Answer: a
Explanation: The condition in If statement is evaluated from left to right. When the first sub-condition is true, AndAlso checks the second sub-condition which is also true. Now OrElse check that the whole sub-condition before it is true, thus it does not evaluate the third sub-condition. Since the whole expression is true, it enters if and Msg contains Hi after the evaluation.

9. What will be in Msg after the following code is evaluated?

If 3>6 AndAlso 7>4 then
   Msg=”Hi”
Else
   Msg=”Bye”
EndIf

a) Hi
b) Bye
c) True
d) False
View Answer

Answer: b
Explanation: the condition in if is evaluated from left to right. When the AndAlso will see that the first sub-condition is false, it will return false, thus will not enter the If part. It will enter the else part, hence Msg will contain Bye.

10. What will be in Msg after the following code is evaluated?

If 5*3 OrElse 3^2 Then
   Msg=”Hi”
Else
   Msg=”Bye”
EndIf

a) Hi
b) Bye
c) True
d) False
View Answer

Answer: a
Explanation: The first sub-condition returns a value, means it does not return zero, thus it is true. Thus OrElse will not check the second sub-condition, but will return true. Since it returns true, thus it will evaluate the If part, hence Msg will contain Hi.

Sanfoundry Global Education & Learning Series – Visual Basic.

To practice all areas of Visual Basic, 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.