Visual Basic Questions and Answers – Searching a String

«
»

This set of Visual Basic Multiple Choice Questions & Answers (MCQs) focuses on “Searching a String”.

1. To determine whether a string has specific sequence of characters, use _________________
a) Contains method
b) Specific method
c) Sequence method
d) Check method
View Answer

Answer: a
Explanation: If you need to determine whether a string contains a specific sequence of characters, you can use either the Contains method or the IndexOf method. The syntax is as follows:

advertisement
string.Contains(subString)
string.IndexOf(subString[, startIndex])

In each syntax, string can be a String variable, a String named constant, or the Text property of a control. When processing the Contains and IndexOf methods, the computer first makes a temporary copy of the string in memory. It then performs the specified search on the copy only.

2. The ______________ argument in syntax of Contains method represents the sequence of arguments you are searching.
a) subString
b) SearchString
c) String
d) Seq
View Answer

Answer: a
Explanation: The subString argument in syntax of Contains method represents the sequence of characters for which you are searching. The method performs a case-sensitive search, which means the case of the subString must match the case of the string in order for both to be considered equal.

3. The Contains Method returns ______________ value.
a) Integer
b) Void
c) Boolean
d) String
View Answer

Answer: c
Explanation: The Contains method returns the Boolean value True when the substring is contained anywhere in the string; otherwise, it returns the Boolean value False. The Contains method always begins the search with the first character in the string.
advertisement

4. The Indexof method returns ________________ value.
a) Integer
b) Boolean
c) Void
d) String
View Answer

Answer: a
Explanation: The IndexOf method returns an integer— either –1 if the subString is not contained in the string or the character index that represents the starting position of the subString in the string. Unless you specify otherwise, the IndexOf method starts the search with the first character in the string. To specify a different starting location, you use the optional startIndex argument.

5. What output will be returned if the following Visual Basic code is executed?

strCityState = "Nashville, TN"
blnIsContained = strCityState.Contains("TN")

a) True
b) False
c) 11
d) 12
View Answer

Answer: a
Explanation: The Contains method returns true if the sequence of character is found. It would have returned 11 if it would have been IndexOf method which returns the index of the first character of the sequence.

6. What output will be returned if the following Visual Basic code is executed?

advertisement
strCityState = "Nashville, TN"
intCharIndex = strCityState.IndexOf("TN")

a) True
b) False
c) 11
d) 12
View Answer

Answer: c
Explanation: The IndexOf method returns the index of the first character of the sequence, thus it returns 11 as the index of the first character “T” is 11. If it would have been Contains Method, it would have returned True.

7. What output will be returned if the following Visual Basic code is executed?

strCityState = "Nashville, TN"
blnIsContained = strCityState.Contains("Tn")

a) True
b) False
c) 11
d) 12
View Answer

Answer: c
Explanation: The Contains Method is case sensitive, thus returns false since the string “Tn” is not available, but the string “TN” is. It would have returned true if it would have been “TN” instead of “Tn”.
advertisement

8. What output will be returned if the following Visual Basic code is executed?

strCityState = "Nashville, TN"
intCharIndex = strCityState.IndexOf("Tn")

a) True
b) False
c) 11
d) -1
View Answer

Answer: d
Explanation: The Indexof method is case sensitive, thus returns -1 since the sequence do not match. If the sequence would have matched, it would have returned the index of the first character of the sequence. It would have matched if the sequence would have been “TN’ instead of “Tn”.

9. ________________ method is used for accessing any number of characters in a String.
a) Substring
b) Contains
c) IndexOf
d) CharAt
View Answer

Answer: a
Explanation: Visual Basic provides the Substring method for accessing any number of characters in a string. The syntax is as follows:

string.Substring(startIndex[, numCharsToAccess])

In the syntax, string can be a String variable, a String named constant, or the Text property of a control. When processing the Substring method, the computer first makes a temporary copy of the string in memory. It then accesses the specified number of characters in the copy only. The startIndex argument in the syntax is the index of the first character you want to access in the string’s copy.

10. _________________ argument in the Substring syntax specifies number of characters you want to access.
a) numCharToAccess
b) CharAccess
c) numAccess
d) ToAccess
View Answer

Answer: a
Explanation: The optional numCharsToAccess argument in the Substring syntax specifies the number of characters you want to access. The Substring method returns a string that contains the number of characters specified in the numCharsToAccess argument, beginning with the character whose index is startIndex. If you omit the numCharsToAccess argument, the Substring method returns all characters from the startIndex position through the end of the string.

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.

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