Thursday, May 1, 2008

Some Exam Problems

Problems from the first sample test are as follows (Hopefully without typos):

1) D
2) A
3) C
4) C
5) C
6) C
7) C
8) D
9) B
10) B

Discussion:
Code from #10:
Dim place, i As Integer
Dim character, sentence, phrase(9) As String
sentence = "Every path hath a puddle."
place = 0
i = 0
character = sentence.Substring(place, 1)
Do
If character = " " Then
phrase(i) = sentence.Substring(0, place)
i += 1
End If
place += 1
character = sentence.Substring(place, 1)
Loop Until character = "."


The answer is ``Every path".

We have the following variables:
i 'The current index of the array
place 'The current location in the sentence string
character 'The current character at which we are looking
sentence 'String: "Every path hath a puddle."
phrase() 'Array of size 10


What the code does, loosely, is going through the sentence string one character at a time and every time it hits a space, it saves the all of the i+1 words in the current array index i into the array phrase().

If we look at the part of the code that actually changes the array (highlighted above) then we can see that every time the array is updated it starts at the very beginning and goes up to place characters. Therefore the final array should look like:

0 1 2 ...
+-------+------------+-----------------+----
| Every | Every path | Every path hath | ...
+-------+------------+-----------------+----

Where every cell has one extra word in it from the beginning. The program stops when character = "." which is at the end of the sentence string.

Programming Problem:
a)

Dim arr(1000) As Integer
For i As Integer = 1 To 1000
arr(i) = CInt(InputBox("Enter number: " ,""))
Next

For j As Integer = 1000 To 1, Step -1
lstBoxOut.Items.Add( arr(j) )
Next

b)
Dim arr(1000) As Integer
For i As Integer = 1 To 1000
arr(i) = CInt(InputBox("Enter number: " ,""))
Next

For j As Integer = 1000 To 1, Step -1
If arr(j) Mod 2 = 0 Then
lstBoxOut.Items.Add( arr(j) )
End If
Next

Second Practice Exam

You can find a past exam 2 here. I made a mistake on the first question when I gave you the answers. Here are the correct answers:

1) D
2) C
3) B
4) B
5) D
6) D
7) C
8) B
9) C
10) A

Part II
a) 70^2 = 4900
b) 24^2 = 576

Part III
Answer: 3

Discussion:
The first two loops simply save all of the numbers from the files into arrays a() and b()

sr = IO.File.OpenText("DATA1.TXT")
For k As Integer= 0 To 19
a(k) = CDbl(sr.ReadLine)
Next
sr.Close()
sr = IO.File.OpenText("DATA2.TXT")
For k As Integer = 0 To 19
b(k) = CDbl(sr.ReadLine)
Next


After this, a() contains:
3, 2, 5, 1, 7, 8, 3, 5, 6, 2, 3, 6, 1, 6, 5, 5, 7, 2, 5, 3

and b() contains:
5, 3, 3, 4, 8, 2, 3, 5, 9, 5, 3, 7, 3, 7, 6, 3, 2, 1, 3, 4

This is in order so a(0) equals 3 and a(1) equals 2; b(0) equals 5 and b(1) equals 3 etc...

The last loop is what changes variable c before it is output:

For k As Integer = 0 To 19
If a(k) = b(k) Then
c += 1
End If

Next
lstBox.Items.Add(c)


What the loop is doing is simply traversing all of the indeces (cells) of the arrays one at a time. Variable c only changes if the numbers in the cells are equal. Therefore we only need to count when the numbers are equal to each other:

a() contains:
3, 2, 5, 1, 7, 8, 3, 5, 6, 2, 3, 6, 1, 6, 5, 5, 7, 2, 5, 3

b() contains:
5, 3, 3, 4, 8, 2, 3, 5, 9, 5, 3, 7, 3, 7, 6, 3, 2, 1, 3, 4

This occurs three times, so the answer is 3.

No comments: