CSCA02 Final Exam
Solution for VB Questions in Course Package

Here are the solutions for the Visual Basic questions that are in the Sample Final Exam in the Course Package.

The Visual Basic questions on the final exam in the Course Package are different than what you can expect on the exam this term. This is because we no longer teach Functions in this course.

Below I have rewritten the questions from the Course Package, so that they do not require you to use functions. I have also provided the solutions. Please note that the 3rd question is very difficult, and I'm not planning to give you one this hard on the final exam this term.


Original 4. (3 marks) The "Additive Inverse" of a number can be described as another number such that if you were to add the number with its additive inverse, your result will be zero (e.g. The numbers +4.72 and -4.72 are each other's additive inverse). The following function will take a number (positive or negative) as parameter and will return a number which is the additive inverse of the original number. Complete its body. (Caution: Marks will be deducted for na•ve implementation of this function).

Rewritten 4. (3 marks) The "Additive Inverse" of a number can be described as another number such that if you were to add the number with its additive inverse, your result will be zero (e.g. The numbers +4.72 and -4.72 are each other's additive inverse). Write a program that takes one number as input. This number is entered into a TextBox called txtIn. When the user of the program clicks on the CommandButton called cmdAddInv, this program will display the Additive Inverse of the number entered on a Label called lblOut.

Private Sub cmdAddInv_Click()

   Dim fNumber As Single
   fNumber = txtIn.Text

   If fNumber = 0 Then
      lblOut.Caption = fNumber
   Else 
      lblOut.Caption = -1 * fNumber
   End If

End Sub

Notes: 1. The above solution did not really need to use an If statement to check to see if the number was equal to zero.
2. It is possible to solve this program without using a variable at all. This program could have been solved in one line, as follows: lblOut.Caption = -1 * Val (txtIn.Text)


Original 5. (5 marks) The following function will take a string variable containing a sequence of bits as parameter and will return a string containing an opposite bit pattern. For example, if the input string is "0011" the output string will be "1100" (all zeros are replaced by ones and all ones are replaced by zeros). Complete its body.

Rewritten 5. (5 marks) Write a program that takes a sequence of bits (a bit pattern) as input, using a TextBox called txtIn for input. This program should display the opposite bit pattern on a label called lblDisplay. For example, if the input string is "0011" the output string will be "1100" (all zeros are replaced by ones and all ones are replaced by zeros). The opposite bit pattern is displayed when the user clicks on a CommandButton called cmdFlipBits.

Private Sub cmdFlipBits_Click()

   Dim sInput As String
   sInput = txtIn.Text

   Dim sOutput As String
   sOutput = ""
             
   Dim iCount As Integer
   For iCount = 1 To Len(sInput)
      sOutput = sOutput & ((Val (Mid (sInput, iCount, 1))+1) Mod 2)
   Next iCount
             
   lblDisplay.Caption = sOutput

End Sub

Note: Here is an alternate solution to this problem that should be easier to understand.
It uses an If statement rather than Mod.

Private Sub cmdFlipBits_Click()

   Dim sInput As String
   sInput = txtIn.Text

   Dim sOutput As String
   sOutput = ""
             
   Dim iCount As Integer
   For iCount = 1 To Len(sInput)

      If Mid (sInput, iCount, 1) = "0" Then
         sOutput = sOutput & "1"
      Else
         sOutput = sOutput & "0"
      End If

   Next iCount
             
   lblDisplay.Caption = sOutput

End Sub

Original 6. (5 marks) You are asked to complete the body of the following function, which will take a string of characters as parameter and will return True if the string contains duplicate characters and False otherwise. Case differences are not a factor in searching for duplicate characters. For instance: If we call this function with the actual parameter "Paper", it will return True since there are two "P" characters (even though one is in uppercase and the other is in lowercase format).

Rewritten 6. (5 marks) Write a program that takes a string of characters as parameter, using a TextBox called txtInput. This program will use a Label called lblResult to print out "Has Duplicates" if the input contains duplicate characters and it will print out "No Duplicates" otherwise. Case differences are not a factor in searching for duplicate characters. For instance: If we run this program and enter "Paper" into the TextBox, it will print "Has Duplicates" since there are two "P" characters (even though one is uppercase and the other is lowercase).

Private Sub cmdDuplicates_Click()
   Dim sWord As String           ' Input read from text box
   sWord = txtInput.Text

   Dim sPreviousChars As String  ' All characters before the current character
   Dim sNextChars As String      ' All characters after the current character
   sPreviousChars = ""
   sNextChars = Mid(sWord, 2)

   Dim bDuplicates As Boolean    ' True/False to indicate if duplicates found
   bDuplicates = False           ' Initially we assume no duplicates found

   ' This loop continues for each character in the input string.  Search for
   ' the current character in the characters before it and the characters after
   ' it in the string.  If a match is found, set boolean variable to True, and
   ' reset the loop counter to force the loop to exit immediately.
   Dim iCount As Integer
   For iCount = 1 To Len(sWord) - 1
      Dim sCurrentChar As String
      sCurrentChar = Mid(sWord, iCount, 1)
      If ((InStr(1, sPreviousChars, sCurrentChar, 1) > 0) Or _
              (InStr(1, sNextChars, sCurrentChar, 1) > 0)) Then
         bDuplicates = True
         iCount = Len(sWord)   ' Forces loop to stop after this iteration
      End If
      
      ' Get ready for the next iteration of the loop.  The previous
      ' characters will now contain the current character, and the
      ' remaining (next) characters will no longer contain the current
      ' character (the first character).
      sPreviousChars = sPreviousChars & sCurrentChar
      sNextChars = Mid(sNextChars, 2)
   Next iCount

   ' Check boolean variable and display result on a label.
   If bDuplicates = True Then
      lblResult.Caption = "Has Duplicates"
   Else
      lblResult.Caption = "No Duplicates"
   End If

End Sub

Note: You are not required to use comments when you write programs on a final exam, but I have shown some comments above to help you to understand the solution to these problems.