Chapter 4  General Procedures

Section 4.1  Sub Procedures, Part I

1.         When an End Sub statement is reached in a Sub procedure, execution jumps to

(A)  the statement before the call statement which invoked the Sub procedure.

(B)  the statement after the call statement which invoked the Sub procedure.

(C)  the beginning of the event procedure containing the call statement.

(D)  the end of the event procedure containing the call statement.

 

2.         Consider the following Sub procedure.

Sub TruncateSum(ByVal var1 As Double, ByVal var2 As Double, _

                ByVal var3 As Double)

  txtBox.Text = CStr(Int(var1 + var2 + var3))

End Sub

What will be the output when TruncateSum is used in the following lines of code?

Dim num1, num2, num3 As Double

num1 = 3.5

num2 = 6.75

num3 = 1

TruncateSum(num1, num2, num3)

(A)  10

(B)  12

(C)  0

(D)  11

 

3.         What is wrong with the following call statement and its corresponding Sub statement?

MyProcedure("The Jetsons", 1000, 209.53)

Sub MyProcedure(ByVal var1 As Double, ByVal var2 As Double, _

                ByVal var3 As Double)

(A)  It is not valid to pass something like "The Jetsons."

(B)  Constant values like 1000 cannot be passed, only variables.

(C)  var1 is not of the same data type as "The Jetsons."

(D)  Nothing is wrong with them.

 

4.         Which one of the following is true about arguments and parameters?

(A)  Arguments appear in call statements; parameters appear in Sub statements.

(B)  Parameters appear in call statements; arguments appear in Sub statements.

(C)  They are synonymous terms.

(D)  They are completely unrelated in a program.

 

5.         What will be the output when the button is clicked?

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click

  Dim first, middle, last As String

  first = "Augusta"

  middle = "Ada"

  last = "Byron"

  Initials(first, middle, last)

End Sub

Sub Initials(ByVal c As String, ByVal b As String, _

             ByVal a As String)

  Dim theInitials As String

  theInitials = a.Substring(0, 1) & b.Substring(0, 1) & _

                c.Substring(0, 1)

  txtBox.Text = theInitials

End Sub

(A)  AAB

(B)  BAA

(C)  abc

(D)  ABA

 

6.         What will be displayed when the button is clicked?

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click

  Dim lng, wid As Double

  lng = 5

  wid = 10

  ComputeArea(lng, wid)

End Sub

Sub ComputeArea(ByVal length As Double, ByVal width As Double)

  Dim area As Double

  area = length * width

  txtBox.Text = CStr(area)

End Sub

(A)  0

(B)  50

(C)  15

(D)  25

 

7.         What will be the output of the following program when the button is clicked?

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click

  Dim word As String

  word = "progression"

  Strange(word)

End Sub

Sub Strange(ByVal var As String)

  txtBox.Text = var.Substring(CInt(Int(var.Length / 2)), 1)

End Sub

(A)  progr

(B)  r

(C)  e

(D)  progre

 

8.         What is the output of the following program when the button is clicked?

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click

  Dim a, b As String

  txtBox.Clear()

  a = "A"

  b = "B"

  PrintWords(a, b)

  PrintWords(b, a)

End Sub

Sub PrintWords(ByVal a As String, ByVal b As String)

  txtBox.Text &= a & b

End Sub

(A)  ab ba

(B)  abba

(C)  ABBA

(D)  AB BA

 

9.         Which of the following is NOT a reason for using procedures?

(A)  They break a complex problem down into smaller pieces.

(B)  They make a program run faster.

(C)  They can be reused easily.

(D)  They make it possible for a team of people to work together on a single program.

 

10.     What will be the output of the following program when the button is clicked?

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click

  Dim word1, word2, word3 As String

  word1 = "First"

  word2 = "Second"

  word3 = "Third"

  Myproc(word1, word2, word3)

End Sub

Sub Myproc(ByVal var3 As String, ByVal var2 As String, _

           ByVal var1 As String)

  txtBox.Text = var1 & var2 & var3

End Sub

(A)  FirstSecondThird

(B)  ThirdSecondFirst

(C)  SecondThirdFirst

(D)  No output

 

11.     The ______________ of a Sub procedure are vehicles for passing numbers and strings to the Sub procedure.

(A)  call statements

(B)  arguments

(C)  parameters

(D)  variables declared inside

 

12.     Items appearing in the parentheses of a call statement are known as _______________.

(A)  call variables

(B)  call strings

(C)  parameters

(D)  arguments

 

13.     The process of transmitting values to a Sub procedure is known as ______________.

(A)  passing

(B)  conveying

(C)  evaluating

(D)  referencing

 

14.     Each argument in a call statement must have the same name as the corresponding parameter in the corresponding Sub statement. (T/F)

 

15.     Both constants and expressions can be used as arguments in call statements. (T/F)

 

16.     Both constants and expressions can be used as parameters in Sub statements. (T/F)

 

17.     A Sub procedure can call another Sub procedure. (T/F)

 

18.     Sub procedures can be called only once during the execution of a program. (T/F)

 

19.     Each parameter defined for a Sub procedure corresponds to an argument passed in a call statement for that procedure. (T/F)

 

20.     Arguments and parameters can be used to pass values to Sub procedures from event procedures or other Sub procedures. (T/F)

 

21.     Parameters appearing in a Sub statement are part of the Sub procedure name. (T/F)

 

Section 4.2   Sub Procedures, Part II

1.         Which of the following statements is guaranteed to pass the variable numVar by value to the Sub procedure Tally?

(A)  Tally(numVar)

(B)  Tally(ByVal numVar)

(C)  Tally((numVar))

(D)  Tally(ByVal numVar As Double)

 

2.         What will be displayed when the button is clicked?

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click

  Dim x, y As String

  x = "tin"

  y = "can"

  Swap(x, y)

  txtOutput.Text = x & " " & y

End Sub

Sub Swap(ByRef x As String, ByVal y As String)

  Dim temp As String

  temp = x

  x = y

  y = temp

End Sub

(A)  tin can

(B)  can tin

(C)  tin tin

(D)  can can

 

3.         What will be the output of the following program when the button is clicked?

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click

  Dim word As String

  word = "hairbrush"

  Decapitate ((word))

  txtBox.Text = word

End Sub

Sub Decapitate(ByRef word As String)

  'Chop the first letter off the word.

  word = word.Substring(1)

End Sub

(A)  airbrush

(B)  hairbrush

(C)  hairbrus

(D)  h

 

4.         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)  VB.NET'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 procedures.

 

5.         What will be the output of the following program when the button is clicked?

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click

  Dim var1, var2, var3, num As Integer

  var1 = 2

  var2 = 4

  var3 = 6

  Add(num)

  txtBox.Text = CStr(num)

End Sub

Sub Add(ByRef num As Integer)

  Dim var1, var2, var3 As Integer

  num = var1 + var2 + var3

End Sub

(A)  0

(B)  12

(C)  6

(D)  None of the above

 

6.         What will be the output of the following program when the button is clicked?

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click

  Dim word1, word2, word3, result As String

  word1 = "The"

  word2 = "English"

  word3 = "Channel"

  CatWords(word1, word2, word3, result)

  txtBox.Text = result

End Sub

Sub CatWords(ByVal var1 As String, ByVal var2 As String, _

             ByVal word3 As String, ByRef final As String)

  final = var1 & var2 & word3

End Sub

(A)  TheEnglishChannel

(B)  TheEnglish

(C)  The English Channel

(D)  No output

 

7.         What will be the output of the following program when the button is clicked?

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click

  Dim number As Double = 3

  DoubleAndSquare(number)

  txtBox.Text = CStr(number)

End Sub

Sub DoubleAndSquare(ByRef myVar As Double)

  myVar = myVar + myVar

  myVar = myVar * myVar

End Sub

(A)  3

(B)  36

(C)  6

(D)  0

 

8.         Variables declared inside a procedure are said to have ________________.

(A)  local scope

(B)  procedure-level scope

(C)  class-level scope

(D)  none of the above

 

9.         A variable that is visible to every procedure in a form’s code without being passed is called a __________ variable.

(A)  local

(B)  class-level

(C)  global

(D)  public

 

10.     The declaration statement for a class-level variable should be placed __________.

(A)  inside an event procedure

(B)  inside a general procedure

(C)  anywhere in the program region, except inside a procedure

(D)  above the statement Public Class Form1

 

11.     Suppose a variable is passed by value to a parameter of a Sub procedure, and the parameter has its value changed inside the Sub procedure. What will the value of the variable be after the Sub procedure has executed?

(A)  It will have the newly modified value from inside the Sub procedure.

(B)  Its value can’t be determined without more information..

(C)  It will retain the value it had before the call to the Sub procedure

(D)  None of the above.

 

12.     Suppose a variable is passed by reference to a parameter of a Sub procedure, and the parameter has its value changed inside the Sub procedure. What will the value of the variable be after the Sub procedure has executed?

(A)  It will have the newly modified value from inside the Sub procedure.

(B)  Its value can’t be determined without more information.

(C)  It will retain the value it had before the call to the Sub procedure

(D)  None of the above.

 

13.     What happens to a variable declared locally inside a Sub procedure after the procedure terminates?

(A)  It maintains its value even after the End Sub statement executes.

(B)  It ceases to exist after the End Sub statement executes.

(C)  It loses its value temporarily after the End Sub statement executes, but regains that value upon re-entry to the Sub procedure.

(D)  It is reset to its default value.

 

14.     Each variable must be passed either by value or by reference. (T/F)

 

15.     Sometimes the statements Tally(num) and Tally((num)) have the same effect. (T/F)

 

16.     When the button is clicked, the output of the following program will be 20. (T/F)

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click

  Dim num As Integer = 20

  DoubleIt(num)

  txtBox.Text = CStr(num)

End Sub

Sub DoubleIt(ByRef var As Integer)

  var = var * 2

End Sub

 

17.     The value of an argument in a call statement can be changed by a Sub procedure only if the same name is used in the Sub procedure's parameter list. (T/F)

 

18.     Sub procedures can be individually tested before being placed into a program. (T/F)

 

19.     A value assigned to a variable in one part of a program always effects the value of the like-named variable in the other part of the program. (T/F)

 

Section 4.3   Function Procedures

1.         What is displayed  when the button is clicked?

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click

  Dim a, b as String

  Dim x as Integer

  a = "How now brown cow."

  b = "brown"

  x = FindIt(a, b)

  txtBox.Text = CStr(x)

End Sub

Function FindIt(ByVal z1 as String, ByVal z2 as String) As Integer

  Dim x as Integer

  x = z1.IndexOf(z2)

End Function

(A)  "How now"

(B)  8

(C)  0

(D)  An error

(E)   None of the above

 

2.         Consider the following event procedure that calls a user-defined function named Cube, which returns the cube of a number.

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click

  Dim num, result As Double

  num = CDbl(InputBox("Enter a number to cube:"))

  result = Cube(num)

  txtBox.Text = "The cube of " & num & " is " & result & "."

End Sub

Which of the following is a correct Function definition for Cube?

1. Function Cube(ByVal var As Double) As Double

     Return var ^ 3

   End Function

2. Function Cube(ByVal num As Double) As Double

     Return num ^ 3

   End Function

(A)  1 only

(B)  2 only

(C)  Both 1 and 2

(D)  Neither 1 nor 2

 

3.         Which of the following names would be best for the following Function (called NoName)?

Function NoName(ByVal number As Double) As Double

  Return number ^ 3 + number ^ 3

End Function

(A)  SquareAndCube

(B)  CubeAndDouble

(C)  CubeAndSquare

(D)  DoubleAndCube

 

4.         What will be the output of the following program when the button is clicked?

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click

  Dim word, result As String

  word = "Benjamin"

  result = Rotate(word)

  result = Rotate(result & word)

  result = Rotate(result)

  txtBox.Text = result

End Sub

Function Rotate(ByVal var As String) As String

  Dim varlength As Integer

  varlength = var.Length

Return var.Substring(1) & var.Substring(0, 1)

End Function

(A)  jaminBBenjaminen

(B)  BenjaminBenjamin

(C)  njaminBe

(D)  None of the above

 

5.         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)  Number of arguments

(B)  Names of arguments

(C)  Data type of arguments

(D)  Order of arguments

 

6.         Based on what it returns, what would be a better name for the function "Mystery" in the following program?

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click   

  Dim sentence As String, result As String

  sentence = "Socrates is a man."

  result = Mystery(sentence)

  txtBox.Text = result

End Sub

Function Mystery(ByVal sentence As String) As String

  Dim position As Integer

  position = sentence.IndexOf(" ")

  Return sentence.Substring(0, position)

End Function

(A)  FirstWord

(B)  LastWord

(C)  FirstLetter

(D)  LastLetter

(E)   DoesNothing

 

7.         Variables appearing in the declaration line of a Function procedure are called ____________.

(A)  values of the function

(B)  parameters

(C)  coordinates

(D)  arguments

 

8.         The input to a user-defined function can consist of:

(A)  a single value

(B)  one or more values

(C)  no values

(D)  All of the above

 

9.         To collapse a piece of code and hide it behind a captioned rectangle, you should use what type of statement?

(A)  a Collapse directive

(B)  a Hide directive

(C)  an Include directive

(D)  a Region directive

 

10.     Which Region directive would display “My Function” in the code rectangle?

(A)  #Region Display("My Function")

(B)  #Region "My Function"

(C)  Region Display("My Function")

(D)  Region "My Function"

 

11.     A user-defined function need not have parameters. (T/F)

 

12.     A Function may return up to two values. (T/F)

 

13.     Function procedures can invoke other Function procedures. (T/F)

 

14.     Although a function can return a value, it cannot directly display information in a text box. (T/F)

 

15.     When the button is clicked, the output of the following program will be 5. (T/F)

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click

  Dim x, y, z, result As Double

  x = 3

  y = 4

  z = 1

  result = CInt(Int(Norm(x, y, z)))

  txtBox.Text = CStr(result)

End Sub

Function Norm(ByVal x As Double, ByVal y As Double, _

              ByVal z As Double) As Double

  Return Math.Sqrt(x^2 + y^2 + z^2)

End Function

 

16.     Function procedures are different from Sub procedures because functions return a single value. (T/F)

 

17.     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 procedures for this task. (T/F)

 

18.     Both the input and output of a Function procedure can consist of several values. (T/F)

 

19.     A variable passed to a Function procedure is normally passed by value. (T/F)

 

20.     The input to a user-defined function can consist of one or more values. (T/F)

 

21.     The declaration line of a Function procedure must include parameters. (T/F)

 

22.     A user-defined function cannot be used in an expression. (T/F)

 

23.     Function names should be suggestive of the role performed. The names also must conform to the rules for naming variables. (T/F)

 

Section 4.4   Modular Design

1.         Top-down design refers to

(A)  an obsolete programming practice in which a program is written without procedures in one large module.

(B)  a program design where the key event procedures act like supervisors, delegating tasks to various Sub procedures.

(C)  a method of organizing a team of programmers.

(D)  a method of increasing the speed of a program.

 

2.         Stepwise refinement refers to

(A)  the process whereby a malfunctioning program is fixed by correcting one "bug" at a time.

(B)  any procedure that calls another procedure (and so on) to accomplish a task.

(C)  breaking a large task into smaller tasks.

(D)  the belief that increasing the number of people working on a project decreases the time it will take to complete the project.

 

3.         Breaking up a large problem into smaller subproblems is called _________________.

(A)  transcribed refinement

(B)  refined modules

(C)  stepwise refinement

(D)  transcribed modules

 

4.         Which of the following is NOT considered part of a good top-down design chart?

(A)  A module should accomplish as many tasks as possible.

(B)  Modules should proceed from general to specific as you read down the chart.

(C)  Modules should be as independent of each other as possible.

(D)  The design should be readable.

 

5.         Which of the following logical structures should not be used in structured programming?

(A)  Sequence

(B)  Selection

(C)  Looping (iteration)

(D)  Unconditional branching

 

6.         In modular programming, a driver is

(A)  another name for the team leader of a project.

(B)  a "dummy" program designed solely to call a single procedure and examine its returned values.

(C)  one of the event procedures that a user can invoke.

(D)  never to be used.

 

7.         A program is said to be _________________ if it meets modern standards of program design.

(A)  top-down

(B)  logical

(C)  modular

(D)  structured

 

8.         A(n) __________ is an encapsulation of data and code that operates on the data.

(A)  object

(B)  driver

(C)  decision

(D)  module

 

9.         One advantage to using top-down design in writing a program is that the program can later be modified with less of a chance of introducing new errors. (T/F)

 

10.     A GoTo statement can always be replaced by a combination of the three logical structures of structured programming. (T/F)

 

11.     "Stub programming" is one method of testing and debugging the key event procedures in a program. (T/F)

 

12.     Studies have shown that despite the many benefits of structured programming, it usually takes a longer amount of time when used on large programming projects. (T/F)

 

13.     Debugging an unstructured program often requires the programmer to look at the entire program in order to make even a simple modification. (T/F)

 

14.     With top-down design, higher-level modules control the flow of the program while lower level modules do the actual work. (T/F)

 

15.     Modular design decreases the programmer’s productivity. (T/F)

 

16.     Most modern programmers use a blend of traditional structured programming along with object-oriented design. (T/F)

 

17.     VB.NET is an object-oriented language. (T/F)