250+ TOP MCQs on Arrays – For Each..Next Statement and Answers

Visual Basic Multiple Choice Questions 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}

Answer: d
Clarification: 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

Answer: b
Clarification: 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. 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

  250+ TOP MCQs on User Interface - Controlling the Tab Order and Answers

Answer: d
Clarification: 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.

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

View Answer

Answer: b
Clarification: 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)

View Answer

  250+ TOP MCQs on Adding a GroupBox to the Form and Using Financial.Pmt Method

Answer: b
Clarification: 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

View Answer

Answer: c
Clarification: 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

Answer: a
Clarification: 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.

  250+ TOP MCQs on Multiple Alternative Selection Structures and Answers

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

Answer: c
Clarification: 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

Answer: b
Clarification: 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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top