# Visual Basic Questions and Answers – Arrays – For Each..Next Statement

«
»

This set of Visual Basic Multiple Choice Questions & Answers (MCQs) focuses on “Arrays – For Each..Next Statement”.

1. Which of the following declares a five-element one-dimensional array?
a) Dim dblAmounts(4) As Double
b) Dim dblAmounts(5) As Double
c) Dim dblAmounts(4) As Double = {3.55, 6.70, 8, 4, 2.34}
d) Dim dblAmounts() As Double={3.55, 6.70, 8, 4, 2.34,1.45}

Explanation: The number of elements to be declared must be given in the definition, and the elements must be declared in within the second bracket as in Dim dblAmounts() As Double={3.55, 6.70, 8, 4, 2.34,1.45}; while it is only defined in Dim dblAmounts(5) As Double; but here it is not declared.

2. The intSales array is declared as follows: Dim intSales() As Integer = {10000, 12000, 900, 500, 20000}. The statement intSales(3) = intSales(3) + 10 will .
a) replace the 500 amount with 10
b) replace the 500 amount with 510
c) replace the 900 amount with 10
d) replace the 900 amount with 910

Explanation: intSales(3) is 500, and we add 10 to it, and it is replaced by 510. 500 is the fourth element, but since array index starts from 0, thus intSales(3) will contain 500. It would have been 900 if, it would have been intSales(2).

3. The strItems array is declared as follows: Dim strItems(20) As String. The intSub variable keeps track of the array subscripts and is initialized to 0. Which of the following Do clauses will process the loop instructions for each element in the array?
a)

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
`Do While intSub > 20`

b)

`Do While intSub < 20`

c)

`Do While intSub >= 20`

d)

` Do While intSub <= 20`
Explanation: To process each element, we need to process till strItems(19), which is the 20th element, thus to process all 20 elements, the do while loop should have intSub < 20.

4. The intSales array is declared as follows: Dim intSales() As Integer = {10000, 12000, 900, 500, 20000}. Which of the following If clauses determines whether the intSub variable contains a valid subscript for the array?
a)

`If intSales(intSub) >= 0 AndAlso intSales(intSub) < 4 Then`

b)

`If intSales(intSub) >= 0 AndAlso intSales(intSub) <= 4 Then`

c)

`If intSub >= 0 AndAlso intSub < 4 Then`

d)

`If intSub >= 0 AndAlso intSub <= 4 Then`
Explanation: intSub variable contains a valid subscript, within ≥0 and ≤ 4, since array initialization starts from 0, thus we have 5 elements; thus first element will be accessed as intSales(0) and the last as intSales(4). Thus, intsub should be ≥ 0 and ≤ 4.

5. The intNums array is declared as follows: Dim intNums() As Integer = {10, 5, 7, 2}. Which of the following blocks of code correctly calculates the average value stored in the array? The intTotal, intSub, and dblAvg variables contain the number 0 before the loops are processed.
a)

```Do While intSub < 4
intNums(intSub) = intTotal + intTotal
intSub = intSub + 1
Loop
dblAvg = intTotal / intSub```

b)

```Do While intSub < 4
intTotal = intTotal + intNums(intSub)
intSub = intSub + 1
Loop
dblAvg = intTotal / intSub```

c)

```Do While intSub < 4
intTotal = intTotal + intNums(intSub)
intSub = intSub + 1
Loop
dblAvg = intTotal / intSub − 1```

d)

```Do While intSub < 4
intTotal = intTotal + intNums(intSub)
intSub = intSub + 1
Loop
dblAvg = intTotal / (intSub − 1)```
Explanation: While calculating the average, we need to sum up all the elements and divide by total number of elements as in done in

```Do While intSub < 4
intTotal = intTotal + intNums(intSub)
intSub = intSub + 1
Loop
dblAvg = intTotal / intSub```

where, we calculate the sum in intTotal and average in dblAvg, where average is sum/no.of elements.

6. The intSales array is declared as follows: Dim intSales() As Integer = {10000, 12000, 900, 500, 20000}. Which of the following loops will correctly add 100 to each array element? The intSub variable contains the number 0 before the loops are processed.
a)

```Do While intSub <= 4
intSub = intSub + 100
Loop```

b)

```Do While intSub <= 4
intSales = intSales + 100
Loop```

c)

```Do While intSub < 5
intSales(intSub) =intSales(intSub) + 100
Loop```

d)

```Do While intSub <6
intSales(intSub) = intSales(intSub) + 100
Loop```
Explanation: Since we have 5 elements, thus the loop will run for < 5, and it will add 100 to each element. To access each element we use intSales(intSub), and add 100 to it, and update the array.

7. What will be assigned to the dblAvg variable after the code has been executed?

```Do While intSub < 4
intNums(intSub) = intTotal + intTotal
intSub = intSub + 1
Loop
dblAvg = intTotal / intSub```

Dim intNums() As Integer = {10, 5, 7, 2}. The intTotal, intSub, and dblAvg variables contain the number 0 before the loops are processed.
a) 0
b) 5
c) 6
d) 8

Explanation: At first intTotal is zero, everytime the loop runs, it makes value of each element 0. At the end intTotal remains 0, and intSub is the no. of elements. Thus 0/ anything leads to zero, thus answer is 0.

8. What will be assigned to the dblAvg variable after the code has been executed?

```Do While intSub < 4
intTotal = intTotal + intNums(intSub)
intSub = intSub + 1
Loop
dblAvg = intTotal / intSub```

Dim intNums() As Integer = {10, 5, 7, 2}. The intTotal, intSub, and dblAvg variables contain the number 0 before the loops are processed.
a) 0
b) 5
c) 6
d) 8

Explanation: At first, it sums up to 24 in the loop, which is the sum of all the elements in the array. It then calculates the average, by dividing the total by no. of elements which gives answer as 6, as we have 4 elements.

9. What will be assigned to the dblAvg variable after the code has been executed?

```Do While intSub < 4
intTotal = intTotal + intNums(intSub)
intSub = intSub + 1
Loop
dblAvg = intTotal / intSub – 1```

Dim intNums() As Integer = {10, 5, 7, 2}. The intTotal, intSub, and dblAvg variables contain the number 0 before the loops are processed.
a) 0
b) 5
c) 6
d) 8

Explanation: At first, it sums up to 24 in the loop, which is the sum of all the elements in the array. It then calculates the average, by dividing the total by no. of elements which gives answer as 6, as we have 4 elements; then it subtracts 1 from it, which results to 5.

10. What will be assigned to the dblAvg variable after the code has been executed?

```Do While intSub < 4
intTotal = intTotal + intNums(intSub)
intSub = intSub + 1
Loop
dblAvg = intTotal / (intSub − 1)```

Dim intNums() As Integer = {10, 5, 7, 2}. The intTotal, intSub, and dblAvg variables contain the number 0 before the loops are processed.
a) 0
b) 5
c) 6
d) 8

Explanation: At first, it sums up to 24 in the loop, which is the sum of all the elements in the array. It then calculates the average, by dividing the total by (no. of elements-1) which gives answer as 8, as we have 4 elements.

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. 