Visual Basic Questions and Answers – Strings in Selection Structures

«
»

This set of Visual Basic online test focuses on “Strings in Selection Structures”.

1. String comparison in Visual basic is case-sensitive.
a) True
b) False
View Answer

Answer: a
Explanation: String comparisons in Visual Basic are case-sensitive, which means that the string “Yes” is not the same as either the string “YES” or the string “yes”. A problem occurs when a comparison needs to include a string that is either entered by the user or read from a file, because you cannot always control the case of the string.
advertisement

2. __________ is used to converting a string to uppercase.
a) toUpper
b) toLower
c) toUpperCase
d) toLowerCase
View Answer

Answer: a
Explanation: Before using a string for comparison you may convert it to either uppercase or lowercase, because you are unaware whether the data read from file or the user input is in lower case or upper case. To do this you use the toUpper method to convert a string to uppercase and toLower method to convert a string to lowercase.

3. The __________ operator reverses the value of the condition.
a) AND
b) OR
c) NOT
d) NAND
View Answer

Answer: c
Explanation: The NOT operator reverses the value of the condition. Let a condition returns TRUE and we write ~condition, the statement will return FALSE. It happens in case of expression also.

4. If the txtPrice control contains the value 75, what value will the variable=Decimal.TryParse (txtPrice.Text,decPrice) method return?
a) False
b) True
c) 75
d) 75.00
View Answer

Answer: b
Explanation: The tryparse method converts the string 75 to its decimal value in decPrice as 75.00 and also returns True to variable, i.e. after converting from string to decimal, it returns a Boolean whether it was able to convert or not.

5. If the txtPrice control contains the value 75, what value will the Decimal.TryParse (txtPrice.Text,decPrice) method return?
a) False
b) True
c) 75
d) 75.00
View Answer

Answer: d
Explanation: The tryparse method converts the string 75 to its decimal value in decPrice as 75.00. In this case, it does not return a Boolean value, since we have not assigned any variable to hold the Boolean value.
advertisement

6. What will be the output of the following Visual Basic code, If the intnumber variable is 90?

If intnumber<=100 Then
   Intnumber=intnumber*2;
Else
   Intnumber=intnumber*3;
EndIf

a) 156
b) 234
c) 180
d) 270
View Answer

Answer: c
Explanation: Since the intnumber is 90, i.e. it is less than 100, thus if part of the code will be executed, i.e., the else part of the code will not be executed. Thus intnumber=intnumber*2 will be executed, thus 90*2=180 will be stored in intnumber.

7. What will be the output of the following Visual Basic code, If the intnumber variable is 110?

If intnumber<=100 Then
   Intnumber=intnumber*2;
Else
   Intnumber=intnumber*3;
EndIf

a) 156
b) 270
c) 180
d) 330
View Answer

Answer: d
Explanation: Since the intnumber is 110, i.e. it is greater than 100, thus else part of the code will be executed, i.e., the if part of the code will not be executed. Thus intnumber=intnumber*3 will be executed, thus 110*3=330 will be stored in intnumber.
advertisement

8. Which is used to check both the conditions in a given if statement?
a) OrElse
b) AndAlso
c) NOT
d) OR
View Answer

Answer: b
Explanation: AndAlso is used to check both the conditions in a single if statement. For example:

If str<>”p” AndAlso str<>”P” Then
   Var=”Pass”
Else
   Var=”Fail”
EndIf

In the above example if both the condition is True then if part is executed otherwise, else part is executed, i.e. if both the condition that is str is not equal to “p” and str is not equal to “P” then only if part is executed, otherwise else part is executed.

9. Which is used to check one among both the conditions in a given if statement?
a) OrElse
b) AndAlso
c) NOT
d) OR
View Answer

Answer: a
Explanation: OrElse is used to check one among both the conditions in a single if statement. For example:

advertisement
If str<>”p” OrElse str<>”P” Then
   Var=”Pass”
Else
   Var=”Fail”
EndIf

In the above example if any one of the condition is True then if part is executed otherwise, else part is executed, i.e. if any one of the condition that is either str is not equal to “p” or str is not equal to “P” then only if part is executed, otherwise else part is executed.

10. In the following Visual Basic code, what will be in msg, if str contains “ik”?

Dim str as String
Dim  msg as String
If str.toUpper=”IK” 
   msg=”Hi”
Else
   msg=”Bye”
EndIf

a) Hi
b) Bye
c) Compiler Error
d) Logical Error
View Answer

Answer: a
Explanation: str contains “ik”. When we do str.toUpper, the string in str is changed to “IK”. Thus, the comparison satisfies, and the if statement is executed, since the comparison returns a true value. Thus msg will contain “Hi”.

Sanfoundry Global Education & Learning Series – Visual Basic.

To practice all areas of Visual Basic for online tests, here is complete set of 1000+ Multiple Choice Questions and Answers.

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!

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn