Working with String - Visual Basic Questions and Answers

1.

The number of characters contained in a String is stored as Integer in String’s ____________

   A.) Substring property
   B.) Length property
   C.) Index property
   D.) Reverse property

Answer: Option 'B'

Length property

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.

DigitalOcean Referral Badge

2.

______________ method is used to remove space from beginning and end of a string.

   A.) Trim
   B.) Remove
   C.) DeleteSpace
   D.) Truncate

Answer: Option 'A'

Trim
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.

DigitalOcean Referral Badge

3.

___________________ method is used to remove specified number of characters located anywhere in the String.

   A.) Trim
   B.) Remove
   C.) DeleteSpace
   D.) Truncate

Answer: Option 'B'

Remove

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.

DigitalOcean Referral Badge

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.) y Swanson
   D.) Penny S

Answer: Option 'A'

Penny
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).

DigitalOcean Referral Badge

5.

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

strName=”Veronica Yardley”
num=strName.Length

   A.) 15
   B.) 16
   C.) 17
   D.) 14

Answer: Option 'B'

16

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.

DigitalOcean Referral Badge

6.

_______________ method allows you to insert anywhere in the string.

   A.) Remove
   B.) Insert
   C.) Delete
   D.) Import

Answer: Option 'B'

Insert

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.

DigitalOcean Referral Badge

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.) Joanne
   C.) C.Hashen
   D.) JoanneC.hashen

Answer: Option 'A'

Joanne C. Hashen

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.

DigitalOcean Referral Badge

8.

__________________ and ____________________ methods are used to align characters in a String.

   A.) PadLeft, PadRight
   B.) AlignLeft, AlignRight
   C.) LeftAlign, RightAlign
   D.) Left, Right

Answer: Option 'A'

PadLeft, PadRight

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

DigitalOcean Referral Badge

9.

If the padCharacter is omitted from the syntax of the padLeft or PadRight, the default is ______________

   A.) Space
   B.) /
   C.) ?
   D.) *

Answer: Option 'A'

Space

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.

DigitalOcean Referral Badge

10.

The ________________ method pads the string on left, that is, it inserts the padded characters at the beginning of string.

   A.) PadFront
   B.) PadRight
   C.) PadBegin
   D.) PadLeft

Answer: Option 'D'

PadLeft

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.

DigitalOcean Referral Badge

Working with String - Visual Basic Questions and Answers Download Pdf

Recent Posts