Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

CSC123F TEST1 memo 29 July 2016

MARKS: 30 Time: 1hour

__________________________________________________________________________________

1. A number is called perfect if the sum of all the factors is equal to twice the number.
For example, 6 is perfect because 1 + 2 + 3 + 6 = 12 = 2*6.
A. Write down a function called Perfect that will accept a number as its argument
and return a string telling if the number is perfect or not. [7]
Ans
Public Function Perfect (ByVal num As Single) As String
Dim k, sum As Single
For k = 1 To num1
If num Mod k = 0 Then
sum += k
End If
Next
If sum = 2 * num Then
Return " is perfect"
Else
Return " is not Perfect"
End If
End Function

B. Write down a calling procedure for the function above that reads n from a text
box and prints out whether n is perfect or not in the message box. [3]
Ans:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs)
Handles Button1.Click
Dim n
n = TextBox1.Text
MsgBox(n & Perfect(n))

End Sub

2. Write down a single line code that is used to :


A. Navigate a website www.ufh.ac.za from the web browser called
WebBrowser1. [2]
Ans: WebBrowser1.Navigate (www.ufh.ac.za)

B. Make label1 invisible. [2]


Ans:
Label1.Visible = False

1|Page
C. Terminate a program while running [2]
Ans
Exit Sub

3. What are the differences between between:


A. Combo box and List Box [2]

Ans: Listbox shows all information at once while combo box provide you with a
dropdown menu to view items

B. Radio button and Check Box [2]

Ans: Radio buttons are mutually exclusive while in check boxes you can select
multiple items

4. Write a program that will compute for you the area of a rectangle whose length and
breadth are read from text box1 and text box2 respectively, and print the area in text
box3. Your program should handle errors that might occur at run time using On Error
GoTo method as follows:
It must print a message Verify the values in you text boxes and show the word
Error in text box3.
Note the above messages should not be shown if everything goes well. [10]

Ans:

Public Class Form1


Private Sub CmdCalculate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles CmdCalculate.Click
Label1.Visible = False
Dim l, b,a As Single
On Error GoTo error_handler
l = TextBox1.Text
b= TextBox2.Text
a = l*b
TextBox3.Text = a
Exit Sub
error_handler:
TexBox3.Text = "Error"
Label1.Visible = True
Label1.Text = " Verify the values in you text boxes "
End Sub
End Class

Grand total = 30 Marks

2|Page

You might also like