You are on page 1of 7

______________________________________________________________________________

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
_______________________

QUESTION 1

a) Write a program that allows user to enter student names and their marks (out of 100) in
each of their 6 exams. Store all the data in an array of records. Display;
i) The average marks in each exam.
i) The name of the student with the highest mark in each exam.
Display the form appropriately. Use a function to validate the marks and only accept a value
from 0 to 100. [20 marks]
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Marks(6), total, average As Integer
Dim i As Integer
Dim j As String
Dim student, chars As String

student(0) = Chars(TextBox1.Text)

Marks(0) = Val(TextBox2.Text)
Marks(1) = Val(TextBox3.Text)
Marks(2) = Val(TextBox4.Text)
Marks(3) = Val(TextBox5.Text)
Marks(4) = Val(TextBox6.Text)
Marks(5) = Val(TextBox7.Text)

For i = 0 To 6
Marks(i) = Marks

Next
For i = 0 To 6

total = Val(TextBox2.Text) + Val(TextBox3.Text) + Val(TextBox4.Text) +


Val(TextBox5.Text) + Val(TextBox6.Text) + Val(TextBox7.Text)
ListBox1.Items.Add("The total is " & total)
Next
average = total / 6
ListBox1.Items.Add("The average is " & average)

For j = 0 To 2

End Sub
End Class

a) Apart from Java and VB. Net name three other OOP languages you know [3 marks]
- Python
- Ruby
- Swift

QUESTION 2
a) With the aid of an example, clearly explain what a ReDim keyword is and its use.
[4
marks]
The ReDim statement is used to size or resize a dynamic array that has already been
formally declared by using a Private, Public, or Dim statement with empty parentheses.
Its syntax is: ReDim [Preserve] arrayname(subscripts)
Example :
Dim MyArray() As Integer ' Declare dynamic array.
Redim MyArray(5) ' Allocate 5 elements.
For I = 1 To 5 ' Loop 5 times.
MyArray(I) = I ' Initialize array.
Next I
a) What is a jagged array? [2 marks]
It is an array of arrays consisting of member arrays of different sizes.
Example of declaring a jagged array
Dim scores As Integer()() = New Integer(5)(){}
b) Give any two operators that can be used for string concatenation in VB [2 marks]
& ampersand symbol
+ the plus symbol
c) Clearly describe the following access modifiers:
i) Friend
This specifies that one or more declared programming elements are accessible
from only within the assembly that contains their declaration.
For example
Friend Sub New()
End Sub

i) Public
It is found in the declaration statement and this specifies that the element can be
accessed from code anywhere in the same project or other projects that reference
the project. For example: Public Class Odd Numbers
ii) Protected
It is a keyword in the declaration statement that specifies that the element can be
accessed only from within the same class. For example:
Protected Class ClassForFirstPlace
iii) Private
It is a keyword in the declaration statement specifying that the elements can be
accessed only from the same module, class or structure. For Example:
Private class As Interger
[4 x 3 marks]

QUESTION 3
a) What is the output from the following code snippets?

Private Sub btnDisplay_Click(…) Handles btnDisplay.Click


Dim amt1 As Integer = 1, amt2 As Integer = 2
lstOutput.Items.Add(amt1 & “ ” & amt2)
Swap(amt1, amt2)
lstOutput.Items.Add(amt1 & “ ” & amt2)
End Sub
Sub Swap(ByRef num1 As Integer, ByVal num2 As Integer)
Dim temp As Integer
temp = num1
num1 = num2
num2 = temp
lstOutput.Items.Add(num1 & “ ” & num2)
num2 = 3
End Sub [4 marks]

This code snippet will output

12
21
22

a) What is the output of the following program when the button is clicked?

Private Sub btnDisplay_Click(...) Handels btnDisplay.Click

Dim word1, word2, newWord As String


word1 = “memory”
word2 = “feather”
newWord = word1.Substring(2,2) & word2.Substring(3,4)
If newWord.Length > 3 Then
txtBox.Text = newWord
Else
txtBox.Text = “Sorry, not found.”
End If

End Sub [4 marks]

This program will output: mother


Line 4 word1.substring(2,2) will count to 2 characters in word 1 and take the 2 character “mo”

And word2.Substring will count 3 characters from word 2 and take the last 4 characters and join
“mo” and “ther” and assign the joined word to newWord.
b) Simplify the following code:

If (a < b < c) Then

txtOutput.Text = b & “is between “ & a & “ and “ & c

End If [2 marks]

QUESTION 4
a) Write a program to take as input two positive integers. The program is to output integers
counting from the first integer to the second integer. For example, the input 3, 7 will
output: 3 4 5 6 7 [10 marks]

a) Write a program that asks the user for an integer between 1 and 5 (inclusive). The
program is to check that the input is within the range and if not, prompt the user for a
valid integer until the input is within the range.
Private Function validatemark(ByVal num1 As Single) As Boolean
If (num1 >= 0 And num1 <= 5) Then
validatemark = True
Else
validatemark = False
End If
Return validatemark
End Function

Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles


btnAdd.Click
Dim number1, answer As Single
Dim errorr As Boolean
number1 = Val(txtNumber1.Text)

errorr = validatemark(number1)
If errorr = False Then
MessageBox.Show("Incorrect Range")
Else
answer = number1
MessageBox.Show("the answer is " & answer)
End If
End Sub
End Class

You might also like