Visual Basic Questions and Answers – Using Pattern Matching to Compare Strings

«
»

This set of Visual Basic Questions and Answers for Aptitude test focuses on “Using Pattern Matching to Compare Strings”.

1. The _______________ operator allows you to use pattern matching characters to determine whether one String is equal to another String.
a) Like
b) Pattern
c) Match
d) StringMatch
View Answer

Answer: a
Explanation: The Like operator allows you to use pattern-matching characters to determine whether one string is equal to another string. The syntax is as follows:
string Like pattern
In the syntax, string can be a String variable, a String named constant, or the Text property of a control. Pattern is a String expression containing one or more of the pattern-matching characters.
advertisement

2. The ___________ in a pattern represents one character only.
a) ?
b) *
c) #
d) **
View Answer

Answer: a
Explanation: The question mark (?) character in a pattern represents one character only, whereas the asterisk (*) character represents zero or more characters. To represent a single digit in a pattern, you use the number sign (#) character.

3. The _____________ in a pattern represents zero or more characters.
a) ?
b) *
c) #
d) **
View Answer

Answer: b
Explanation: The question mark (?) character in a pattern represents one character only, whereas the asterisk (*) character represents zero or more characters. To represent a single digit in a pattern, you use the number sign (#) character.

4. The _____________ in a pattern represents a single digit.
a) ?
b) *
c) #
d) **
View Answer

Answer: c
Explanation: The question mark (?) character in a pattern represents one character only, whereas the asterisk (*) character represents zero or more characters. To represent a single digit in a pattern, you use the number sign (#) character.

5. The Like operator returns _______________ value.
a) Integer
b) Boolean
c) String
d) Float
View Answer

Answer: b
Explanation: The Like operator compares the string to the pattern; the comparison is case-sensitive. If the string matches the pattern, the Like operator returns the Boolean value True; otherwise, it returns the Boolean value False.
advertisement

6. Which of the following expressions evaluates to True when the strPart variable contains the string “123X45”?
a) strPart Like “999[A-Z]99”
b) strPart Like “######”
c) strPart Like “###[A-Z]##”
d) strPart Like “##??##”
View Answer

Answer: c
Explanation: The pattern in “###[A-Z]##” gives we have 3 digits followed by a character between A-Z and followed by two digits. It could also have been written as “###?##”.

7. In the following code, the while loop will execute if strId would have been _______________

Do While strId Like "###*"

a) 123ABC
b) Fgh2
c) Xzya
d) Xyza
View Answer

Answer: a
Explanation: The while loop will execute if the condition returned is true that is if strId will be 3 digits followed by zero or more number of characters, as we see in case of 123ABC where we have 3 digits followed by 3 characters.

8. In the following code, the body of If will execute if txtState.Text contains?

advertisement
If txtState.Text Like "K*" Then

a) K
b) K123
c) K4
d) 4K
View Answer

Answer: a
Explanation: The body of If will execute if the condition evaluates to True. The condition evaluates to True when the value in the txtState control’s Text property begins with the letter K followed by zero or more characters; otherwise, it evaluates to False. In this case we see that “K” matches the condition, thus if txtState.Text is “K” then it will execute the body of the If.

9. In the following code, the body of If will execute if strFirst contains?

If strFirst.ToUpper Like "B?LL" Then

a) Bill
b) Lii
c) LL1
d) 123
View Answer

Answer: a
Explanation: The body of If will execute if the condition evaluates to True. The condition evaluates to True when the string stored in the strFirst variable (converted to uppercase) begins with the letter B followed by one character and then the two letters LL; otherwise, it evaluates to False. In this case we see that “Bill” matches the condition, thus if strFirst is “Bill” then it will execute the body of the If.
advertisement

10. In the following code, the body of If will execute if strFirst contains?

If strFirst.ToUpper Like "T[OI]M" Then

a) TIM
b) TM
c) IM
d) OM
View Answer

Answer: a
Explanation: The body of If will execute if the condition evaluates to True. The condition evaluates to True when the string stored in the strFirst variable (converted to uppercase) is either “TOM” or “TIM”. When the variable does not contain “TOM” or “TIM” the condition evaluates to False. In this case we see that “TIM” matches the condition, thus if strFirst is “Tim” then it will execute the body of the If.

Sanfoundry Global Education & Learning Series – Visual Basic.

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