Visual Basic Questions and Answers – Working with String

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

1. The number of characters contained in a String is stored as Integer in String’s ____________
a) Substring property
b) Length property
c) Reverse property
d) Index property
View Answer

Answer: b
Explanation: The number of characters contained in a String is stored as Integer in String’s Length Property. The syntax to calculate length is string.Length. In the syntax, string can be a String variable, a String named constant, or the Text property of a control.

2. _______________ method is used to remove space from beginning and end of a string.
a) Trim
b) Remove
c) Truncate
d) DeleteSpace
View Answer

Answer: a
Explanation: Trim method is used to remove space from beginning and end of a string. The syntax is string.Trim. In the syntax, string can be a String variable, a String named constant, or the Text property of a control. When processing the Trim method, the computer first makes a temporary copy of the string in memory. It then performs the specified removal on the copy only. In other words, the method does not remove any characters from the original string. The method returns a string with the appropriate characters removed.

3. _______________________ method is used to remove specified number of characters located anywhere in the String.
a) Trim
b) Remove
c) Truncate
d) DeleteSpace
View Answer

Answer: b
Explanation: Remove method is used to remove specified number of characters located anywhere in the string. The syntax is string.Remove(startIndex[, numCharsToRemove]). In the syntax, string can be a String variable, a String named constant, or the Text property of a control. When processing the Remove method, the computer first makes a temporary copy of the string in memory. It then performs the specified removal on the copy only. In other words, the method does not remove any characters from the original string. The method returns a string with the appropriate characters removed.
advertisement
advertisement

4. What will contain in txtFirst after the following Visual Basic code is executed?

strName = "Penny Swanson"
txtFirst.Text = strName.Remove(5)

a) Penny
b) Swanson
c) Penny S
d) y Swanson
View Answer

Answer: a
Explanation: When number of characters is not mentioned in case of Remove method, it removes all the characters from the index or position provided. Thus it assigns string “Penny” to the txtFirst control’s Text property. You can also write the assignment statement as txtFirst.Text=strName.Remove(5,8).
Note: Join free Sanfoundry classes at Telegram or Youtube

5. What will be the value of num after the following Visual Basic code is executed?

advertisement
strName=”Veronica Yardley”
num=strName.Length

a) 15
b) 16
c) 14
d) 17
View Answer

Answer: b
Explanation: strName.Length stores the number of characters in the String strName as integer, i.e. it gives the length of the string, and belongs to the Steing’s Length Property.
advertisement

6. _____________________ method allows you to insert anywhere in the string.
a) Remove
b) Insert
c) Import
d) Delete
View Answer

Answer: b
Explanation: Visual Basic’s Insert method allows you to insert characters anywhere in a string. The syntax is as follows: string.Insert(startIndex, value). In the syntax, string can be a String variable, a String named constant, or the Text property of a control. When processing the Insert method, the computer first makes a temporary copy of the string in memory. It then performs the specified insertion on the copy only. In other words, the method does not affect the original string. The startIndex argument in the Insert method is an integer that specifies where in the string’s copy you want the value inserted.

7. What will be in strName after the following code is executed?

strName=”Joanne Hashen”
strName=strName.Insert(7,”C.”);

a) Joanne C. Hashen
b) JoanneC.hashen
c) Joanne
d) C.Hashen
View Answer

Answer: a
Explanation: The code assigns the string “Joanne C. Hashen” to the variable strName. The string “C.” is inserted in between at position 7, and it is inserted in the variable which contained the original String. Thus the original String will change, if no copy of the original String is changed.

8. ____________________ and ____________________ methods are used to align characters in a String.
a) PadLeft, PadRight
b) Left, Right
c) LeftAlign, RightAlign
d) AlignLeft, AlignRight
View Answer

Answer: a
Explanation: You can use Visual Basic’s PadLeft and PadRight methods to align the characters in a string. The methods do this by inserting (padding) the string with zero or more characters until the string is a specifi ed length; each method then returns the padded string. The syntax is as follows:

string.PadLeft(totalChars[, padCharacter])
string.PadRight(totalChars[, padCharacter])

9. If the padCharacter is omitted from the syntax of the padLeft or PadRight, the default is ______________
a) Space
b) *
c) /
d) ?
View Answer

Answer: a
Explanation: The optional padCharacter argument is the character that each method uses to pad the string until it reaches the desired number of characters. If the padCharacter argument is omitted, the default padding character is the space character.

10. The ________________ method pads the string on left, that is, it inserts the padded characters at the beginning of string.
a) PadLeft
b) PadRight
c) PadFront
d) PadBegin
View Answer

Answer: a
Explanation: The PadLeft method pads the string on the left, which means it inserts the padded characters at the beginning of the string; doing this right-aligns the characters within the string. The PadRight method, on the other hand, pads the string on the right, which means it inserts the padded characters at the end of the string and left-aligns the characters within 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.

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.