ANSWERS FOR CSC 336 MIDTERM EXAM -- WINTER 2004 Part A. 1. (d) Both (b) and (c) are true. 2. (e) All of the above. Obvious. 3. (c) A user interface should be consistant in font, color, and control arrangement. It should also be intuitive and user friendly. Complexity is NOT a good attribute of a user interface. 4. (d) Option Strict On means that no automatic type conversions are allowed. The CType function must be used to explicitly change one datatype to another. 6. (a) (b - a = 2 And a * c < 20) & " " & (False Or b <> 5) (5 - 3 = 2 And 3 * 7 < 20) & " " & (False Or 5 <> 5) (True And False) & " " & (False Or False) False & " " & False 7. (c) New is used to invoke a constructor. We are only talking about invocation, not definition. 8. (d) A line in a csv file can contain 1 or 2 fields as well as 3 or more. 9. (a) Dim a(9) As Integer defines an array with indices 0, ... , 9, which means 10 elements. (c) and (d) each define an array with one element. 10. (d) A Pen object is not needed to draw text in a list box. Part B. 1. Public Sub picInput_MouseUp(sender As Object, e As Args) _ Handles picInput.MouseUp Dim p As String p = e.X & " " & e.Y lstPoints.Items.Add(p) txtPoints.Text = p MessageBox.Show(p) Dim r As New StreamReader("c:\Points.txt") r.WriteLine(p) r.Close() End Sub 2. Public r As StreamReader Public Sub Form1_Load(sender As Object, e As Args) _ Handles MyBase.Load r = New StreamReader("c:\Students.txt") End Sub Public Sub btnSubmit_Click(sender As Object, e As Args) _ Handles btnSubmit.Click Dim sum = 0, count As Integer = 0 Dim fields() As String Dim ave As Double line = r.ReadLine() While Not IsNothing(line) fields = Split(line, ",") If fields(1) = "F" And radFemale.Checked Or _ fields(1) = "M" And radMale.Checked Then sum += fields(2) count += 1 End If line = r.ReadLine() End While ave = sum / count txtAve.Text = ave.ToString("#0.00") End Sub 3. (a) Public Function SumOfFactors(n As Integer) As Integer Dim m As Integer Dim sum As Integer = 0 For m = 0 To n / 2 If n Mod m = 0 Then sum += m End If Next Return sum End Function (b) Public Module Main() WriteLine(SumOfFactors(12)) WriteLine(SumOfFactors(9)) WriteLine(SumOfFactors(7)) End Module (c) Public Sub btnSubmit_Click(sender As Object, e As Args) _ Handles btnSubmit.Click txtSum.Text = SumOfFactors(txtNumber.Text) End Sub