A pencil-and-paper workbook: save data in files, understand life online, then get exam-ready.
Variables disappear when a program closes. A file keeps data on the disk so that it is still there tomorrow. You will learn the few commands that open, read, write and close a text file, and you will use only these commands in your PAT and in exams.
var tFile : TextFile; // logical name (a variable) begin AssignFile(tFile, 'Sales.txt'); // link it to the physical name on the disk // from now on we only use tFile in the commands
| Command | What it does |
|---|---|
| AssignFile(tFile, 'Data.txt') | Links the logical name to the physical name. Always first. |
| Reset(tFile) | Opens an existing file for reading, starting at the first line. |
| Rewrite(tFile) | Creates a new empty file for writing. If the file exists, everything in it is erased. |
| Append(tFile) | Opens an existing file for writing at the end. Old lines are kept. |
| ReadLn(tFile, sLine) | Reads one whole line into sLine and moves to the next line. |
| WriteLn(tFile, sText) | Writes sText to the file as one new line. |
| EOF(tFile) | End Of File: True when there is nothing left to read. |
| CloseFile(tFile) | Saves the data and closes the file. Never forget it. |
// declared: tFile : TextFile; sLine : String;
if FileExists('Names.txt') then
begin
AssignFile(tFile, 'Names.txt');
Reset(tFile); // open for reading
while not EOF(tFile) do
begin
ReadLn(tFile, sLine); // one line per pass
memOut.Lines.Add(sLine);
end;
CloseFile(tFile); // always close
end
else
ShowMessage('Names.txt was not found');AssignFile(tFile, 'Sales.txt');
if FileExists('Sales.txt') then
Append(tFile) // keep the old sales
else
Rewrite(tFile); // first sale ever: create the file
WriteLn(tFile, edtAmount.Text); // one new line
CloseFile(tFile);Circle the command that opens the file correctly for the job.
| 1 | Read the marks that are already in Marks.txt. | Reset Rewrite Append |
| 2 | Every Monday, create a fresh Report.txt and throw away last week's. | Reset Rewrite Append |
| 3 | Add today's sale to the end of Sales.txt without losing earlier sales. | Reset Rewrite Append |
| 4 | Show every line of Names.txt in a memo. | Reset Rewrite Append |
| 5 | Save the very first high score, when no file exists yet. | Reset Rewrite Append |
Sales.txt holds four lines of text: 25, 40, 15 and 60 (one rand amount per line). Complete a row for every check of the loop. The last row is the check that stops the loop, and the values stay as they were.
iTotal := 0; AssignFile(tFile, 'Sales.txt'); Reset(tFile); while not EOF(tFile) do begin ReadLn(tFile, sLine); iAmount := StrToInt(sLine); iTotal := iTotal + iAmount; end; CloseFile(tFile);
| Check | EOF(tFile) | sLine | iTotal |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 |
Someone saves Sales.txt and accidentally leaves an empty line at the end. What happens when the loop reads that line, and why?
Log.txt does not exist before this code runs. Write the lines that Log.txt contains after each of the three blocks.
AssignFile(tFile, 'Log.txt'); // block 1 Rewrite(tFile); WriteLn(tFile, 'Monday'); WriteLn(tFile, 'Tuesday'); CloseFile(tFile); AssignFile(tFile, 'Log.txt'); // block 2 Append(tFile); WriteLn(tFile, 'Wednesday'); CloseFile(tFile); AssignFile(tFile, 'Log.txt'); // block 3 Rewrite(tFile); WriteLn(tFile, 'Thursday'); CloseFile(tFile);
This code should add up the numbers in Numbers.txt and show the total. Find four errors.
var
tFile : TextFile;
sLine : String;
iTotal : Integer;
begin
iTotal := 0;
Reset(tFile);
while EOF(tFile) do
begin
ReadLn(sLine);
iTotal := iTotal + StrToInt(sLine);
end;
lblTotal.Caption := IntToStr(iTotal);
end;Draw a simple diagram that shows: your Delphi program with the variable tFile, the file Marks.txt on the disk, and what AssignFile, Reset and ReadLn do. Use arrows and short labels.
Sales.txt holds one whole-rand amount on each line. Write code that displays the total in lblTotal and the number of sales above R30 in lblBig. Check that the file exists first. (Variables: tFile, sLine, iAmount, iTotal, iBig are declared.)
A learner's name is typed in edtName and the mark in edtMark. Add one line to the existing file Results.txt in the form Thabo: 56, keeping the lines already in the file.
Names.txt holds one learner name per line. Write code that copies only the names that start with S into a new file SNames.txt. You need two file variables: tIn and tOut.
Most of what you do on a phone happens through internet services. You will learn what online storage, synchronisation and collaboration mean, how to shop and share safely, and how to write answers that earn full marks.
Circle the service that each situation describes best.
| 1 | Mpho saves her Maths project on a provider's servers so that she can open it at school or at home. | Storage Sync Collaborate |
| 2 | Her phone, tablet and laptop all show the newest version of her timetable, without her copying anything by hand. | Storage Sync Collaborate |
| 3 | Three learners type into the same presentation at the same time and leave comments for each other. | Storage Sync Collaborate |
| 4 | Dad uploads 40 holiday photos so that they are safe if the laptop is stolen. | Storage Sync Collaborate |
| 5 | When the Wi-Fi comes back after load shedding, her offline edits appear on every device. | Storage Sync Collaborate |
| 6 | The teacher shares a document and decides that learners may comment but not edit. | Storage Sync Collaborate |
Everything starts as version 1 (v1) on the laptop, the phone and in the cloud. Complete what each place holds after every event.
| Event | Laptop | Phone | Cloud |
|---|---|---|---|
| 1. The phone is switched off. Thandi edits the report on the laptop, online. | |||
| 2. The phone is switched on and connects to the internet. | |||
| 3. Load shedding: the router is off. She edits the report again on the laptop. | |||
| 4. Power returns and the laptop reconnects. |
Circle what each clue tells you about an online shop.
| 1 | The address begins with https and shows a padlock. | Safe sign Warning |
| 2 | The seller accepts payment only by EFT into a personal bank account. | Safe sign Warning |
| 3 | The site shows a street address, a phone number and a clear return policy. | Safe sign Warning |
| 4 | A famous laptop for R1 500, with a countdown clock saying “2 minutes left”. | Safe sign Warning |
| 5 | Your bank sends a one-time PIN to your phone to approve the payment. | Safe sign Warning |
| 6 | An email says your parcel is held and asks you to click a link and type your card number. | Safe sign Warning |
Look at the model answer in Write in sentences, not lists. Copy the sentence that makes the point, the sentence that gives the example, and the sentence that says how to reduce the risk.
A learner answered the question Explain why cloud storage is useful for a learner like this: “- anywhere - safe - backup”. Rewrite the answer as a short paragraph of 2 or 3 full sentences.
Write a paragraph of 3 or 4 sentences. Start with: One risk of social media is…. Include a point, an explanation, an example and how the risk can be reduced.
A family in a small town far from the nearest big shop decides to buy mostly online. In a paragraph, discuss one advantage and one risk for the family.
Time to bring Terms 1 to 3 together: operators, IF and CASE, Boolean logic, loops, strings and number algorithms. Do this unit like a mini exam: pencil, no computer, and show every step.
| The problem needs… | Use | Remember |
|---|---|---|
| A choice by condition or range | IF … THEN … ELSE | Brackets round each AND / OR condition; no ; before ELSE |
| A choice by exact value | CASE … OF | Ranges like 1..5; ELSE catches the rest |
| A known number of repeats | FOR | to counts up, downto counts down |
| Unknown repeats; may be zero | WHILE | Change something inside the loop |
| Unknown repeats; at least one | REPEAT … UNTIL | Stops when the condition is True |
| Whole part / remainder | DIV / MOD | 17 DIV 5 = 3 and 17 MOD 5 = 2 |
| Every character of a string | FOR + s[i] | Positions start at 1 |
iTotal := 0; // running total
for i := 1 to 5 do
iTotal := iTotal + i;
bFound := False; // flag: assume No
for i := 1 to Length(sWord) do
if sWord[i] = 'Z' then
bFound := True; // change it when foundCircle the correct value.
| 1 | 17 DIV 5 | 2 3 3.4 |
| 2 | 17 MOD 5 | 2 3 3.4 |
| 3 | Trunc(8.9) | 8 9 8.9 |
| 4 | Sqr(6) | 12 36 2.45 |
| 5 | Length('Mzansi') | 5 6 7 |
| 6 | Ord('C') | 65 66 67 |
| 7 | Chr(Ord('A') + 4) | D E F |
Given bRain = True, bCold = False and iTemp = 12, write True or False for each expression.
| Expression | True or False? |
|---|---|
| bRain and bCold | |
| bRain or bCold | |
| not bRain | |
| (iTemp < 15) and bRain | |
| not (bCold or (iTemp > 20)) | |
| (iTemp mod 5 = 2) and (not bCold) |
Trace this code. For each check write whether the condition is True, then the values after the body. The last row is the check that stops the loop.
iNum := 4086; iSum := 0; while iNum > 0 do begin iSum := iSum + iNum mod 10; iNum := iNum div 10; end;
| Check | iNum > 0 ? | iSum | iNum |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 |
In one sentence, say what the loop above calculates, and give the value of iSum.
What does lblOut show? Describe in one sentence what the code does.
sIn := 'Cape Town';
sOut := '';
for i := 1 to Length(sIn) do
if sIn[i] in ['a'..'z'] then
sOut := sOut + Chr(Ord(sIn[i]) - 32)
else
sOut := sOut + '-';
lblOut.Caption := sOut;What are the final values of bPrime and iDiv? Trace carefully: when does the loop stop?
iNum := 21;
bPrime := True;
iDiv := 2;
while (iDiv < iNum) and bPrime do
begin
if iNum mod iDiv = 0 then
bPrime := False;
Inc(iDiv);
end;This code should say whether the age in edtAge is from 14 to 19. Find three errors.
var
iAge : Integer;
begin
iAge := StrToInt(edtAge.Text);
if iAge >= 14 and iAge <= 19 then
lblOut.Caption = 'In range'
else
lblOut.Caption := 'Out of range'
ShowMessage('Checked');
end;A route number from 1 to 4 is chosen in sedRoute. Use a CASE statement to show the fare in lblFare: route 1 costs R12, route 2 costs R15, routes 3 and 4 cost R22. Any other route must show No such route.
A palindrome reads the same forwards and backwards (MADAM, LEVEL). The word is in sWord. Using a loop and a Boolean flag, show Palindrome or Not a palindrome in lblOut. Compare the first letter with the last, the second with the second last, and so on.
Paper 2 tests what you understand about computers: number systems, data storage, hardware, networks, security and the law. This unit checks the whole year, one topic at a time.
| Topic | Remember |
|---|---|
| Hardware | Input (keyboard, mouse, scanner, microphone), output (monitor, printer, speakers, projector), processing (CPU), memory (RAM, temporary), storage (HDD, SSD, flash drive), communication (NIC, router, modem). |
| Networks | PAN (about arm's reach), HAN (one home), LAN (one building, such as a school lab), WAN (cities and countries; the internet). Client-server has a central server; peer-to-peer has equal computers. |
| Security | Virus (needs a file to be opened), worm (spreads by itself), trojan (pretends to be useful), ransomware (locks files for payment), spyware (spies), phishing (fake messages). Antivirus removes malware; firewall filters network traffic; UPS survives power cuts. |
| POPIA | The law that protects personal information. An organisation must collect only what it needs, ask for permission, use it for the stated purpose, keep it safe and let people see and correct their data. |
Convert as asked. The binary answers must have 8 bits. Write the place values above the bits if it helps.
| Convert | Answer |
|---|---|
| 00001101 (binary) to decimal | |
| 00101010 (binary) to decimal | |
| 10010110 (binary) to decimal | |
| 11111111 (binary) to decimal | |
| 19 (decimal) to binary | |
| 64 (decimal) to binary | |
| 100 (decimal) to binary | |
| 205 (decimal) to binary |
Use the space to show the repeated division by 2 for 205. Write each division with its remainder, then circle the remainders in the order you must read them.
Write the answer. Remember A = 10, B = 11, C = 12, D = 13, E = 14, F = 15.
| Convert | Answer |
|---|---|
| 3C (hex) to decimal | |
| B5 (hex) to decimal | |
| 175 (decimal) to hex | |
| 250 (decimal) to hex | |
| 11010111 (binary) to hex | |
| 5E (hex) to 8-bit binary |
Choose the device from the word bank and write it next to its description.
Word bank: barcode scanner · CPU · microphone · NIC · projector · RAM · router · SSD
| Description | Device |
|---|---|
| Carries out the instructions of a program | |
| Fast flash storage with no moving parts | |
| Temporary memory for programs and data in use; emptied when the power goes off | |
| Connects a LAN to the internet and directs data using IP addresses | |
| Reads black and white stripes at the till | |
| The card that lets a computer connect to a network | |
| Shows a computer's screen on the classroom wall | |
| Changes sound waves into digital data |
Circle PAN, HAN, LAN or WAN.
| 1 | Bluetooth earphones paired with a phone | PAN HAN LAN WAN |
| 2 | All the devices on the Wi-Fi in one family home | PAN HAN LAN WAN |
| 3 | The computers in a school lab | PAN HAN LAN WAN |
| 4 | A bank linking its branches in Cape Town and Durban | PAN HAN LAN WAN |
| 5 | A smartwatch linked to a phone | PAN HAN LAN WAN |
Write the word from the word bank that matches each description.
Word bank: phishing · ransomware · spyware · trojan · virus · worm
| Description | Threat |
|---|---|
| Attaches itself to a file and spreads when someone opens that file | |
| Spreads across a network by itself, without anyone opening a file | |
| Looks like a useful program, but secretly lets an attacker in | |
| Locks your files and demands money to unlock them | |
| Secretly records what you type and the sites you visit | |
| A fake message that tricks you into typing your password on a false website |
Circle the security measure that solves each problem.
| 1 | A program tries to send your files out to the internet. | UPS Firewall Encryption |
| 2 | A downloaded game hides a virus. | Antivirus Access rights UPS |
| 3 | Load shedding switches the PC off mid-save. | UPS Firewall Antivirus |
| 4 | A stolen laptop's files must stay unreadable. | Encryption UPS Firewall |
| 5 | Learners may open only their own folders. | Antivirus Access rights UPS |
A gym collects its members' ID numbers, cell numbers and home addresses. In a short paragraph, explain two things that POPIA requires the gym to do with this information.
Knowing the work is only half of it. Learners also lose marks by misreading the question, writing too little (or too much), skipping their working and running out of time. This unit trains the habits that protect your marks.
| Command word | What you must do | How much to write |
|---|---|---|
| Give / Name / State / List | Write the answer only. | A word, a short phrase or one sentence. |
| Define | Say exactly what the term means. | One clear sentence. |
| Explain | Say what it is and why or how. | One point and its reason for each mark. |
| Describe | Say what it looks like or how it works. | A few connected sentences. |
| Discuss | Give several points (often advantages and disadvantages) and explain each one. | A paragraph; every point explained. |
| Motivate / Justify | Choose, and then give reasons for your choice. | The choice plus explained reasons. |
| Differentiate / Compare | Show how two things are alike or different. | Both items mentioned in each point. |
| Calculate | Work it out and show the steps. | Formula, working, answer with unit. |
The question is: Explain how a firewall protects a computer. [2] Three learners answered. A: “It protects it.” B: “A firewall checks all the data coming into and going out of the computer. It blocks traffic that is not allowed, such as a hacker trying to connect.” C: “firewall, security, blocks”. Give each answer a mark out of 2 and say why.
Answer each question with the right amount of detail.
(a) Give one example of an online storage service. [1]
(b) Explain why you should log out of a shared computer. [2]
(c) Motivate whether a school should buy SSDs or HDDs for its computer lab. [3]
A taxi carries 15 passengers. The number of people waiting is typed in edtWaiting. The program must show how many full taxis can leave and how many people are left over. Draw a small table with three headings: Input, Process, Output, and fill it in. Do not write Delphi yet.
Using your plan, write the Delphi code. Declare your variables.
Each photo on Thandi's phone is about 300 KB. She has 45 photos. How many MB do they take up in total? Give the answer correct to 2 decimal places and show your working.
No notes, no computer. Show all your working. Write Delphi code neatly and use correct indentation.
Marks.txt contains four lines of text: 62, 48, 75 and 50. Complete the table for every check of the loop. The last row is the check that ends the loop.
iPass := 0;
AssignFile(tFile, 'Marks.txt');
Reset(tFile);
while not EOF(tFile) do
begin
ReadLn(tFile, sLine);
iMark := StrToInt(sLine);
if iMark >= 50 then
Inc(iPass);
end;
CloseFile(tFile);| Check | EOF(tFile) | iMark | iPass |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 |
What does lblOut show? Describe in one sentence what the code does.
sIn := 'HAL'; sOut := ''; for i := 1 to Length(sIn) do sOut := sOut + Chr(Ord(sIn[i]) + 1); lblOut.Caption := sOut;
List the lines that are in Count.txt after this code has run.
AssignFile(tFile, 'Count.txt'); Rewrite(tFile); for i := 1 to 3 do WriteLn(tFile, IntToStr(i * 10)); CloseFile(tFile); AssignFile(tFile, 'Count.txt'); Append(tFile); WriteLn(tFile, 'Done'); CloseFile(tFile);
This code should create a brand-new file Scores.txt that contains one line: the name and score typed in edtName and edtScore. Find and correct three errors.
AssignFile(tFile, 'Scores.txt');
Append(tFile);
WriteLn(edtName.Text + ' ' + edtScore.Text);
CloseFile('Scores.txt');This code should count the multiples of 3 from 1 to 20 and display the count. Find and correct three errors.
iNum := 1;
while iNum <= 20 do
begin
if iNum mod 3 = 0 then
Inc(iCount);
end;
lblOut.Caption := iCount;Temps.txt holds the maximum temperature of each day in whole degrees, one value on each line (all above 0). Write code that reads the file and displays in lblAvg the average temperature and in lblMax the highest temperature. (Variables tFile, sLine, iTemp, iTotal, iCount, iMax and rAverage are declared.)
Write code that creates a new file Evens.txt and writes in it all the even numbers from 2 to 20, one number on each line.
A sentence is stored in sSentence. Build a new string in which every vowel (A, E, I, O, U, in capitals or small letters) is replaced by a star (*). All other characters stay the same. Display the new string in lblOut.
Show your working in the margin. Convert as asked.
| Convert | Answer |
|---|---|
| 00110101 (binary) to decimal | |
| 78 (decimal) to 8-bit binary | |
| 9B (hex) to decimal | |
| 90 (decimal) to hex | |
| 11100010 (binary) to hex | |
| The ASCII code of the letter M, if A = 65 |
Circle whether each device is an input device, an output device or a storage device.
| 1 | Barcode scanner | Input Output Storage |
| 2 | Projector | Input Output Storage |
| 3 | SSD | Input Output Storage |
| 4 | Speakers | Input Output Storage |
Circle the type of network.
| 1 | A smartphone connected to a Bluetooth speaker | PAN LAN WAN |
| 2 | The computers in a school computer lab | PAN LAN WAN |
| 3 | The internet | PAN LAN WAN |
Explain the difference between a firewall and antivirus software.
Thandi uses a phone at home and a computer at school. Explain how online storage and synchronisation help her to carry on working on the same project on both devices.
Discuss two risks of using social media and how a learner can protect themselves from each.