admin@kcseforecast.com

Q&A- Elementary Programming Principles

Give a reason why HMTL is not regarded as a programming language

  •  It does not have declaration part
  • It does not  have control structures

Differentiate between object code and source code

  • Object code refers to a program in machine language while source code to a program in a high level language that must be translated to object code for it to be machine readable

Define programming

The process of developing computer programs to solve a particular problem

State and briefly describe three program control structures

Sequence:Sequential execution of a program one line after another

Selection: Involve the a decision between two or more options e.g  if...then...else

Loops/Repetitions:use for looping i.e a line of code can be made to execute a given number times before terminating.

List three types of errors that are likely to exist in a program                             (3 marks)

Syntax errors-Incorrect use of programming language

Logical errors- The program do not give the expected output

Runtime errors- Premature end of a program

Computer programs require translation to execute. (a) Compilers and interpreters translate high-level programming languages into machine code. Describe the main differences between a compiler and an interpreter.

  • A compiler translates the whole program in one go whereas An interpreter translates each line of code (often an intermediate code) at run time
  • A compiler produces an executable file that will run on the target hardware machine without the compiler being installed  A run time interpreter will be required at run time
  • Compilers tend to be large complex programs Interpreters are smaller simpler programs
  • Interpreted programs can be amended and run without translating whole program Compiled programs have to be re-compiled after a change
  • Compilers compile programs that will usually only run on the target platform (hardware/operating system)  Interpreters will interpret same program (or intermediate code) on different (some virtual) platforms

One facility of a Software Development Environment is to convert source code to machine code.  Name and briefly describe four other facilities commonly found in a Software Development Environment.

  • Editor: this allows a programmer to enter and edit source code
  • Automatic formatting:  Correctly indents code
  • Automatic colour coding:  Changes key words, literals and annotation to different colours
  • Linker: this is a program which allows previously compiled code, from software libraries, to be linked together
  • Loader: this is a program which loads previously compiled code into memory.
  • Debugger: this is a program which helps locate, identify and rectify errors in a program
  • Syntax error detection:  Highlighting syntax errors before code is translated
  • Trace: this is a facility which displays the order in which the lines of a program are executed, and possibly the values of variables as the program is being run
  • Break point: this is a facility which interrupts a program on a specific line of code, allowing the programmer to compare the values of variables against expected values.  The program code can then usually be executed one line at a time.  This is called single-stepping
  • Variable watch: this is a facility which displays the current value of any variable.  The value can be 'watched' as the program code is single-stepped to see the effects of the code on the variable.  Alternatively a variable watch may be set, which will interrupt the program flow if the watched variable reaches a specified value
  • Memory inspector: this is a facility which will display the contents of a section of memory
  • Error diagnostics: these are used when a program fails to compile or to run.  Error messages are displayed to help the programmer diagnose what has gone wrong
  • Emulator: will provide and emulator to run the code/app so no physical device required
  • Context sensitive menu:  SDE suggests available options
  • Statement completion:  SDE will complete a statement such as adding an ‘end if’ to an ‘if’ statement GUI creation:  Allows programmer to create a GUI by dragging and dropping controls (buttons, etc...) onto a form.
  • Publisher: facility to package up and deploy program as an easy to install package

6 Programmers write software that controls hardware and interacts with users. Some of this software ensures that networks function properly.

(a) Compilers and interpreters translate source code written in programming languages.
  State four other features of a compiler.

 A single executable file is produced 

The executable program is portable between machines

The program source code is not available

Code can be optimised/made more efficient 

Entire source code file is converted at once

No special environment is needed to execute the code 

It creates object/machine code

Error report is provided at the end of the compilation

There are many different types of errors that can occur when developing computer programs. (a) State the name of the two different types of programming error described below.

(i) Unexpectedly halts the program. [1]  ..............................................................................................

(ii) The program produces the wrong output. [1]  ..............................................................................................
 (b) Another error can result from incorrectly using the rules or grammar of the programming language.

(i) Name this error.  .............................................................................................. [1]

(ii) State when this error is detected.

 

(i) Unexpectedly halts the program. Run time (execution)

(ii) The program produces the wrong output.  Logical
 (b) Another error can result from incorrectly using the rules or grammar of the programming language.

(i) Name this error. Syntax

(ii) State when this error is detected.

Syntax error is detected during translation into executable code / attempted to run but fails

Compare the use of a compiler with the use of an interpreter to translate code

Both translate source code written in high level programming languages into machine code.
Compiler: Produces a single executable file that is portable between machines
Interpreter: Source code needs a special environment to run
Compiler: Needs to be compiled for a particular architecture
Interpreter: Can run on any architecture that has the translator/interpreter
Complier: Entire source code file is translated at once
Interpreter: Code is translated line by line
Compiler: Provides error report at the end of the compilation
Interpreter: Errors more obvious in the sequence
Compiler: The program source code is not available (so helps protect IP)
Interpreter: Source code is available
Compiler: Only has to be translated once (affecting the speed of execution)
Interpreter: Has to be translated each time, therefore (affecting the speed of execution)
Compiler: Requires two files to be maintained (one for execution and for editing purposes)
Interpreter: Requires only one file to be maintained
 

Explain why a developer, who is good at both low-level and high-level programming, would normally use high-level languages when writing programs.

  • High-level languages have built-in functions;
  • High-level languages have built-in libraries;
  • High-level languages have more support/help;
  • High-level languages have structures (such as selection and iteration);
  • High-level languages can be less machine-dependent/more portable;
  • It (usually) requires fewer lines of code to be written;
  • It is (usually) quicker to develop code in high-level languages;
  • It is easier to find mistakes in code;
  • The code is easier to maintain//understand;
  • It is easier to structure code in high-level languages;

Describe each of the following types of program errors, using an example.

Syntax error

Logical error

Run-time error

Syntax error

  • An error in the source code of a program like pront("Hello, " + name) instead of print
  • These are errors that emanate from improper use of language rules such as spelling mistakes,improper use of variables and misuse of reserved words

Logical error

  • A mistake in a program's source code that results in incorrect or unexpected behaviour like if age == 65: print("Eligible for a discount.") instead of using if age >= 65: print("Eligible for a discount.")
  • These are errors that occur due to imperfect comparisons

Run-time error

  • An error which occurs when a program is asked to do something that it cannot, resulting in a ' crash'9. For example, asking a number to be divided by 0 such as 72/0.
  • An error that results in a sudden halt of an executing program

 

Describe and give an example of one syntax and one logical programming error.

Syntax
An error that occurs when a command does not follow the expected syntax of the language, e.g. when a keyword is incorrectly spelt
Incorrect: IF A ADN B Then
Correct: IF A AND B Then

Logical
An error that causes a program to output an incorrect answer (that does not necessarily crash the program)
An algorithm that calculates a person’s age from their date of birth, but ends up giving negative numbers

Describe the role of the one-dimensional array in programming

  • holds an indexed sequence of variables of the same data type
  • Arrays can be used to store values of the same data type and the array is then used as a singular variable to increase efficiency

Identify two advantages to a programmer of using a high-level language instead of a low-level language.

  • it is easier / quicker to read/write/understand
  • it is easier / quicker to debug
  • code is portable.

 

Suggest one disadvantage to the programmer of using a high-level language instead of a low-level language.

  • not able to directly manipulate the hardware
  • may need to wait for translation before running
  • the program may be less efficient.

 

Describe each of the following types of a program errors, giving an example in each case.

(i) Syntax error.
(ii) Logic error.
(iii) Arithmetic error/runtime error

(i) Incorrect use of language, e.g. PLINT instead of PRINT
(ii) A mistake in the structure of the solution, e.g. a jump goes to the wrong line
(iii) Inappropriate arithmetic is used, e.g. division by 0 is attempted

Explain what is meant by a LIFO data structure.

  • Data enters at one end (of a stack)
  • Leaves at the same end
  • Hence 'last in, first out'

Describe the purpose of the
(i) control unit
(ii) memory unit
(iii) arithmetic logic unit

in a computer.

i) To manage the execution of instructions
By running a clock
To decode instructions
(ii) To store OS
To store those parts of applications programs currently running
To store data currently in use
(iii) Part of the processor where data is processed/manipulated
All I/O must pass through here

A school has 3000 students sitting final examinations.

Each student sits eight examinations.

Write an algorithm, using pseudocode or a flowchart, which:

inputs the marks for all 8 examinations for each student

outputs for each student the average mark for their 8 examinations

outputs the highest mark overall

highest = -1
for student = 1 to 3000
total = 0
for exam = 1 to 8
input mark
total = total + mark
if mark > highest then highest = mark
next
average = total/8
output average
next
output highest

Python code:

highest = -1
for student in range(1, 3001):
    total = 0
    for exam in range(1, 9):
        mark = int(input("Enter mark: "))
        total += mark
        if mark > highest:
            highest = mark
    average = total / 8
    print("Average:", average)
print("Highest mark:", highest)

VBA Code

Option Explicit

Sub CalculateAverageAndHighest()
    Dim highest As Integer
    Dim total As Integer
    Dim mark As Integer
    Dim average As Double
    
    highest = -1
    
    For student = 1 To 3000
        total = 0
        For exam = 1 To 8
            mark = InputBox("Enter mark:")
            total = total + mark
            If mark > highest Then
                highest = mark
            End If
        Next exam
        average = total / 8
        MsgBox "Average: " & average
    Next student
    
    MsgBox "Highest mark: " & highest
End Sub

 

Explain why a developer, who is good at both low-level and high-level programming, would normally use high-level languages when writing programs                                                                                    [4 marks]

  • High-level languages have built-in functions;
  • High-level languages have built-in libraries;
  •  High-level languages have more support/help;
  • High-level languages have structures (such as selection and iteration);
  • High-level languages can be less machine-dependent/more portable;
  • It (usually) requires fewer lines of code to be written;
  • It is (usually) quicker to develop code in high-level languages;
  • It is easier to find mistakes in code;
  • The code is easier to maintain//understand;
  • • It is easier to structure code in high-level languages;

What is a source code as used in computer programming?

  • It is a program written in a high level langauage

State and explain characteristics of an algorithm

Input : An algorithm can take 0 or more inputs well defined outputs

Finiteness: an algorithm must terminate in finite time and after finite number of steps. 

Definiteness : an algorithm must be clear and unambiguous. Each and every step of an algorithm must be clear and precise and it must have only one meaning

Feasibility: should be feasible with the available resources

 

Output : must produce at least one well defined output

Independent : should have a step-by-step directions which is independent of any programming language

Correctness: should solve the problem it is designed to solve

Efficiency: must take the shortest time possible to run and should require the least memory space. It should use a minimum number of computation steps and memory to produce the desired  output. takes little time and memory to run

Generality: should not be limited to a specific case. It should work for all the valid inputs. works for many possible inputs

Effectiveness : should statements that are relevant to the problem. Every step must be basic and essential.