A2 Computer Science Programs
Question Sample paper 3(a)
Module Module1
MustInherit Class PV
Protected regno As String
Protected seats As Integer
Public Sub showregno()
Console.WriteLine(regno)
End Sub
Public Sub showseats()
Console.WriteLine(seats)
End Sub
End Class
Class bus : Inherits PV
Private maxstd As Integer
Public Sub New(ByVal rn As String, ByVal sea As Integer, ByVal ms As Integer)
regno = rn
seats = sea
maxstd = MS
Console.WriteLine("Detail of buses " & regno & " " & seats & " " & maxstd)
End Sub
Public Sub showmaxstd()
Console.WriteLine(maxstd)
End Sub
End Class
Class coach : Inherits PV
Public belts As Boolean
Public Sub New(ByVal rn, ByVal seats, ByVal sb)
regno = rn
seats = seats
belts = sb
Console.WriteLine("Detail of coaches " & regno & " " & seats & " " & belts)
End Sub
End Class
Sub Main()
Dim pv1 As bus = New bus("das 67", 45, 1)
Dim pv2 As bus = New bus("gfd 98", 67, 2)
Dim co1 As coach = New coach("XX 567", 56, True)
Dim CO2 As coach = New coach("GG 456", 43, False)
Console.ReadKey()
End Sub
End Module
Question Sample paper 3(b)
Sahir Masroor Ph. 03009543074 Page 1
A2 Computer Science Programs
Module Module1
MustInherit Class PV
Public regno As String
Public seats As Integer
Public Sub showregno()
Console.WriteLine(regno)
End Sub
Public Sub showseats()
Console.WriteLine(seats)
End Sub
End Class
Class bus : Inherits PV
Public maxstd As Integer
Public Sub New(ByVal rn As String, ByVal sea As Integer, ByVal ms As Integer)
regno = rn
seats = sea
maxstd = ms
Console.WriteLine(rn & " " & sea & " " & ms)
End Sub
'Public Sub showall(ByVal x, ByVal y, ByVal z)
' Console.WriteLine(regno & " " & seats & " " & maxstd)
'End Sub
Public Sub showmaxstd()
Console.WriteLine(maxstd)
End Sub
End Class
Sub Main()
Dim pv1 As bus = New bus("das 67", 45, 1)
Dim pv2 As bus = New bus("gfd 98", 67, 2)
pv1.showregno()
pv1.showseats()
pv1.showmaxstd()
Console.ReadKey()
End Sub
End Module
Program 2 Inheritance
Module Module1
Class A
Protected bvalue As Integer
Public Sub Display()
Console.WriteLine(bvalue)
End Sub
End Class
Class B : Inherits A
Public Sub New(ByVal value As Integer)
bvalue = value
End Sub
End Class
Sahir Masroor Ph. 03009543074 Page 2
A2 Computer Science Programs
Class C : Inherits A
Public Sub New(ByVal value As Integer)
bvalue = value * 2
End Sub
End Class
Sub Main()
Dim b As B = New B(5)
b.Display()
Dim c As C = New C(5)
c.Display()
Console.ReadKey()
End Sub
End Module
Program 3 (Hash Table)
Module Module1
Sub Main()
Dim table As Hashtable = New Hashtable
table.Add(100, "Perl")
table.Add(200, "Dot")
table.Add(300, "Net")
' Current count.
Console.WriteLine("Number of items " & table.Count)
' Clear.
table.Clear()
' Current count is now zero.
Console.WriteLine("new count " & table.Count)
Console.ReadKey()
End Sub
End Module
Program 4 (Hash Table)
Sahir Masroor Ph. 03009543074 Page 3
A2 Computer Science Programs
Module Module1
Sub Main()
Dim ht As Hashtable = New Hashtable()
Dim k As String
ht.Add("001", "Zara Ali")
ht.Add("002", "Abida Rehman")
ht.Add("003", "Joe Holzner")
ht.Add("004", "Mausam Benazir Nur")
ht.Add("005", "M. Amlan")
ht.Add("006", "M. Arif")
ht.Add("007", "Ritesh Saikia")
If (ht.ContainsValue("Nuha Ali")) Then
Console.WriteLine("This student name is already in the list")
Else
ht.Add("008", "Nuha Ali")
End If
' Get a collection of the keys.
Dim key As ICollection = ht.Keys
For Each k In key
Console.WriteLine(" {0} : {1}", k, ht(k))
Next k
Console.ReadKey()
End Sub
End Module
Program 5 (List)
Module Module1
Sub Main()
Dim list As New List(Of Integer)
list.Add(2)
list.Add(3)
list.Add(7)
' Loop through list elements.
Dim num As Integer
For Each num In list
Console.WriteLine(num)
Next
Console.readkey()
End sub
End module
Program 6 (List)
Sahir Masroor Ph. 03009543074 Page 4
A2 Computer Science Programs
Module Module1
Sub Main()
Dim list As New List(Of Integer)
list.Add(2)
list.Add(3)
list.Add(7)
' Loop through list with a for-to loop.
Dim i As Integer
For i = 0 To list.Count - 1
Console.WriteLine(list.Item(i))
Next i
Console.ReadKey()
End Sub
End Module
Program 7 (List)
Module Module1
Sub Main()
' Create a list of three integers.
Dim list As New List(Of Integer)(New Integer() {2, 3, 5})
' Loop through each number in the list.
' ... Then check it against the integer 3.
Dim num As Integer
For Each num In list
If (num = 3) Then
Console.WriteLine("Contains 3")
End If
Next
Console.ReadKey()
End Sub
End Module
Program 8 (List)
Sahir Masroor Ph. 03009543074 Page 5
A2 Computer Science Programs
Module Module1
Sub Main()
' Create a list of three integers.
Dim list As New List(Of Integer)(New Integer() {2, 3, 5})
' Loop through each number in the list.
' ... Then check it against the integer 3.
Dim num As Integer
For Each num In list
If (num = 3) Then
Console.WriteLine("Contains 3")
End If
Next
Console.ReadKey()
End Sub
End Module
Program 9 (List tail)
Module Module1
Sub Main()
Dim list As New List(Of Integer)(New Integer() {22, 23, 24, 25, 26})
list.Add(27)
list.Insert(4, 5)
Console.WriteLine("Whole List:")
Dim num As Integer
For Each num In list
Console.WriteLine(num)
Next
Console.WriteLine("First: {0}", list.First)
Dim str As String = New String("Tail:")
For i = 1 To list.Count - 1
str = str + " " + list(i).ToString()
Next
Console.WriteLine(str)
Console.ReadKey()
End Sub
End Module
Program 10 (Polymorphism)
Sahir Masroor Ph. 03009543074 Page 6
A2 Computer Science Programs
Module Module1
Sub Main()
Dim two As New One()
Console.WriteLine(two.add(10))
'calls the function with one argument
Console.WriteLine(two.add(10, 20))
'calls the function with two arguments
Console.WriteLine(two.add(10, 20, 30))
'calls the function with three arguments
Console.ReadKey()
End Sub
Public Class One
Public i, j, k As Integer
Public Function add(ByVal i As Integer) As Integer
'function with one argument
Return i
End Function
Public Function add(ByVal i As Integer, ByVal j As Integer) As Integer
'function with two arguments
Return i + j
End Function
Public Function add(ByVal i As Integer, ByVal j As Integer, ByVal k As Integer)
As Integer
'function with three arguments
Return i + j + k
End Function
End Class
End Module
Program 11 (polymorphism Overridable /overrides)
Module Module1
Public Class poly
Public Overridable Sub show()
Console.WriteLine("hello")
End Sub
End Class
Public Class poly2 : Inherits poly
Public Overrides Sub show()
Console.WriteLine("world")
End Sub
End Class
Sub Main()
Dim a As poly2 = New poly2()
a.show()
Console.ReadKey()
End Sub
End Module
Sahir Masroor Ph. 03009543074 Page 7
A2 Computer Science Programs
Program 12 (polymorphism)
Module Module1
Public Class poly
Public Overridable Sub show()
Console.WriteLine("hello")
End Sub
End Class
Public Class poly2 : Inherits poly
Public Overloads Sub show()
Console.WriteLine("world")
End Sub
End Class
Sub Main()
Dim a As poly2 = New poly2()
a.show()
Console.ReadKey()
End Sub
End Module
Program 13 (polymorphism)
Module Module1
Public Class poly
Public Sub show()
Console.WriteLine("hello")
End Sub
End Class
Public Class poly2 : Inherits poly
Public Overloads Sub show()
Console.WriteLine("world")
End Sub
End Class
Sub Main()
Dim a As poly2 = New poly2()
a.show()
Console.ReadKey()
End Sub
End Module
Program 13 (Exception handling)
Module Module1
Sahir Masroor Ph. 03009543074 Page 8
A2 Computer Science Programs
Sub Main()
Call TryExample()
Console.ReadKey()
End Sub
Public Sub TryExample()
' Declare variables.
Dim x As Integer = 5
Dim y As Integer = 0
' Set up structured error handling.
Try
' Cause a "Divide by Zero" exception.
x = x \ y
' This statement does not execute because program
' control passes to the Catch block when the
' exception occurs.
Console.writeline("end of Try block")
Catch ex As Exception
' Show the exception's message.
Console.writeline(ex.Message)
' Show the stack trace, which is a list of methods
' that are currently executing.
Console.writeline("Stack Trace: " & vbCrLf & ex.StackTrace)
Finally
' This line executes whether or not the exception occurs.
Console.writeline("in Finally block")
End Try
End Sub
End Module
Program 14 (Exception handling)
Module Module1
Sub Main()
Try
Dim n, d, r As Integer
n = 5
d = 0
r = d / r
Catch
Console.WriteLine("invalid")
End Try
Console.ReadKey()
End Sub
End module
Program 15 (exception handling)
Sahir Masroor Ph. 03009543074 Page 9
A2 Computer Science Programs
Program (general)
Module Module1
Sub Main()
Dim two As New One()
Console.WriteLine(two.add(10))
'calls the function with one argument
Console.WriteLine(two.add(10, 20))
'calls the function with two arguments
Console.WriteLine(two.add(10, 20, 30))
'calls the function with three arguments
Console.ReadKey()
End Sub
Public Class One
Public i, j, k As Integer
Public Function add(ByVal i As Integer) As Integer
'function with one argument
Return i
End Function
Public Function add(ByVal i As Integer, ByVal j As Integer) As Integer
'function with two arguments
Return i + j
End Function
Public Function add(ByVal i As Integer, ByVal j As Integer, ByVal k As Integer)
As Integer
'function with three arguments
Return i + j + k
End Function
End Class
End Module
Sahir Masroor Ph. 03009543074 Page 10