Intro to Programming - Quiz 3Practice

Question 1
Given the following statement:

If IsValid(txtAmount.Text) = True Then …

Which of the function definitions below match the IsValid function call?

  1. Function IsValid(ByVal myArg as Integer) as String
  2. Function IsValid(ByVal myArg as String) as Boolean
  3. Function IsValid(ByVal myArg as String) as Integer
  4. Function IsValid(ByVal myArg as Integer) as Boolean

Answer:The statement calling the IsValid functionis providing a value in a text box as an argument. Since a text box value is always a string, the function must expect a string as an argument. This eliminates the 1st and 4th responses which expect an integer value as an argument.

The statement calling the IsValid functioniscomparing IsValid(txtAmount.Text) to the boolean value True, so the IsValid function must return a boolean value. This eliminates the 3rd response since it returns an integer value.

Response (b) is correct since it is the only onethat defines a function which accepts a string value and returns a Boolean value.

Question 2
Write a function called TenLong that will accept twostring values as arguments and return True ifone or both of the strings containat least 10 characters.

Answer:Function TenLong(ByVal str1 as String, ByVal str2 as String) as Boolean
If str1.Length >= 10 or str2.Length >= 10 Then
Return True
Else
Return False
End Function

Question 3
Explain the nature of the syntax error in the following function:

Function Sum(ByVal nbr1 as Single, ByVal nbr2 as Single) as Integer
Return nbr1 + nbr2
End Function

Answer: Both nbr1 and nbr2 are declared as variables of type Single. Thus when you add nbr1 and nbr2, the result must also be a Single. However, the function expects to return a value of type Integer, not Single. The function should be modified to either return a value of type Single or expect the arguments nbr1 and nbr2 to be of type Integer.

Question 4
What are the values of the variables nbr1 and nbr2 after completing the SumSquare(x, y) instruction?

Dim nbr1 as Integer = 4
Dim nbr2 as Integer = 6
SumSquare(nbr1, nbr2)
Sub SumSquare(ByVal arg1 as Integer, ByVal arg2 as Integer)
arg1 = arg1 + arg2
arg2 = arg1 * arg1
End Sub

Answer:The values nbr1 and nbr2 are used in the main procedure and passed to the sub procedure. Since both these values are passed to the sub procedure ByVal, the sub procedure can’t possibly have an effect on the values nbr1 and nbr2 in the main procedure.

The values nbr1 and nbr2 remain unchanged at 4 and 6 after completing the SumSquare(x, y) instruction.

Question 5
Your project already contains a form design for a splash screen called frmSplash. Write a code segment to create a new instance of this form and display it as a modeless form.

Answer:Dim SplashScreen as New frmSplash()
SplashScreen.Show()

Question6
Write a code segment thatsearches a two dimensional arraysales to find which column of each row contains thehighest value. Save the column number for the highestvalue found ineach row in a one dimensional arrayhighCol. Save the highest value of each row in aone dimensional parallel array highVal.Assume the two dimensional array saleshas been declared previously as Integer with 6 rows and 4 columns. All other variables or arrays should be declared in your code.

Answer:Dim intRow as Integer
Dim intCol as Integer
Dim highCol(5) as Integer
Dim highVal(5) as Integer

For intRow = 0 to 5
highCol(intRow) =0
highVal(intRow) = sales(intRow, 0)
For intCol =1 to3
If sales(intRow, intCol) > highVal(intRow) Then
highVal(intRow) = sales(intRow, intCol)
highCol(intRow) = intCol
End If
Next intCol
Next intRow