O Level Programming Pre-Release Material
O Level Programming Pre-Release Material
Computer Science
Programming Workbook
Compiled by
Section 2
Topics
Theory of Computer Science
1.1 Data representation
1.1.1 Binary systems
1.1.2 Hexadecimal
1.1.3 Data storage
1.2 Communication and Internet technologies
1.2.1 Data transmission
1.2.2 Security aspects
1.2.3 Internet principles of operation
1.3 Hardware and software
1.3.1 Logic gates
1.3.2Computer architecture and the fetch-execute cycle
1.3.3 Input devices
1.3.4 Output devices
1.3.5 Memory, storage devices and media
1.3.6 Operating systems
1.3.7High- and low-level languages and their
translators
1.4 Security
1.5 Ethics
Practical Problem-solving and
Programming
2.1 Algorithm design and problem-solving
2.1.1 Problem-solving and design
2.1.2 Pseudocode and flowcharts
2.2 Programming
2.2.1 Programming concepts
2.2.2 Data structures; arrays
2.3 Databases
Assessment at a glance
Components
Paper 1 Theory
1 hour 45 minutes
This written paper contains short-answer and structured questions. All
questions are compulsory.
No calculators are permitted in this paper.
75 marks
Externally assessed.
Paper 2 Problem-solving and Programming
1 hour 45 minutes
This written paper contains short-answer and structured questions. All
questions are compulsory. 20 of the marks for this paper are from questions
set on the pre-release material. 1
No calculators are permitted in this paper.
50 marks
Externally assessed.
Page 2
Weighting
60%
40%
ruknuddin.com
2.2 Programming
Visual Basic .NET (VB.NET) is an object-oriented computer programming language
implemented on the .NET Framework. Although it is an evolution of classic Visual Basic
language, it is not backwards-compatible with VB6, and any code written in the old
version does not compile under VB.NET. Like all other .NET languages, VB.NET has
complete support for object-oriented concepts. Everything in VB.NET is an object,
including all of the primitive types (Short, Integer, Long, String, Boolean, etc.) and user
defined types, events, and even assemblies. All objects inherits from the base class
Object.
VB.NET is implemented of Microsoft's .NET framework. Therefore it has full access all
the libraries in the .Net Framework. It's also possible to run VB.NET programs on Mono,
the open-source alternative to .NET, not only under Windows, but even Linux or Mac
OSX.
The following reasons make VB.Net a widely used professional language:
Modern, general purpose.
Object oriented.
Component oriented.
Easy to learn.
Structured language.
It produces efficient programs.
It can be compiled on a variety of computer platforms.
Part of .Net Framework.
Strong Programming Features VB.Net
VB.Net has numerous strong programming features that make it endearing to multitude
of programmers worldwide. Let
us mention some of these features:
Boolean Conditions
Automatic Garbage Collection
Standard Library
Assembly Versioning
Properties and Events
Delegates and Events Management
Easy to use Generics
Indexers
Conditional Compilation
Simple Multithreading
VB.NET - ENVIRONMENT
Integrated Development Environment (IDE) For VB.Net
Microsoft provides the following development tools for VB.Net programming:
Page 3
ruknuddin.com
A VB.NET console application uses a command line window for its user interface.
Because there is no opportunity for the user to provide inputs other than those asked for
by the program, a console application is not event driven. The actions performed in the
program must follow in a linear fashion, and are completely defined by the program, and
thus the programmer. This procedural programming is limiting, but makes a good
introduction since the complexity of the user interface is eliminated.
1. Start Microsoft Visual Studio.NET. Note that it is not listed as Visual Basic.NET. Visual
Studio.NET supports other programming languages other than VB. A Start Screen is
displayed to help you open new or existing VB projects.
Page 4
ruknuddin.com
5. Enter ConsoleTut as the name of the project. All the files associated with the project
will be stored in a folder as indicated in the Location text box (note this so you can find
it later). Click OK to create the project.
Page 5
ruknuddin.com
6. The VS (Visual Studio) IDE (Integrated Development Environment) is where you build
your program. You can actually create any VB program in Notepad, but the IDE provides
a large number of tools that greatly simplifies the process of creating a Windows
program.
7. For this tutorial, you can ignore most of the windows and tools in the IDE. The
Solution Explorer in the top right corner of the IDE shows the files associated with the
project. The console application template provides the required files. The Module1.vb
file is the active file, and its contents are shown in the editor window.
Page 6
ruknuddin.com
8. The code contained within the Module1.vb file is listed between the two statements:
Module Module1
End Module
The module currently contains a single subroutine (dont worry too much about terms
just yet) called Main. The code inside the Main subroutine is automatically run when
the program starts. You will add all the code for this tutorial in between the Sub Main()
and End Sub statements.
Tools: (Console.ReadLine for Input, Console.WriteLine for Output)
The console application simplifies input and output in that the user interface is essentially text
based. The user types in the input, and the output is posted to the same text window. This text
window is referenced in your program code by using the keyword Console. Three actions
associated with the Console object enable you to receive input (ReadLine) and output (Write and
WriteLine) text characters to the console window.
Variables:
A variable is a named location in the computers memory that stores a certain type of data
(character, string of characters, integer, real number, and so on). The variable is identified in the
program by a unique name.
By storing each of the inputs in memory (in a variable), you can use them again and again within
the program without requiring that the user re-input them.
Variable Declaration:
Before variables can be used to store data, they must be Declared. This reserves the
appropriate amount of memory to store the value assigned to the variable.
1. In the editor window, below Sub Main() but above End Sub, type:
DIM Name As String
DIM Weight1, Weight2, Weight_Difference As Single
DIM Count As Integer
First program
Start a new Project and select Console Application. Type the following code:
Page 7
ruknuddin.com
module module1
sub main()
console. writeline("In the name of Allah)
Console.writeline(Hello World!")
console. readKey()
end sub
end module
(Press F5 to run the code)
Tasks
0.1 Write a program that displays the message Asslam-o-Alaikum
0.2 Write a program that displays the message My name is Muhammad and I live in Brussels
(replace Dave and Brussels with your own information)
0.3 Write a program that displays the lyrics to your national anthem. Each line of the song
should be on a fresh line.
Example Program 1 - Assignment - Integer, byte, real, boolean, character, string, date/time.
Module Module1
Sub Main()
Dim theNumber As Integer
Dim theWord As String
theWord = "Bird"
theNumber = 9
Console.WriteLine(theWord & " is the word")
Console.WriteLine("And the number is " & theNumber)
Console.ReadKey()
theWord = "Cat"
Console.WriteLine("Enter a number>")
theNumber = Int(Console.ReadLine())
Console.WriteLine("Now " & theWord & " is the word and the number is " &
theNumber)
Console.ReadKey()
End Sub
End Module
Page 8
ruknuddin.com
Tasks
3.1) Write a program that divides a number entered by the user by 2
3.2) Write a program that displays the 7 times table
3.3) Write a program that displays any times table the user requests
Tasks
1. Write a program to ask the user what 24+9 is. Say Excellent if they get it right.
2. Write a program to ask the user how many in a bakers dozen? and say most
excellent if they get it right.
3. Write a program to ask the user to enter their age. If their age is under 18 then say
Sorry, this film is not for you.
4. Write a program to ask the user for two numbers. Compare the first with the second
and then print out one of three messages. Either the numbers are equal, the first is
bigger, or the second is bigger. You will need more than one IF to solve this one.
5. Write a program which asks the user to enter their password. If they enter the word
Page 9
ruknuddin.com
Page 10
ruknuddin.com
Page 11
ruknuddin.com
num As Double
rounded As Integer
squarert As Double
trunc As Integer
Tasks
1) Write a program that asks for 5 numbers, calculates the mean average and then rounds it
down. Display the result on screen.
Tasks
Tasks
1. Write a program that checks a username against a stored value. How the user enters the
username should NOT be case sensitive.
2. Adapt program 1 so that it also takes in a password. If the user enters spaces after the
password the computer will trim them out automatically.
3. Write a program that will check a phone number is of the correct length.
4. Write a program that asks for a users full name in one inputbox/textbox but then stores the
first and second names in different variables.
Page 12
ruknuddin.com
Tasks
1. Write a program which asks for your name and then displays it 5 times on the screen.
2. Write a program to display the name of the town you live in 10 times.
3. Write a program to ask for a persons favourite CD and the artist. Both should be displayed on
the same line 5 times.
4. Write a program to ask for a number and display its multiplication table 1 to 100
5. Write a program that asks the user for a number 5 times and adds them all up to give a total.
Page 13
ruknuddin.com
Tasks
1) Write a program which will set up an array to hold 50 numbers. Call the array numbers. Display
the array's contents across the screen. They should all be 0.
2) Create a program that stores an array of car records. At least 5 cars and 4 fields per record.
3) Create a program that stores an array of 5 people records. The information should be entered by
the user.
4) Adapt program 2 to now do a linear search for a certain car and display its details.
Tasks
1) Write a program that validates a user is old enough to drive (older than 17, younger than 80)
2) Write a program that checks that a telephone number entered is long enough (string length)
3) Write a program that checks that both a username and password is correct before allowing
you to proceed.
Page 14
ruknuddin.com
Tasks
1) Write a program that reads the students names from a txt file and displays them on the
screen
2) Write a program that reads 10 team names from a txt file and stores them in an array
3) Write a program that reads 5 song titles from a csv file and displays them on the screen
4) Write a program that reads 20 team names from a csv file into an array, then displays the
array on screen
Page 15
ruknuddin.com
Page 16
ruknuddin.com
Main()
Name(30) AsString
Count AsInteger
Weight1(30) AsSingle
'Task 1
For Count = 1 To 5
Console.WriteLine("Student No. : "& Count)
Console.Write("Enter name : ")
Name(Count) = Console.ReadLine()
Console.Write("Enter Weight at day 1 of term ")
Weight1(Count) = Console.ReadLine()
'Validation Check for Weight
While Weight1(Count) < Lower_Limit Or Weight1(Count) > Upper_Limit
Console.WriteLine("Error: Invalid weight. It must be between 5
and 500")
Console.Write("Re-enter weight on first day ")
Weight1(Count) = Console.ReadLine()
EndWhile
Next
'For Displaying list of name and weight of students
For Count = 1 To 5
Console.WriteLine(Name(Count) &" "& Weight1(Count))
Next
'Task 2
Dim weight2(30), Weight_Difference(30) AsSingle
For Count = 1 To 5
Console.WriteLine(Count &"
"& Name(Count) &" "& Weight1(Count))
Console.Write("Enter weight on last day ")
weight2(Count) = Console.ReadLine()
'Validation Check for Weight
While weight2(Count) < Lower_Limit Or weight2(Count) > Upper_Limit
Console.WriteLine("Error: Invalid weight. It must be between 5
and 500")
Console.Write("Re-enter weight on lastt day ")
weight2(Count) = Console.ReadLine()
EndWhile
Page 17
ruknuddin.com
Console.ReadKey()
EndSub
EndModule
Page 18
ruknuddin.com
Sample Questions
Q1. (a) Declare the array to store each students names and weights on day 1, weights
last day and weight differences.
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................
(b) (i) Show the design of your algorithm to complete Task 1 using pseudo-code,
programming statements or a flowchart.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................
(c) (i) Show the design of your algorithm to complete Task 2 using pseudo-code,
programming statements or a flowchart.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
Page 19
ruknuddin.com
...............................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................
(d) State validation rule you have used in task 1 and justify your rule.
Validation Rule: ........................................................................................................................
...................................................................................................................................................
Justification: ...........................................................................................................................
...................................................................................................................................................
(e) Show two different sets of student data that you could use to check the validation
used in Task 1. Explain why you chose each data set.
Set 1: ..............................................................................................................................................
Reason for choice: .......................................................................................................................
...................................................................................................................................................
Set 2: ..............................................................................................................................................
Reason for choice: .......................................................................................................................
...................................................................................................................................................
(f) (i) Explain how students with difference in weight will be chosen (Task 3). You may
include pseudo-code or programming statements to help illustrate your explanation.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................
(ii) How does your program work if we need to know how many students have gained
and lost their weights? You may include pseudo-code or programming statements to help
Page 20
ruknuddin.com
Page 21
ruknuddin.com