THE KENYA NATIONAL EXAMINATIONS COUNCIL

DIPLOMA IN INFORMATION COMMUNICATION TECHNOLOGY

MODULE II

VISUAL PROGRAMMING

3 Hours

a) Outline the functions of each of the following standard properties used in a Visual Basic program:
i) Name: The unique identifier for a control. It is used to reference the control in code.
ii) Caption: The text displayed on a control, such as a label or button, that describes its function to the user.
iii) BackColor: The background color of a control. It is used to change the color that appears behind the content of a control.
iv) Text: The text contained within a control, such as in a textbox, which can be edited by the user or manipulated through code.

b) Explain three factors to consider when selecting a Visual Basic control to use in a program:
i) Purpose of the Control: Determine whether the control is appropriate for the task (e.g., using a TextBox for input, a Label for displaying text).
ii) User Interaction: Consider how the user will interact with the control (e.g., buttons for actions, checkboxes for multiple selections).
iii) Data Binding: Choose controls that support data binding if they are needed to display or manipulate data from a database.

c) Describe three considerations to be made when installing a Visual Basic programming environment:
i) System Requirements: Ensure that the computer meets the minimum hardware and software requirements to run Visual Basic.
ii) Compatibility: Check the compatibility of Visual Basic with the operating system and other software on the computer.
iii) Licensing: Verify that you have the appropriate licenses for Visual Basic and any additional tools or libraries.

2.
a) Write a Visual Basic program that would accumulate the sum of each of the following three integer numbers 10, 20, and 30:

Dim num1 As Integer = 10
Dim num2 As Integer = 20
Dim num3 As Integer = 30
Dim totalSum As Integer

totalSum = num1 + num2 + num3

MsgBox(“The sum of the numbers is: ” & totalSum)
b) Explain the function of the following sections in a Visual Basic program:
i) Declarations: Define variables, constants, and other elements used throughout the program.
ii) Event procedures: Contain code that is executed in response to user actions or events, such as clicking a button.
iii) General procedures: Modular blocks of code that can be called from different parts of the program to perform specific tasks.
iv) Statements: Individual lines of code that perform operations like assigning values or calling functions.

c) Differentiate between event-driven (Object-Event) and State-Event programming methods in a Visual Basic environment:

Event-driven (Object-Event): Code is executed in response to events triggered by user actions (e.g., clicks, key presses).
State-Event: Code is executed based on the state of the application or system, rather than direct user actions.
3.
a) Write a Visual Basic program for the ‘Add’ button that allows a user to add items to the list box when the ‘Add’ button is clicked:

Private Sub btnAdd_Click()
If txtItem.Text <> “” Then
lstBox.Items.Add(txtItem.Text)
txtItem.Clear()
Else
MsgBox(“Please enter an item to add.”)
End If
End Sub
b) Outline the procedure followed in shell sort algorithm in programming:

Step 1: Divide the list into smaller sublists using a gap sequence.
Step 2: Sort the sublists using insertion sort.
Step 3: Gradually reduce the gap sequence and continue sorting.
Step 4: Repeat the process until the gap is 1 and the list is fully sorted.
c) Explain each of the following interface controls in a Visual Basic program:
i) Labels: Display static text or information that cannot be edited by the user.
ii) Text boxes: Allow users to input or edit text.
iii) Combo boxes: Provide a drop-down list of items for the user to choose from, with an option to type in an entry.
iv) Command buttons: Trigger actions or events when clicked by the user.

4.
a) Write a Visual Basic program that calculates the total sales amount through the use of a procedure. The program should display an appropriate message box indicating the total commission:

Private Sub CalculateCommission()
Dim sales As Double
Dim commissionRate As Double
Dim commission As Double

sales = CDbl(txtSales.Text)

Select Case sales
Case Is < 15000
commissionRate = 0.1
Case 15000 To 19999
commissionRate = 0.12
Case 20000 To 29999
commissionRate = 0.14
Case 30000 To 39999
commissionRate = 0.15
End Select

commission = sales * commissionRate
MsgBox(“Total Commission: ” & commission)
End Sub
b) Outline four advantages of object-oriented programming over procedural programming:
i) Encapsulation: Combines data and methods into a single unit, enhancing security and modularity.
ii) Reusability: Classes and objects can be reused across multiple programs, reducing redundancy.
iii) Inheritance: Allows new classes to inherit properties and methods from existing classes, promoting code reuse and extension.
iv) Polymorphism: Enables methods to be used in different ways depending on the context, providing flexibility in the code.

c) Differentiate between sequential and binary search methods of data searching in a Visual Basic environment:

Sequential Search: Searches each element in a list one by one until the target is found or the list ends.
Binary Search: Searches by dividing a sorted list into halves, reducing the search area by half with each step until the target is found or the search space is empty.
5.
a) Write a Visual Basic program that displays a menu to enter the first 5 integers through the use of the InputBox function. The program should display the sum of the integers in a message box:


Dim sum As Integer = 0
Dim num As Integer
Dim i As Integer

For i = 1 To 5
num = CInt(InputBox(“Enter integer ” & i & “:”))
sum += num
Next

MsgBox(“The sum of the integers is: ” & sum)
b) Outline four factors to consider when selecting a data source in a Visual Basic program:
i) Data Format: Ensure the data format (e.g., XML, SQL) is compatible with the program requirements.
ii) Connectivity: Consider how the data source will be connected to the program (e.g., ODBC, ADO).
iii) Performance: Evaluate the data source’s speed and efficiency for handling queries and transactions.
iv) Scalability: Choose a data source that can handle the potential growth of data over time.

c) Explain each of the following types of Visual Basic programming environments:
i) Single environment: A standalone environment where all development and execution take place within one system.
ii) Multi-environment: Involves multiple systems or platforms where development may occur in one environment, and execution or deployment happens in another.

6.
a) Explain each of the following activities carried out during program development in Visual Basic programming:
i) Compilation: The process of translating the source code into machine code that can be executed by the computer.
ii) Debugging: Identifying and fixing errors or bugs in the code to ensure the program runs correctly.
iii) Code walkthrough: A manual review of the source code by a team to check for logic errors, adherence to coding standards, and potential improvements.

b) Explain how to enable Visual Basic commands to run in the three fields of the Author table as shown below

Dim authorID As Integer = CInt(txtAuthorID.Text)
Dim authorName As String = txtAuthorName.Text
Dim nationality As String = txtNationality.Text

‘ Example of inserting a new record into the Author table
Dim sql As String = “INSERT INTO Author (Author_ID, Author_Name, Nationality) VALUES (” & authorID & “, ‘” & authorName & “‘, ‘” & nationality & “‘)”
‘ Execute the SQL command using a command object
c) Write a Visual Basic code that accepts the user’s input for the Author_ID field and displays all the records associated with that Author_ID in the Author table:


Dim authorID As Integer = CInt(InputBox(“Enter Author ID:”))

Dim sql As String = “SELECT * FROM Author WHERE Author_ID = ” & authorID
Dim cmd As New OleDbCommand(sql, connection)
Dim reader As OleDbDataReader = cmd.ExecuteReader()

While reader.Read()
MsgBox(“Author Name: ” & reader(“Author_Name”) & vbCrLf & “Nationality: ” & reader(“Nationality”))
End While

reader.Close()
7.
a) With the aid of an example, outline the functions of each of the following operators as used in a Visual Basic program:
i) AND: Logical operator that returns True if both operands are true. Example: If (a > 0) AND (b > 0) Then MsgBox(“Both are positive”).
ii) OR: Logical operator that returns True if at least one operand is true. Example: If (a > 0) OR (b > 0) Then MsgBox(“At least one is positive”).
iii) MOD: Arithmetic operator that returns the remainder of a division operation. Example: result = 10 MOD 3 returns 1.
iv) IsEmpty: Function that checks whether a variable is initialized or contains any data. Example: If IsEmpty(variable) Then MsgBox(“Variable is empty”).

b) State four tests that can be performed in data environment designer as used in the generation of Visual Basic programs:
i) Connection Test: Verify the connection to the data source is successful.
ii) Query Execution Test: Ensure that SQL queries return the expected results.
iii) Schema Test: Validate the structure and format of tables and fields in the database.
iv) Data Integrity Test: Check for consistency and accuracy of data within the database.

c) With the aid of an example, explain each of the following terms as used in arrays:
i) Indexing: Refers to the position of an element in an array. Example: array(2) refers to the third element in the array.
ii) Sorting: The process of arranging elements in an array in a specific order, such as ascending or descending. Example: Array.Sort(myArray) sorts the elements in ascending order.

8.
a) Write a Visual Basic program that displays a menu to enter the first 5 integers through the use of the InputBox function. The program should display the sum of the integers in a message box:

Dim sum As Integer = 0
Dim num As Integer
Dim i As Integer

For i = 1 To 5
num = CInt(InputBox(“Enter integer ” & i & “:”))
sum += num
Next

MsgBox(“The sum of the integers is: ” & sum)
b) Explain each of the following types of Visual Basic programming environments:
i) Single environment: A standalone environment where all development and execution take place within one system.
ii) Multi-environment: Involves multiple systems or platforms where development may occur in one environment, and execution or deployment happens in another.

c) Differentiate between local variable and global variable as used in a Visual Basic environment:

Local Variable: A variable that is declared within a procedure or block of code and can only be accessed within that scope.
Global Variable: A variable that is declared at the module level and can be accessed by any procedure within that module or program.
9.
a) Integrate the following Visual Basic code statement by statement:

Dim numerator As Integer
Dim denominator As Integer
Dim quotient As Single

For position = 1 To 10
quotient = numerator / denominator
MsgBox(“Quotient at position ” & position & “: ” & quotient)
Next position
b) Figure 2 shows the interface of a load system used for selecting load items from the database as shown below:

LOAD SYSTEM

Load Item Price
(Add) (Delete)
Write a Visual Basic program for the following command buttons

‘ Add button click event
Private Sub btnAdd_Click()
If lstAvailable.SelectedItem IsNot Nothing Then
lstSelected.Items.Add(lstAvailable.SelectedItem)
lstAvailable.Items.Remove(lstAvailable.SelectedItem)
Else
MsgBox(“Please select an item to add.”)
End If
End Sub

‘ Delete button click event
Private Sub btnDelete_Click()
If lstSelected.SelectedItem IsNot Nothing Then
lstAvailable.Items.Add(lstSelected.SelectedItem)
lstSelected.Items.Remove(lstSelected.SelectedItem)
Else
MsgBox(“Please select an item to delete.”)
End If
End Sub
THIS IS THE LAST PRINTED PAG