wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Visual Basic Ch.5 Loops

Total questions: 20

Worksheet time: 19mins

Name
Class
Date
1.
What is wrong with the following Do loop?   Dim index As Integer = 1
Do While index <> 9  
MsgBox("Hello")
index =index+1   
Loop
a)
(a)The test variable should not be changed inside a Do loop. 
b)
(b) The test condition will never be true. 
c)
(c) This is an infinite loop. 
d)
(d) Nothing 
2.
Private Sub disp ()
Dim num as Double = 10  
Do While num > 1
MsgBox(num)
 num = num - 3
Loop
End Sub
 What numbers will be displayed in the list box when the button is clicked? 
a)
(a) 10, 7, and 4 
b)
(b)  10, 7, 4, and 1 
c)
(c)10, 7, 4, 1, and -2 
d)
(d) no output
3.
Which While statement is equivalent to Until num < 100? 
a)
(A)  While num <= 100 
b)
(B) While num > 100 
c)
(c) While num >= 100 
d)
(d) (A)  There is no equivalent While statement. 
4.
What is wrong with the following Do loop?   Dim index As Integer = 1
 Do While index <> 10  
MsgBox("Hello")
index =index+ 2  
Loop
a)
(A)  It should have been written as a Do Until loop. 
b)
(B) It is an infinite loop. 
c)
(C)  The test variable should not be changed within the loop itself. 
d)
(D) Nothing
5.
In analyzing the solution to a program, you conclude that you want to construct a loop so that the loop terminates either when (a < 12) or when (b = 16). Using a Do loop, the test condition should be 
a)
(A)  Do While (a > 12) Or (b <> 16) 
b)
(B)  Do While (a >= 12) Or (b <> 16) 
c)
(c)  Do While (a < 12) Or (b <> 16) 
d)
(D) (A)  Do While (a >= 12) And (b <> 16) 
6.
When Visual Basic executes a Do While loop it first checks the truth value of the _________. 
a)
(A)  pass 
b)
(B)  loop 
c)
(C) condition 
d)
(D) statement
7.
If the loop is to be executed at least once, the condition should be checked at the __________. 
a)
(A)  top of the loop 
b)
(B)  middle of the loop 
c)
(C)  bottom of the loop 
d)
(D) Nothing should be checked. 
8.
How many times will HI be displayed when the following lines are executed?  
Dim c As Integer = 12
Do  
MsgBox("HI")
c =c+ 3  
Loop Until (c >= 30)
a)
(A) 5 
b)
(B) 9 
c)
(C) 6 
d)
(D) 10
9.
What numbers will be displayed in the list box by the following code when the button is clicked?  
Private Sub Display_()  
 Dim num As Integer = 7  
Do  
 num += 1
MsgBox(num)  
Loop Until (num > 6)
MsgBox(num)  
 End Sub
a)
(A) 7 
b)
(B) 7 & 8 
c)
(C) 7 & 7
d)
(D) 8 & 8
10.
 __________ calculate the number of elements in lists. 
a)
(A)  Sentinels 
b)
(B)  Counter variables 
c)
(C)  Accumulators 
d)
(D) Nested loops
11.
 __________ calculate the number of elements in lists. 
a)
(A)  Sentinels 
b)
(B)  Counter variables 
c)
(C)  Accumulators 
d)
(D) Nested loops
13.
_________ calculate the sums of numerical values in lists
a)
(A) sentinels
b)
(B) counter variable
c)
(C) accumulator
d)
(D) nested loops
13.
_________ calculate the sums of numerical values in lists
a)
(A) sentinels
b)
(B) counter variable
c)
(C) accumulator
d)
(D) nested loops
14.
When the number of repetitions needed for a set of instructions is known before they are executed in a program, the best repetition structure to use is a(n) 
a)
(A)  Do While...Loop structure.
b)
(B) Do...Loop Until structure. 
c)
(C) For...Next loop
d)
(D) If blocks
15.
What is one drawback in using non-integer Step sizes? 
a)
(A)  Round-off errors may cause unpredictable results. 
b)
(B)  Decimal Step sizes are invalid in Visual Basic. 
c)
(C) A decimal Step size is never needed. 
d)
(D) Decimal Step sizes usually produce infinite loops. 
16.
When the odd numbers are added successively, any finite sum will be a perfect square (e.g., 1 + 3 + 5 = 9 and 9 = 3^2). What change must be made in the following program to correctly demonstrate this fact for the first few odd numbers?  
 Private Sub Display()    
Dim oddNumber As Integer
Dim sum As Integer = 0  
 For i As Integer = 1 To 9 Step 2 
'Generate first few odd numbers
oddNumber = i
For j As Integer = 1 To oddNumber Step 2  'Add odd numbers  
sum =sum+ j
Next  
 MsgBox(sum & " is a perfect square.")
 Next
End Sub
a)
(A)  Change the Step size to 1 in the first For statement. 
b)
(B) Move oddNumber = i inside the second For loop. 
c)
(C) Reset sum to zero immediately before the second Next statement
d)
(D)  Reset sum to zero immediately before the first Next statement. 
17.
Suppose the days of the year are numbered from 1 to 365 and January 1 falls on a Tuesday as it did in 2013. What is the correct For statement to use if you want only the numbers for the Fridays in 2013? 
a)
(A)  For i As Integer = 3 to 365 Step 7 
b)
(B)  For i As Integer = 1 to 365 Step 3 
c)
(C)  For i As Integer = 365 To 1 Step -7 
d)
(D) For i As Integer = 3 To 365 Step 6 
18.

In the code below, what is the value stored in intCount when the MessageBox is displayed?

a)

0

b)

6

c)

6

d)

7

19.

In what type of loop are the conditions evaluated before the instructions are processed?

a)

Pretest

b)

Finite

c)

Infinite

d)

Posttest

20.

Typically, accumulators and counters are initialized to which value?

a)

1

b)

0

c)

2

d)

Nothing