Unit 19 · Programming · 32 min read
Fundamentals of Computer Programming
What the syllabus asks for 5
- Programming languages: low-level (machine and assembly) and high-level languages
- Language processors: compiler, interpreter and assembler
- Syntax errors, logical errors and run-time errors; testing and debugging
- Common high-level languages: QBASIC, Python, C and Java
- Constants, variables and data types
A computer's processor understands only one thing: patterns of 0s and 1s. People, on the other hand, think in words and numbers. A programming language is the bridge between the two. This lesson explains the kinds of programming language, the translator programs that turn what we write into what the machine runs, the three kinds of mistake every programmer makes and how to find them, four languages you will meet in school and beyond, and the basic building blocks of every program: constants, variables and data types. Lesson 20 then puts all of it to work in QBASIC.
Expect definitions (programming language, machine language, assembly language, high-level language, compiler, interpreter, assembler, source code, object code, bug, debugging, constant, variable), two-point differences (low-level and high-level language, compiler and interpreter, syntax and logical error, constant and variable), lists (rules for naming variables, QBASIC data types and their symbols) and a 4-mark question that explains the three levels of language or the three types of error with examples. A favourite short question gives a few variable names and asks which are valid.
19.1 Programming languages: low-level (machine and assembly) and high-level languages
A programming language is a set of words, symbols and rules (its syntax) used to write instructions that a computer can carry out. A program written in a programming language is called code, and writing it is called coding.
Programming languages are divided into two groups according to how close they are to the machine.
Low-level languages
A low-level language is close to the hardware: each instruction does one tiny job inside the processor, and a program written for one type of processor does not work on another. There are two low-level languages.
Machine language (first generation, 1GL) is written entirely in binary — 0s and 1s. It is the only language the processor understands directly, so it needs no translation and runs fastest. But it is almost impossible for people to read, very easy to get wrong, and different for every family of processors (it is machine-dependent). The first computers were programmed this way, by setting switches or punching holes in cards.
Assembly language (second generation, 2GL) replaces each binary instruction with a short word called a mnemonic: MOV (move), ADD, SUB (subtract), JMP (jump). It is easier to read than binary, but it is still machine-dependent and needs one instruction for each tiny step. A program called an assembler translates it into machine language. Assembly language is still used where speed and size matter most, as in parts of device drivers and the tiny chips inside washing machines and remote controls.
High-level languages
A high-level language (third generation, 3GL, and later) uses English words and ordinary mathematical signs, such as PRINT, IF, INPUT and C = A + B. One high-level statement may stand for many machine instructions. High-level programs are easy to read, write and correct, and the same program works on many kinds of computer (it is portable, or machine-independent). They must be translated into machine language by a compiler or an interpreter before they run. QBASIC, Python, C, C++ and Java are high-level languages. Some books also name fourth-generation languages (4GL) such as SQL, used to ask a database for information, and fifth-generation languages (5GL) such as Prolog, used in artificial intelligence.
| Point | Low-level language | High-level language |
|---|---|---|
| Form | binary (machine) or mnemonics (assembly) | English-like words and mathematical signs |
| Closeness | close to the machine | close to the programmer |
| Ease | hard to write, read and correct | easy to write, read and correct |
| Portability | machine-dependent | machine-independent (portable) |
| Translation | none (machine) or an assembler | a compiler or an interpreter |
| Speed of the program | very fast, small | a little slower and larger |
| Examples | machine code, assembly language | QBASIC, Python, C, Java |

Problem. For each piece of code, name the level of language and the translator (if any) needed before it can run: (a) 10110000 00000101 (b) ADD AL, 3 (c) PRINT "Namaste" (QBASIC) (d) print("Namaste") (Python) (e) printf("Namaste"); (C).
Solution.
| Code | Level | Translator needed |
|---|---|---|
| (a) | machine language (low-level) | none |
| (b) | assembly language (low-level) | assembler |
| (c) | high-level (QBASIC) | interpreter (QBASIC) |
| (d) | high-level (Python) | interpreter (Python) |
| (e) | high-level (C) | compiler (for example GCC) |
19.2 Language processors: compiler, interpreter and assembler
A language processor, or translator, is a program that converts a program written in assembly or a high-level language into machine language. The program the programmer writes is the source code (source program); the machine-language result is the object code (object program). There are three kinds: the compiler, the interpreter and the assembler.
Compiler. A compiler reads the whole source program, checks all of it, and translates it at once into an object program — on Windows, usually an .exe file. If there are errors, it lists them all after it has finished reading, and no object program is made until they are corrected. Once compiled, the program runs on its own, without the compiler, as often as you like, and it runs fast. C, C++ and FORTRAN programs are compiled; GCC (the GNU Compiler Collection) and the compiler in Microsoft Visual Studio are examples.
Interpreter. An interpreter takes the source program one line (statement) at a time: it translates that line, carries it out at once, and only then moves to the next line. It stops at the first error it meets and reports it, which makes errors easy to find and fix — ideal for learning. But no separate object program is made, so the interpreter is needed every time the program runs, and the program runs more slowly. QBASIC and Python are run by interpreters.
Assembler. An assembler translates a program in assembly language into machine language, one mnemonic at a time into the matching binary instruction.
| Point | Compiler | Interpreter |
|---|---|---|
| How it translates | the whole program at once | one line at a time, running each line straight away |
| Errors | listed together after the whole program is checked | shown one at a time; stops at the first error |
| Object program | made and saved (for example an .exe file) |
not made |
| Needed to run the program again | no | yes, every time |
| Speed of running | fast | slower |
| Finding mistakes | harder for a beginner | easier, so good for learning |
| Examples | C, C++, FORTRAN compilers; GCC | QBASIC, Python |

"Python is an interpreter" and "C is a compiler" are common slips, and one textbook even lists COBOL among compilers. Python, C and COBOL are languages. The Python interpreter is the program that runs Python code; GCC is a program that compiles C code. Write "QBASIC programs are run by an interpreter", not "QBASIC is a compiler".
Problem. Which translator suits each situation, and why? (a) A grade 8 student is learning to program and wants to see the effect of each line at once. (b) A software company in Lalitpur wants to sell a fast billing program without giving customers its source code. (c) A teacher wants to test a short program again and again after each small change.
Solution. (a) Interpreter — it runs one line at a time and stops at the first error, so the student sees exactly where things go wrong. (b) Compiler — it makes a fast object program (.exe) that runs without the compiler, so only the object program need be given to customers. (c) Interpreter — there is no separate compiling step, so each change can be run at once.
19.3 Syntax errors, logical errors and run-time errors; testing and debugging
An error in a program is called a bug. The word was used by engineers long before computers, but it became famous on 9 September 1947, when operators of the Harvard Mark II computer found a moth stuck in one of its relays, taped it into their log book and wrote "First actual case of bug being found."

Program errors are of three kinds.
A syntax error is a mistake in the grammar of the language: a misspelt keyword, a missing bracket or quotation mark, a wrong symbol. The translator cannot understand the line, so it reports the error and the program does not run. Example: PRIMT "Average ="; AVG in QBASIC (PRINT misspelt), or print("Namaste" in Python (closing bracket missing).
A run-time error (execution error) happens while the program is running: the program is grammatically correct, but it is asked to do something impossible, and it stops with an error message. Examples: dividing by zero (QBASIC shows "Division by zero"), finding the square root of a negative number ("Illegal function call"), storing a number too big for its variable ("Overflow"), or opening a file that does not exist.
A logical error is a mistake in the method of the program. The program runs to the end without any message, but its output is wrong: a wrong formula, a wrong operator, a wrong condition such as > where >= was meant, or steps in the wrong order. The computer cannot detect a logical error; only testing with known answers can.
| Point | Syntax error | Run-time error | Logical error |
|---|---|---|---|
| What it is | breaking the rules of the language | an impossible operation while running | a mistake in the method |
| When it is found | when the program is translated | while the program runs | when the output is checked |
| Who finds it | the compiler or interpreter | the computer, which stops the program | the programmer, by testing |
| Message shown | yes | yes | none |
| Example | PRIMT for PRINT |
dividing by zero | AVG = A + B / 2 for the average |
Python gives exactly the same kinds of error. These messages were produced by Python 3.12:
print("Namaste"
SyntaxError: '(' was never closed
total = 500
students = 0
print("Share =", total / students)
ZeroDivisionError: division by zero
Testing and debugging
Testing means running a program with chosen test data — inputs whose correct outputs you have worked out beforehand — and comparing the program's output with them. Good test data includes normal values (a mark of 65), boundary values (0, 100, and exactly the pass mark), and wrong values (a negative mark, or a word where a number is expected), to see whether the program copes.
Debugging means finding where an error is and correcting it. Useful ways are:
- Read the error message: it names the error and usually the line.
- Dry run (trace) the program on paper with a trace table, as in Lesson 18.
- Print the values of variables at different points to see where they first go wrong.
- Step through the program one line at a time and watch the variables. In QBASIC, F8 runs one line at a time and F9 sets a breakpoint, a line where the program pauses.
- Check one part at a time, and test again after every correction.
Problem. This QBASIC program should print the average of the marks in English and Maths. Find the errors, name the type of each, and write the corrected program with its output for marks 60 and 80.
CLS
INPUT "Enter marks in English and Maths: ", E, M
AVG = E + M / 2
PRIMT "Average ="; AVG
END
Solution. Line 4: PRIMT is a misspelling of PRINT — a syntax error; neither FreeBASIC nor QB64 (two modern QBASIC-compatible compilers) will accept the program. Line 3: the computer divides before it adds, so it works out 60 + 80 ÷ 2 = 60 + 40 = 100 instead of the average 70 — a logical error. With only line 4 corrected, the program runs and prints the wrong answer without any warning:
Enter marks in English and Maths: 60, 80
Average = 100
The corrected program puts brackets round the sum:
CLS
INPUT "Enter marks in English and Maths: ", E, M
AVG = (E + M) / 2
PRINT "Average ="; AVG
END
Enter marks in English and Maths: 60, 80
Average = 70
Check: (60 + 80) ÷ 2 = 140 ÷ 2 = 70 ✓. Only the test with known marks revealed the logical error.
In "identify the error" questions, ask yourself two things. Does the program run at all? If not, it is a syntax error. If it runs, does it stop with a message? Then it is a run-time error. If it finishes but the answer is wrong, it is a logical error. Always write the corrected line too: the correction usually carries a mark of its own.
19.4 Common high-level languages: QBASIC, Python, C and Java
Hundreds of high-level languages exist. The first to be widely used was FORTRAN (FORmula TRANslation), released by John Backus and his team at IBM in 1957 for science and engineering; COBOL (COmmon Business-Oriented Language), from 1959, ran the accounts of banks and governments for decades. Four languages matter most to you.
QBASIC (Quick Beginner's All-purpose Symbolic Instruction Code). The first BASIC was created by John G. Kemeny and Thomas E. Kurtz at Dartmouth College in the USA; its first program ran on 1 May 1964. Microsoft's QBASIC came with MS-DOS 5.0 in 1991. It is run by an interpreter inside its own simple editor, its keywords are plain English (PRINT, INPUT, IF, FOR), and it needs no setting up, so it is still a favourite first language in Nepali schools. Modern free programs such as QB64 and FreeBASIC run QBASIC programs on today's computers.
Python. Created by Guido van Rossum in the Netherlands and first released in February 1991. Python code is short and very readable, it uses indentation instead of END words, and it comes with a huge collection of ready-made libraries. It is used for artificial intelligence, data science, websites and automation, and it is the programming language of the CDC grade 9 and 10 course.
C. Developed by Dennis Ritchie at Bell Laboratories in the USA in 1972, to write the UNIX operating system. C programs are compiled; they are fast and work closely with the hardware, so C is used for operating systems (much of Linux and Windows), device drivers and the small computers inside machines. C++, an extension of C made by Bjarne Stroustrup, came in 1985.
Java. Developed by James Gosling and his team at Sun Microsystems and released in 1995. A Java compiler turns the program into bytecode, which the Java Virtual Machine (JVM) then runs on any computer — "write once, run anywhere". Java is widely used for Android apps, banking systems and large business software. (JavaScript, the language of web pages, is a different language with a similar name.)
| Language | Released | Created by | How it is translated | Typical use |
|---|---|---|---|---|
| QBASIC | 1991 (BASIC 1964) | Microsoft (BASIC: Kemeny and Kurtz) | interpreter | learning to program |
| Python | 1991 | Guido van Rossum | interpreter | AI, data, web, school courses |
| C | 1972 | Dennis Ritchie, Bell Labs | compiler | operating systems, drivers, devices |
| Java | 1995 | James Gosling, Sun Microsystems | compiler to bytecode, run by the JVM | Android apps, banking, business software |
Problem. Write a program in QBASIC, Python, C and Java that stores 25 and 17 in two variables and prints their sum.
Solution. Each program was run, and each printed the same line, Sum = 42.
A = 25
B = 17
PRINT "Sum ="; A + B
END
a = 25
b = 17
print("Sum =", a + b)
#include <stdio.h>
int main(void)
{
int a = 25, b = 17;
printf("Sum = %d\n", a + b);
return 0;
}
public class Sum {
public static void main(String[] args) {
int a = 25, b = 17;
System.out.println("Sum = " + (a + b));
}
}
Sum = 42
The QBASIC and Python versions are the shortest. C and Java need more lines because the program must be placed inside a main function and each variable's type (int) must be declared first.
19.5 Constants, variables and data types
Every program works with data: numbers such as marks, prices and ages, and text such as names and addresses. A program stores data in two ways, as constants and as variables.
A constant is a value that does not change while the program runs. A numeric constant is a number, such as 45, -7.5 or 3.2E+4 (which means 3.2 × 10⁴ = 32,000). A string constant is any group of characters inside double quotation marks, such as "Kathmandu", "9841234567" or "2081". A constant can also be given a name: in QBASIC, CONST PI = 3.14159 makes PI a symbolic constant.
A variable is a name given to a location in the computer's memory, where a value is stored. The value can change while the program runs, but the variable holds only one value at a time: storing a new value replaces the old one. In QBASIC, LET A = 5 (or simply A = 5) stores 5 in the variable A.
| Constant | Variable |
|---|---|
| a fixed value | a named memory location whose value can change |
| its value never changes while the program runs | its value can be changed by assignment or input |
examples: 25, 3.14, "Nepal" |
examples: A, AGE, N$ |
In mathematics A = A + 1 is impossible. In a program, = means "work out the right-hand side, then store it in the variable on the left". So A = A + 1 adds 1 to the old value of A — the update step of every counter in Lesson 18. For the same reason 5 = A is wrong: the left side must always be a variable.
Rules for naming variables in QBASIC
- A name must begin with a letter (A to Z).
- It may contain letters, digits and the full stop (period), up to 40 characters.
- No spaces and no other symbols are allowed, except one type symbol (
%,&,!,#or$) at the end. - A keyword (reserved word) such as
PRINT,INPUTorENDcannot be a variable name. - Capital and small letters are the same in QBASIC:
age,AgeandAGEare one variable. (In Python they are three different variables.)
Choose meaningful names — TOTAL, PRICE, NAME$ — rather than single letters, so that the program explains itself. Python's rules are similar: a name starts with a letter or underscore, may contain letters, digits and underscores, cannot be a keyword, and is case-sensitive.
Problem. Which of these are valid QBASIC variable names? Give a reason for each invalid one: AGE, 2NDNAME, FIRST NAME, N$, PRINT, TOTAL.MARKS, ROLL-NO, PRICE%.
Solution. Valid: AGE, N$ (a string variable), TOTAL.MARKS (a full stop is allowed), PRICE% (an integer variable). Invalid: 2NDNAME begins with a digit; FIRST NAME contains a space; PRINT is a keyword; ROLL-NO contains a hyphen, which QBASIC reads as a minus sign.
Data types
The data type of a value tells the computer what kind of data it is, how much memory to give it and what can be done with it. The two basic kinds are numeric data, used in calculations, and string (character) data, which is text: letters, digits and symbols treated as characters, not as amounts. In QBASIC the type of a variable is shown by a symbol at the end of its name; a name with no symbol is SINGLE.
| QBASIC type | Symbol | Memory | Values it can hold | Example |
|---|---|---|---|---|
| INTEGER | % |
2 bytes | whole numbers from −32,768 to 32,767 | AGE% = 14 |
| LONG (long integer) | & |
4 bytes | whole numbers from −2,147,483,648 to 2,147,483,647 | POP& = 29164578 |
| SINGLE (default) | ! or none |
4 bytes | numbers with decimals, about 7 significant digits | HEIGHT! = 1.52 |
| DOUBLE | # |
8 bytes | numbers with decimals, about 15 to 16 significant digits | PI# = 3.14159265358979 |
| STRING | $ |
1 byte per character | text of up to 32,767 characters | N$ = "Sita" |
Problem. Write a QBASIC program that stores a student's name, age and height, and the population of Nepal from the 2021 census (29,164,578), in variables of suitable types, and prints them.
Solution.
CLS
REM Variables of different data types
N$ = "Sita Thapa"
AGE% = 14
HEIGHT! = 1.52
POP& = 29164578
PRINT N$; " is"; AGE%; "years old and"; HEIGHT!; "m tall."
PRINT "Population of Nepal, census 2021:"; POP&
END
Sita Thapa is 14 years old and 1.52 m tall.
Population of Nepal, census 2021: 29164578
The population needs LONG, because it is far larger than 32,767, the biggest INTEGER. Notice the spaces in the output: QBASIC prints every number with a space in front (where a minus sign would go) and a space after it, so " is"; AGE%; "years" comes out as "is 14 years".
Python works out the type from the value itself, and type() shows it. It has a fourth basic type, bool, for True and False.
name = "Sita Thapa"
age = 14
height = 1.52
population = 29164578
is_student = True
print(name, "is", age, "years old and", height, "m tall.")
print(type(name), type(age), type(height), type(is_student))
Sita Thapa is 14 years old and 1.52 m tall.
<class 'str'> <class 'int'> <class 'float'> <class 'bool'>
Problem. Choose a QBASIC data type for each: (a) the number of students in a class; (b) the price of 1 kg of rice, Rs 85.50; (c) the name of a district; (d) a mobile number, 9841234567; (e) the number of people in Kathmandu district.
Solution. (a) INTEGER (%) — a small whole number. (b) SINGLE (!) — it has decimals. (c) STRING ($) — text. (d) STRING ($) — no arithmetic is ever done with a phone number, and as a number it is too big even for LONG (whose largest value is 2,147,483,647). (e) LONG (&) — a whole number of about two million, too big for INTEGER.
In the computer lab, open QB64 (or QBASIC, if your lab has it) and Python's IDLE. (1) Type PRINT 7 * 6 in QB64's editor and print(7 * 6) at IDLE's >>> prompt, and run both. (2) Misspell the keyword in each and note the exact error message. (3) Make each program divide by zero and note the message. (4) Make a three-column table — language, translator, message — for your notebook, and label each message as a syntax or run-time error.
Chapter summary
- A programming language is a set of words, symbols and rules (syntax) for writing instructions for a computer.
- Low-level languages: machine language (binary; runs directly; machine-dependent) and assembly language (mnemonics such as MOV and ADD; needs an assembler). High-level languages (QBASIC, Python, C, Java) are English-like and portable.
- Language processors turn source code into object code: a compiler translates the whole program at once and makes an object program; an interpreter translates and runs one line at a time; an assembler translates assembly language.
- Syntax error: breaking the rules of the language. Run-time error: an impossible operation while running, such as division by zero. Logical error: wrong method, wrong output, no message.
- Testing runs a program with test data of known answers; debugging finds and corrects errors (error messages, dry runs, printing values, stepping with F8, breakpoints with F9).
- QBASIC (Microsoft, 1991; BASIC 1964), Python (Guido van Rossum, 1991), C (Dennis Ritchie, 1972), Java (James Gosling, Sun, 1995).
- A constant never changes (numeric or string); a variable is a named memory location whose value can change; QBASIC names begin with a letter, have no spaces and are not keywords.
- QBASIC data types: INTEGER
%, LONG&, SINGLE!(the default), DOUBLE#, STRING$.
Practice questions
Group A — Objective (1 mark each)
- Machine language is written using [1]aEnglish wordsbmnemonicsc0s and 1sdflowchart symbols
- The translator that converts assembly language into machine language is [1]aa compilerban interpretercan assemblerdan editor
- Which translator converts the whole program at once and makes an object program? [1]ainterpreterbcompilercassemblerddebugger
- QBASIC programs are run by [1]aan assemblerban interpretercan operating systemda modem
- Which of these is a low-level language? [1]aPythonbJavacassembly languagedQBASIC
- Dividing a number by zero while a program is running causes [1]aa syntax errorba logical errorca run-time errordno error
- A program runs to the end but prints a wrong answer because of a wrong formula. This is [1]aa syntax errorba logical errorca run-time errorda hardware fault
- Python was created by [1]aDennis RitchiebJames GoslingcGuido van RossumdJohn Kemeny
- In QBASIC, the symbol that marks a string variable is [1]a
%b&c!d$ - Which is a valid QBASIC variable name? [1]a1STNAMEbFIRST NAMEcTOTAL.MARKSdPRINT
- The range of an INTEGER variable in QBASIC is [1]a0 to 255b−32,768 to 32,767c−2,147,483,648 to 2,147,483,647dunlimited
- A program written by a programmer in a high-level language is called [1]aobject codebsource codecmachine codedbytecode
Answers to Group A
1. (c) — machine language is binary.
2. (c) — an assembler translates mnemonics into machine code.
3. (b) — a compiler translates everything at once.
4. (b) — QBASIC is an interpreter, running one line at a time.
5. (c) — assembly language is close to the machine; the others are high-level.
6. (c) — the error appears only while the program runs.
7. (b) — the program runs, but the method is wrong.
8. (c) — Guido van Rossum, 1991.
9. (d) — $ marks a string, as in N$.
10. (c) — a full stop is allowed; the others start with a digit, contain a space or are a keyword.
11. (b) — INTEGER uses 2 bytes.
12. (b) — the translator turns source code into object code.
Group B — Short answer (2 marks each)
- Write any two differences between a low-level and a high-level language. [2]
- What is a language processor? Name its three types. [2]
- Write any two differences between a compiler and an interpreter. [2]
- What is assembly language? Why does it need an assembler? [2]
- Define syntax error and logical error with one example each. [2]
- What is a run-time error? Give two examples. [2]
- What is debugging? Write any two ways of debugging a program. [2]
- Define constant and variable with one example of each. [2]
- Write any four rules for naming a variable in QBASIC. [2]
- Name the five data types of QBASIC with their type symbols. [2]
Answers to Group B
1. A low-level language (machine or assembly) is close to the hardware and machine-dependent, while a high-level language uses English-like words and is portable. Low-level programs are hard to write and correct but run very fast; high-level programs are easy to write and correct but must be translated by a compiler or interpreter.
2. A language processor (translator) is a program that converts a program written in assembly or a high-level language into machine language. Its three types are the compiler, the interpreter and the assembler.
3. A compiler translates the whole program at once, while an interpreter translates and runs one line at a time. A compiler makes an object program that runs without it, while an interpreter makes no object program and is needed every time. (Also: a compiler lists all errors after checking the whole program; an interpreter stops at the first error.)
4. Assembly language is a low-level language that uses short words called mnemonics (such as MOV, ADD, SUB) in place of binary instructions. The processor understands only machine language, so an assembler must translate the mnemonics into binary before the program can run.
5. A syntax error breaks the grammar rules of the language, for example PRIMT "Hello" instead of PRINT "Hello". A logical error is a mistake in the method, so the program runs but gives a wrong result, for example AVG = A + B / 2 instead of AVG = (A + B) / 2.
6. A run-time error is an error that occurs while the program is running, when it is asked to do something impossible, and it stops the program with a message. Examples: dividing a number by zero; the square root of a negative number; storing a number too large for its variable (overflow).
7. Debugging is the process of finding and correcting the errors (bugs) in a program. Any two ways: reading the error messages; dry running the program with a trace table; printing the values of variables at different points; stepping through it line by line (F8 in QBASIC) or using breakpoints (F9).
8. A constant is a value that does not change while the program runs, for example 25 or "Nepal". A variable is a named memory location whose value can change while the program runs, for example AGE in AGE = 14.
9. Any four: it must begin with a letter; it may contain letters, digits and a full stop; it may be up to 40 characters long; it must not contain spaces or other symbols except a type symbol at the end; it must not be a keyword such as PRINT or END.
10. INTEGER (%), LONG (&), SINGLE (!, the default when no symbol is used), DOUBLE (#) and STRING ($).
Group C — Long answer (4 marks each)
- What is a programming language? Explain machine language, assembly language and high-level language, giving one advantage and one disadvantage of each. [1+3]
- Explain compiler, interpreter and assembler. Which translator is used for QBASIC programs and which for C programs? [3+1]
- What are testing and debugging? Explain the three types of programming error with one example each. [1+3]
- Write short notes on any two of these languages: QBASIC, Python, C, Java. [2+2]
- Study the program below. Find the errors, name the type of each, and write the correct program and its output when the length is 12 and the breadth is 5. [4]
CLS
INPUT "Enter length and breadth: ", L, B
A = L + B
PRINT "Area ="; A
P = 2 * L + B
PRNT "Perimeter ="; P
END
Answers to Group C
1. A programming language is a set of words, symbols and rules used to write instructions for a computer (1 mark). Machine language: written in 0s and 1s; advantage — runs directly and fastest, needs no translation; disadvantage — very hard to write and correct, and machine-dependent. Assembly language: uses mnemonics such as MOV and ADD; advantage — easier than binary while still fast; disadvantage — machine-dependent and needs an assembler. High-level language: English-like statements, such as QBASIC and Python; advantage — easy to write, read and correct, and portable; disadvantage — must be translated by a compiler or interpreter and runs a little slower.
2. A compiler translates the whole high-level program at once into an object program (such as an .exe file), lists all errors after checking, and the object program then runs fast without the compiler. An interpreter translates and carries out one line at a time, stops at the first error, makes no object program and is needed every time the program runs. An assembler translates assembly-language mnemonics into machine code (3 marks). QBASIC programs are run by an interpreter; C programs are translated by a compiler such as GCC (1 mark).
3. Testing is running a program with test data whose correct results are known and comparing the outputs; debugging is finding and correcting the errors (1 mark). Syntax error: breaking the rules of the language, found by the translator, e.g. PRIMT for PRINT. Run-time error: an impossible operation while the program runs, which stops it with a message, e.g. dividing by zero. Logical error: a wrong method; the program runs but gives wrong output and no message, e.g. AVG = A + B / 2 (3 marks).
4. Any two, about four points each. QBASIC: Microsoft, 1991, based on BASIC (Kemeny and Kurtz, Dartmouth College, 1964); run by an interpreter in its own editor; English-like keywords; used for learning. Python: Guido van Rossum, 1991; very readable, uses indentation; interpreted; large libraries; used in AI, data science and the web, and in the CDC grade 9–10 course. C: Dennis Ritchie, Bell Labs, 1972; compiled, fast, close to the hardware; used for operating systems and device programs. Java: James Gosling, Sun Microsystems, 1995; compiled to bytecode run by the Java Virtual Machine, so programs run on any computer; used for Android apps and business software.
5. Line 3 A = L + B is a logical error — area is length × breadth. Line 5 P = 2 * L + B is a logical error — perimeter is 2 × (length + breadth), so brackets are needed. Line 6 PRNT is a syntax error — the keyword is PRINT. Corrected program:
CLS
INPUT "Enter length and breadth: ", L, B
A = L * B
PRINT "Area ="; A
P = 2 * (L + B)
PRINT "Perimeter ="; P
END
Enter length and breadth: 12, 5
Area = 60
Perimeter = 34
Check: 12 × 5 = 60 and 2 × (12 + 5) = 34 ✓.
Practise more: Computer Science question bank