0% found this document useful (0 votes)
445 views4 pages

Department of Computer Science CSC 121 Test 3 Total Marks: 50 Duration: 2 Hours

This document contains instructions for a test on elementary computer programming concepts. It includes 12 multiple choice and written response questions covering topics like formatting data in ListBoxes, reading and writing to text files, generating random numbers, and identifying palindromic strings. Students are asked to write VB code to display data tables, calculate averages from files, and filter or write values to files based on certain criteria. The test is worth 50 total marks and takes 2 hours to complete.

Uploaded by

Siwe Mavundla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
445 views4 pages

Department of Computer Science CSC 121 Test 3 Total Marks: 50 Duration: 2 Hours

This document contains instructions for a test on elementary computer programming concepts. It includes 12 multiple choice and written response questions covering topics like formatting data in ListBoxes, reading and writing to text files, generating random numbers, and identifying palindromic strings. Students are asked to write VB code to display data tables, calculate averages from files, and filter or write values to files based on certain criteria. The test is worth 50 total marks and takes 2 hours to complete.

Uploaded by

Siwe Mavundla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Elementary Computer Programming

August 26, 2016

Department of Computer Science


CSC 121 Test 3
Total Marks: 50 Duration: 2 Hours
1. How do you display data in tabular form on a ListBox? [2]
 By using a fixed length font
 By using zones
2. What application do you use to open or create a text file? [1]
 Note pade
3. State any two (2) functions that can be used for formatting numerical values. [2]
 FormatCurrency
 FormatNumber
 FormatPercent
4. By default, where does Visual Basic save the files that are created at run time if the file path is
not specified? [1]
 Debug Folder
5. What value will be stored in an integer variable x if: [2]
a. x = Int(3.94) = 3 b. x = Int(-5.7) = -6
6. What object do you use to: [3]
a. Read from a text file line by line – StreamReader
b. Read from a delimitated text file – TextFieldParser
c. Write to a text file – StreamWriter
7. Write a program that will print the information in the table below on a Listbox using zones.
Make the size of each zone 30 and the contents in the first column must be right aligned. [5]
College Enrolment Endowment Public SS
Harvard 6660 19.2 0.659
Walden 5278 10.1 0.532
Dim value As Font
Dim fmtStr As String = "{0, -10}{1, 12}{2, 14}{3, 14}"
value = New Font("Courier New", 14)
ListBox1.Font = value
ListBox1.Items.Clear()
ListBox1.Items.Add(String.Format(fmtStr, "College", "Enrollment", "Endowment", "Public SS"))
ListBox1.Items.Add(String.Format(fmtStr, "Harvard", 6660, 19.2, 0.659))
ListBox1.Items.Add(String.Format(fmtStr, "Walden", 5278, 10.1, 0.532))

8. Write a program that emulates 50 tosses of a dice and prints out the number of times the number
6 occurs. [4]
Private Function TossDice() As Integer
Return (Int(6 * Rnd()) + 1)
End Function

Private Sub Button1_Click() Handles Button1.Click


Elementary Computer Programming
August 26, 2016

Dim k, count, toss As Integer


count = 0
For k = 1 To 50
toss = TossDice()
If toss = 6 Then
count = count + 1
End If
Next
Label11.Text = “6 occurred ” & count & “ times”
End Sub

9. A file called “numbers.txt” contains an unknown number of integers. Write a program that will
read the numbers from the file and print out: [8]
a. All odd numbers.
b. All perfect numbers.
c. The average of all the numbers.
Dim fileReader As IO.StreamReader = IO.File.OpenText("numbers.txt")
Dim sumFactors, count, sum, num, k As Integer
count = 0
sum = 0
While Not fileReader.EndOfStream
num = Val(fileReader.ReadLine)

If num Mod 2 = 1 Then


ListBox1.Items.Add(num)
End If

sumFactors = 0
For k = 1 To num
If num Mod k = 0 Then
sumFactors = sumFactors + k
End If
Next
If sumFactors = 2 * num Then
ListBox2.Items.Add(num)
End If

sum = sum + num


count = count + 1

End While
fileReader.Close()
Console.WriteLine(“{0}{2}{1}”, “Average of all the numbers is ”, sum/count, vbTab)
Elementary Computer Programming
August 26, 2016

10. A delimited file called “employees.txt” contains information about 100 employees of a
company in format: name, employee number, salary, tax. Write a program that will read the
information from the file and print out: [10]
a. The average salary of all the employees.
b. The total number of employees whose tax is more than R5000.
c. The employee names and employee numbers of all the employees who earn more than
R25000.
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(“employees.txt”)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
Dim currentRow(3) As String
Dim k, count, emp_num As Integer
Dim salary, tax, sum As single
Dim name As String
Dim fmtStr As String = "{0, 20}{1, 20}"
sum = 0
count = 0
ListBox1.Items.Add(“Employees Who Earn More Than R25000”)
For k = 1 To 100
currentRow = MyReader.ReadFields()
name = currentRow(0)
emp_num = currentRow(1)
salary = currentRow(2)
tax = currentRow(3)
sum = sum + salary
If tax > 5000 Then
count = count + 1
End If
If salary > 25000 Then
ListBox1.Items. Add(String.Format(fmtStr, name, emp_num))
End If
Next
End Using

11. Write a program that generates 500 integers less than 1000, if the integer generated is prime,
the program should write it to a file called “prime.txt”. [6]
Dim k, j, num, count As Integer
Dim fw As IO.StreamWriter
fw = New System.IO.StreamWriter("prime.txt", True)
For k = 1 To 500
num = Int(Rnd()*1000)
count = 0
For j = 1 To num
If num Mod j = 0 Then
count = count + 1
End If
Next
If count = 2 Then
Fw.WriteLine(num)
End If
Next
Fw.Close()
Elementary Computer Programming
August 26, 2016

12. A file “strings.txt” contains 20 Strings. Write a program that will write all palindrome strings
in the file into a file called “pali.txt”. [6]
Dim fileReader As IO.StreamReader = IO.File.OpenText("strings.txt")
Dim str, rev As String
Dim k, j, sum As Integer
Dim fp As IO.StreamWriter
fp = New System.IO.StreamWriter("pali.txt ", True)
For k = 1 To 20
str = fileReader.ReadLine
rev = “”
For j = Len(str) To 1 Step -1
rev = rev + Mid(str, j,1)
Next
If str = rev Then
fp.WriteLine(str)
End If
Next k
fp.Close()

You might also like