# 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

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.

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

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) **

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) **

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

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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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 “##??##”

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

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?

`If txtState.Text Like "K*" Then`

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

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

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.

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

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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]