Visual Basic Questions and Answers – Sorting a One-Dimensional Array

«
»

This set of Visual Basic Multiple Choice Questions & Answers (MCQs) focuses on “Sorting a One-Dimensional Array”.

1. ________ method is used to sort an array in visual basic.
a) Array.arrange()
b) Array.arrayArrange()
c) Array.Sort()
d) Array.sortAscending()
View Answer

Answer: c
Explanation: You can use the Array.Sort method to sort a one-dimensional array’s values in ascending order. To sort the values in descending order, you first use the Array.Sort method to sort the values in ascending order, and then use the Array.Reverse method to reverse the values. The syntax is as follows, where the arrayName is the name of the array:
Array.Sort(arrayName)
advertisement

2. Arranging data in a specific order is called as ___________
a) Arranging
b) Sorting
c) Organizing
d) Making
View Answer

Answer: b
Explanation: In some applications, you might need to arrange the contents of an array in either ascending or descending order. Arranging data in a specific order is called sorting. When an array is sorted in ascending order, the first element in the array contains the smallest value and the last element contains the largest value. When an array is sorted in descending order, on the other hand, the first element contains the largest value and the last element contains the smallest value.

3. __________________ method is used to reverse the values.
a) Array.Reverse
b) Array.Sort Desc
c) Array.Sort Asc
d) Array.rev
View Answer

Answer: a
Explanation: You can use the Array.Sort method to sort a one-dimensional array’s values in ascending order. To sort the values in descending order, you first use the Array.Sort method to sort the values in ascending order, and then use the Array.Reverse method to reverse the values. The syntax is as follows, where the arrayName is the name of the array:
Array.Reverse(arrayName)

4. What will be the output of the following Visual Basic code?

Dim intScores As Integer = {78, 83, 75, 90}
Array.Sort(intScores)

a) 78, 83, 75,90
b) 75,78, 83,90
c) 78,75,83,90
d) 90,83,78,75
View Answer

Answer: b
Explanation: Array.Sort sorts the array in ascending order. In the question above it sorts the contents of the array in ascending order, as follows: 75, 78, 83, and 90.
advertisement

5. What will be the output of the following Visual Basic code?

Dim intScores As Integer = {78, 83, 75, 90}
Array.Reverse(intScores)

a) 78, 83, 75,90
b) 75,78, 83,90
c) 78,75,83,90
d) 90,75,83,78
View Answer

Answer: d
Explanation: Array.Reverse method reverses the array list. In the above question it reverses the contents of the array, placing the values in the following order: 90, 75, 83, and 78.

6. What will be the output of the following Visual Basic code?

advertisement
Dim intScores As Integer = {78, 83, 75, 90}
Array.Sort(intScores)
Array.Reverse(intScores)

a) 78, 83, 75,90
b) 75,78, 83,90
c) 78,75,83,90
d) 90,83,78,75
View Answer

Answer: d
Explanation: Array.Sort sorts the content of the array in ascending order, and then Array.Reverse reverses the array list, thus we get sorted array in descending order. In the above question, it sorts the contents of the array in ascending order and then reverses the contents, placing the values in descending order as follows: 90, 83, 78, and 75.

7. What is the array of these statements?

Dim intScores As Integer = {78, 83, 75, 90}
Array.reverse (intScores)

a) { 75, 78, 83, 90}
b) { 78, 75, 90, 83 }
c) { 90, 83, 78, 75 }
d) Compilation Error
View Answer

Answer: d
Explanation: The command to reverse array list is Array.Reverse, and not array.reverse, thus it leads to compilation error. The syntax is as follows, where the arrayName is the name of the array:
Array.Reverse(arrayName)
advertisement

8. What is the array of these statements?

Dim intScores As Integer = {78, 83, 75, 90}
Array.sort (intScores)

a) {75, 78, 83, 90}
b) {78, 75, 90, 83}
c) {90, 83, 78, 75}
d) Compilation Error
View Answer

Answer: d
Explanation: The command to sort array list is Array.Sort, and not Array.sort. Thus it leads to compilation error. The syntax is as follows, where the arrayName is the name of the array:
Array.Sort(arrayName)

9. Which of the following statements assigns (to the intElements variable) the number of elements contained in the intNums array?
a) intElements = Len(intNums)
b) intElements = Length(intNums)
c) intElements = intNums.Len
d) intElements = intNums.Length
View Answer

Answer: d
Explanation: The number of elements in a one-dimensional array is stored, as an integer, in the array’s Length property.The syntax is as follows:
arrayName.Length

10. Which of the following assigns the string “Rover” to the fifth element in a one-dimensional array named strPetNames?
a) strPetNames(4) = “Rover”
b) strPetNames[4] = “Rover”
c) strPetNames(5) = “Rover”
d) strPetNames.Items.Add(5) = “Rover”
View Answer

Answer: a
Explanation: An array is populated with the following syntax: arrayname(index)=”value”; where index is of between the range 0 to (length of array -1). Thus in this example, strPetnames(4)=”Rover”, is the proper initialization of the array.

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