Computer Science 230 – Spring 2007
04/04/07 Mid Term Test # 2 Test Form A
Name:
Instructions:
Answer ALL questions on the answer sheet provided.
Hand in TEST, Answer Sheet, and Help Sheet, each with your name on it.
Remember to put the letter of your test form on your answer sheet.
Please ask any clarifying questions you have (come up).
There is no access to computers allowed during this exam!
Completion (4 points each)
True/False (3 points each)
Multiple Choice (4 points each)
A) when defining them in the procedure, you must specify their data type
B) when passing them to a procedure, you must specify their type in the call
C) all parameters for a given procedure must be the same type
D) all of the above
E) none of the above
A) only when the number of times the loop will go around is known when the program is being written
B) it is appropriate for all uses of loops
C) when the number of times the loop will go around will be known at least by the time the program reaches the beginning of the loop.
D) None of the above.
A) PadLeft
B) IndexOf
C) Substring
D) NextLine
E) Space
Valid/Invalid (4 points each)
For each, tell whether valid or invalid in VB. If invalid, tell why!
Generally they are taken out of context, we assume that valid context surrounds them (e.g. inside a program, variables declared, given values if necessary, etc). Option Explicit and Option Strict are ON!
Dim x as integer
Dim y as integer = 17
Do while x < y
x = x + 1
y = y -1
Loop until y > x
Private function getSomething ( byVal firstparam as double)
Dim stuff as double
‘ valid code in here – assume rest of body is fine, including giving value to stuff
Return stuff
End function
Assuming that the following procedure has been declared:
Private sub doSomething ( byVal firstparam as double, byVal secondparam as String)
‘ valid code in here – assume body of procedure is fine
End sub
And assume variables have been declared and properly initialized:
Dim second as double = 12.5
Dim first as String = “Wild World”
Report if the following is call is valid or invalid – if invalid, explain why!
doSomething(first,second)
Doing and Understanding: (points as shown)
(9 points)
Private function doSomething (byRef first as integer, byVal second as integer) as integer
Dim res as integer
First = first * 2
Second = second + 1
Res = first * second
Return res
End function
Dim that as integer = 4
Dim other as integer = 10
Dim answ as integer
answ = doSomething(that, other)
msgbox(“that: “ & that.toString() & “ other: “ & other.toString() & “ answ: “ & answ.toString())
(10 points)
13. What is displayed on the screen when the following code fragment runs?
Dim wan as double = 20
Dim tu as double = 2
Dim cnt as integer = 0
Do while wan > 1
Msgbox(“wan: “ & wan.toString() & “ tu: “ & tu.toString() )
wan = wan / tu
tu = tu + 1
cnt = cnt + 1
loop
Msgbox(“cnt: ” & cnt.toString() & " wan: “ & wan.toString() & “ tu: “ & tu.toString() )
(8 points)
14. What is displayed on the screen when the following code fragment runs?
Dim length as integer
Dim width as integer
For length = 3 to 5
For width = 2 to 3
Msgbox(“=> “ & length.toString() & “ X “ & width.toString() )
Next width
Next length
Writing Code (points as shown)