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)

 

  1. With loops, sometimes a variable is used as a(n) ________; it keeps track of the sum of some values that are being processed inside the loop..

 

  1. The most common form or parameter passing is _________; when this is used, any changes in the procedure to the parameter do not have an effect back in the calling code.

 

 

True/False (3 points each)

 

  1. The contents of a post-test loop will always be executed at least once

 

  1. A given procedure may only be called from one other place in a program.

 

  1. If you write to a file with WriteAllText with the append parameter equal to false, any previous contents of the file will be lost.

 

 

Multiple Choice (4 points each)

 

  1. With regard to parameters for independent sub procedures:

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

 

  1. In VB, for loops are appropriate when:

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.

 

  1. Which of the following is NOT a function/procedure available for strings that is helpful in working with files?

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!

 

  1.  

Dim x as integer

Dim y as integer = 17

Do while x < y

                x = x + 1

                y = y -1

Loop until y > x

 

  1.  

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

 

  1.  

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)

  1. Given the following doSomething definition when the code fragment at bottom executes, exactly what is displayed in message boxes?

 

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)

 

(12 points)

15.    Write a VB Function “CalcScore” for a game. The function should be passed the number of aces,  the number of kings, number of queens, and the number of jacks.  It should return the score based on the following: 4 points per ace, 3 points per king, 2 points per queen, and 1 point per jack. The function DOES NOT need to check if the passed values are valid.

 

(20 points)

  1. Suppose a function (named playSlots) is available (don’t try to write it! You don’t have enough info available to even try!) that given parameter amount bet (double) will return the net winnings (negative the amount bet if they lose, positive amount won if they won (based on odds and payoffs – you don’t have to know how it works; just use it!)).