exam2answers

advertisement
COSC 2409
Spring 2002
Exam 2 Answers
For each of the following questions choose (a) for true and (b) for false, and code your choice in the
answer sheet.
1.
Each argument in a Call statement must have the same name as the corresponding parameter in the
corresponding Sub statement. False
2.
A Sub procedure can call another Sub procedure. True
3.
Both Sub and Function procedures are accessed through the Call statement. False
4.
When a general sub procedure is used, the number of arguments must equal the number of parameters.
True
5.
Suppose you want to write a procedure that takes three numbers, num1, num2, and num3; and returns
their sum, product, and average. It is best to use a Function procedure for this task. False
6.
One disadvantage of using arrays is they cannot easily be passed to procedures. False
7.
Parallel arrays are two or more arrays that have corresponding elements. True
8.
The statement Dim state(50)has the same effect as the statement Dim state(1 To 50).
False
9.
Consider the following Dim and assignment statements for myArray(). The assignment statement will
cause a "Subscript out of range" error. False
Dim myArray(1 To 50) As Single
myArray(34) = 51
10. Consider the following sub procedure declaration. False
Private Sub MyProcedure(var1 As Single, var2 As Single, var3 As Single)
Arguments passed to this sub procedure are passed by value rather than by reference.
For each of the following questions choose the best possible answer from choices (a) through (d), and
code your choice in the answer sheet.
11.
What is wrong with the following Call statement and its corresponding Sub statement?
Call MyProcedure("The Jetsons", 1000, 209.53)
Private Sub MyProcedure(var1 As Single, var2 As Single, var3 As
Single)
(A)
(B)
*(C)
(D)
It is not valid to pass something like "The Jetsons."
Constant values like 1000 cannot be passed, only variables.
Var1 is not of the same data type as "The Jetsons."
Nothing is wrong with them.
12.
What will be displayed when the command button is clicked?
Private Sub cmdButton_Click()
Dim x As String, y As String
x = "tin"
y = "can"
Call Swap(x, y)
picOutput.Print x; " "; y
End Sub
Sub Swap(x As String, ByVal y As String)
Dim temp As String
temp = x
x = y
y = temp
End Sub
(A)
(B)
(C)
*(D)
tin can
can tin
tin tin
can can
13.
Suppose the variable myName is declared in a Dim statement in two different Sub procedures.
Which statement is true?
(A) The program will malfunction when it is executed.
(B) When the value of myName is changed in one Sub procedure, it will also be changed in the
other Sub procedure.
(C) Visual Basic's smart editor will alert you that this is an error before the program is executed.
*(D) The two variables will be local to their respective Sub procedure.
14.
What will be the output of the following program when the command button is clicked?
Private Sub cmdButton_Click()
Dim var1 As Integer, var2 As Integer, var3 As Integer, num As
Integer
var1 = 2
var2 = 4
var3 = 6
Call Add(num)
picBox.Cls
picBox.Print num
End Sub
Private Sub Add(num As Integer)
Dim var1 As Integer, var2 As Integer, var3 As Integer
num = var1 + var2 + var3
End Sub
*(A)
(B)
(C)
(D)
0
12
6
None of the above
15.
What will be the output of the following program when the command button is clicked?
Private Sub cmdButton_Click()
Dim number As Single
picBox.Cls
number = 3
Call SquareAndDouble(number)
picBox.Print number
End Sub
Private Sub SquareAndDouble(myVar As Single)
myVar = myVar * myVar
myVar = myVar + myVar
End Sub
(A)
*(B)
(C)
(D)
3
18
36
0
16.
Consider the following event procedure that calls a user-defined function named Cube, which
returns the cube of a number.
Private Sub cmdButton_Click()
Dim num As Single, result As Single
num = Val(InputBox("Enter a number to cube:"))
result = Cube(num)
picBox.Print "The cube of"; num; "is"; result
End Sub
Which of the following is a correct Function definition for Cube?
1. Private Function Cube(var As Single) As Single
Cube = var ^ 3
End Function
2. Private Function Cube(num As Single) As Single
Cube = num ^ 3
End Function
(A)
(B)
*(C)
(D)
1 only
2 only
Both 1 and 2
Neither 1 nor 2
17.
What will be the output of the following program when the command button is clicked?
Private Sub cmdButton_Click()
Dim word As String, result As String
picBox.Cls
word = "Benjamin"
result = Rotate(word)
result = Rotate(result & word)
result = Rotate(result)
picBox.Print result
End Sub
Private Function Rotate(var As String) As String
Dim varlength As Integer
varlength = Len(var)
Rotate = Mid(var, 2, varlength - 1) & Left(var, 1)
End Function
*(A)
(B)
(C)
(D)
jaminBBenjaminen
BenjaminBenjamin
njaminBe
No output.
18.
The arguments appearing in a call statement must match the parameters in the appropriate Sub or
Function statement in all but one of the following ways. Which one?
(A)
*(B)
(C)
(D)
Number of arguments
Names of arguments
Data type of arguments
Order of arguments
19.
After the following Dim statement, how many subscripted variables called myvar(i) will be
available?
Dim myvar(3 To 8) As Single
(A)
*(B)
(C)
(D)
20.
5
6
7
8
Which of the following is NOT a valid Dim statement?
(A)
(B)
*(C)
(D)
Dim
Dim
Dim
Dim
myArray(0 To 1) As String
bigarray(100) As Single
numbers(4 To 1) As Single
values(-3 To 3) As Integer
21.
What is the output of the following program when the command button is clicked?
Private Sub cmdButton_Click()
Dim i As Integer, nom(1 To 5) As String
Open "DATA.TXT" For Input As #1
For i = 1 To 5
Input #1, nom(i)
Next i
Close #1
picBox.Cls
For i = 5 To 1 Step -2
picBox.Print nom(i); " ";
Next i
End Sub
Contents of DATA.TXT: "Bach", "Borodin", "Brahms", "Beethoven", "Britain"
(A)
(B)
(C)
*(D)
22.
Bach Brahms Britain
Britain Beethoven Brahms Borodin Bach
Bach Borodin Brahms Beethoven Britain
Britain Brahms Bach
What is the output of the following program segment?
Dim numbers(1 To 4) As Single, h As Single, i As Integer, k As
Integer
h = 0
Open "DATA.TXT" For Input As #1
For i = 1 To 4
Input #1, numbers(i)
Next i
Close #1
For k = 1 to 4
h = h + numbers(k)
Next k
picBox.Cls
picBox.Print h
Contents of DATA.TXT: 2, 4, 2, 3
*(A)
(B)
(C)
(D)
11
2
7
4
23.
What is the output of the following program segment?
Dim numbers(0 To 4) As Single, i As Integer, k As Integer
Open "DATA.TXT" For Input As #1
For i = 0 To 4
Input #1, numbers(i)
Next i
Close #1
For k = 1 To 3
numbers(k + 1) = numbers(k + 1) + numbers(k)
Next k
picBox.Cls
picBox.Print numbers(4)
Contents of DATA.TXT: 3, 6, 4, 8, 3
(A)
*(B)
(C)
(D)
10
21
18
11
24.
Given the Dim statement below, which set of statements will initialize all elements of myArray()
to 100?
Dim myArray(0 To 100) As Single
(A) myArray = 100
(B) For i = 0 To 100
(i) = 100
Next i
*(C) For j = 0 to 100
myArray(j) = 100
Next j
(D) myArray() is already initialized to 100 by the ReDim statement.
25.
Form_Load is which of the following?
(A)
(B)
(C)
*(D)
A command to load a new copy of the form.
An event procedure that executes when the program runs the first time.
A method of loading pictures or text onto the form.
An event procedure that is automatically executed at the beginning of the program.
Answers to Part 2 of the exam are on the following page.
Option Explicit
Private Sub cmdExam_Click()
Dim arrNumbers(1 To 10) As Integer
Dim intLIndex As Integer
Dim intLValue As Integer
Call FillArray(arrNumbers())
Call FindElement(arrNumbers(), intLValue, intLIndex)
Call DisplayData(arrNumbers(), intLValue, intLIndex)
End Sub
Private Sub FillArray(ByRef arrN() As Integer)
Dim i As Integer
Open App.Path & "/numbers.txt" For Input As #1
For i = 1 To 10
Input #1, arrN(i)
Next i
Close #1
End Sub
Private Sub FindElement(ByRef arrNu() As Integer, _
ByRef intLV As Integer, ByRef intLI As Integer)
Dim i, intValue, intIndex As Integer
intValue = 0
For i = 1 To 10
If intValue < arrNu(i) Then
intValue = arrNu(i)
intIndex = i
End If
Next i
intLV = intValue
intLI = intIndex
End Sub
Private Sub DisplayData(ByRef arrNum() As Integer, _
ByVal intLVa As Integer, ByVal intLIn As Integer)
Dim i As Integer
picExam.Cls
picExam.Print "The ten element data array:"
For i = 1 To 10
picExam.Print arrNum(i);
Next i
picExam.Print
picExam.Print
picExam.Print "The value of the largest array element is "; intLVa
picExam.Print "The index of this largest array element is "; intLIn
End Sub
Download