Computer Science 230 – Fall 2004

11/04/04                      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).

 

Completion (3 points each)

 

  1. A loop can be inside another loop. We refer to the inner loop as _______ inside the outer loop.

 

  1. Files that are accessed in order from beginning to end are known as ________ files. When reading or writing, the first line is processed first and so on.

 

  1. In VB, ___________ capabilities are available in order to allow the program to deal with run-time errors without crashing. Sometimes (frequently) this is unnecessary if careful use of if’s can avoid the errors in the first place.

 

True/False (2 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. All types of loops in VB exit when a test comes out false.

 

 

Multiple Choice (3 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 working with files

A)    a fileWriter variable is used for writing to output files

B)     a stream reader variable is used for reading from input files

C)    when checking for the end of a file, a fileEnd variable is consulted

D)    all of the above

E)     none of the above

 

  1. If you open a file with CreateText

A)    if the file already exists, the open will cause an exception

B)     if the file already exists, the open will cause previous contents to be lost

C)    if the file already exists, new content will be added after the existing contents at the end of the file

D)    all of the above

E)     none of the above

 

 

Valid/Invalid (4 points each)

 

  1. Indicate if the following is valid in VB. If it is invalid, explain why.

 

Dim x as integer = 137

Do

     X = X / 10

Until x < 1

 

  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 silly as double = 12.5

            Dim wild as String = “Wild World”

Report if the following is call is valid or invalid – if invalid, explain why!

                        doSomething(wild,silly)

 

 

  1. Indicate if the following is valid in VB. If it is invalid, explain why.

dim x as integer = 3

dim y as integer = 50

do while (x < 10) AndAlso (y <100)

            x = x + 1

            y = y + 3

loop

 

 

  1. Indicate if the following is valid in VB. If it is invalid, explain why.

Dim I as integer

For I = 5 to 3 step -1

            Msgbox(i.toString() )

Next i

 

 

 

Short Answer (points as shown)

 

            (6 points)

  1. This fragment is syntactically correct (it will compile successfully), what all could go wrong at run-time? (lstStuff is a list box on the current form)

 

            Dim inpf as IO.StreamReader

            Dim cnt as integer = 0

            Dim current as string

            Inpf = IO.File.OpenText(“myinputs.txt”)

            For cnt = 0 to 10

                        Current = inpf.ReadLine()

                        lstStuff.items.add(current)

            next cnt

inpf.close()

 

Doing and Understanding: (points as shown)

 

(5 points)

  1. Given the following code fragment, exactly what is displayed in message boxes as the following executes?

 

Dim num as integer = 20

Dim max as integer = 12

do

            Msgbox(“==> “ & num.toString( ) )

            Num = num + 1

Loop while num < max

 

(6 points)

  1. Given the withdraw definition, and the code fragment above that, exactly what is displayed in message boxes as the following executes?

           

Dim x as double = 100

Do while Withdraw(x, 30)

            msgbox(“X:  “ & x.toString( ))

loop

 

Private function Withdraw( byRef balance as double,  byVal amount as double) as Boolean

                        If balance >= amount then

                                    Balance = balance – amount

                                    Return true

                        Else

                                    Return false

                        End if

            End function

 

            (9 points)

  1. Given the following code fragment, exactly what is displayed in message boxes as the following executes?

 

Dim X as integer = 1

Dim Y as integer = 2

For cnt = 1 to 2

            X = x + cnt

            Y = y + x

            Do until y > 15

                        Y = y * x

                        Msgbox( “in - X: “ & x.toString() & “ Y: “ & y.toString())

            Loop

            Msgbox( “out - X: “ & x.toString() & “ Y: “ & y.toString())

Next cnt

Msgbox(“outa here”)

 

            (8 points)

  1. Given the following code fragment, and the contents of text.txt shown below, exactly what is displayed in message boxes as the following executes? (Assume variables have been properly declared)

 

Dim inpf as io.streamreader

Dim strLine as string

Dim loc as integer

Dim stuff as string

Inpf = io.file.opentext(“text.txt”)

 

Do until inpf.peek = -1

            strLine = inpf.ReadLine()

            loc = strLine.IndexOf(“,”c)

            stuff = strLine.substring(loc + 1)

            msgbox(“stuff: “ & stuff)

loop

           

text.txt:

Hello, The, Numb

I, End, By

Must, Of, Linkin

Be, The, Park

 

 

 

 


Writing Code (points as shown)

 

            (6 points)

  1. Write code to declare needed variable and open the file “junk.txt” so that information written will start at the beginning of the file.

 

(8 points)

  1. Write a VB function that is passed the number of miles traveled and the number of minutes elapsed, returns the average speed in miles per hour on the trip.

 

(12 points)

  1. Suppose a function (named getMPG) is available that given parameters - the type of car (a string) and speed (integer) will return the expected mpg. Write code that considers all possible integer speeds from 20-75 for your “Camry” and finds the highest mpg and what speed it is achieved at.