2.5.1 Languages · Paper 2
Languages and translators
Each section has pictures and things to try, then its key points and key words. Exam practice is at the end of the page.
You need to know
- High-level and low-level languages, and the differences between them
- Why translators are needed
- Compilers and interpreters: differences, benefits and drawbacks
You don't need to know
- Assemblers
High-level and low-level
| High-level (e.g. Python, Java) | Low-level (machine code, assembly) | |
|---|---|---|
| Looks like | English words and maths | Binary, or short codes for CPU instructions |
| Easy to write? | Yes, easier to read and debug | No, hard to read |
| Runs on | Many types of computer | Only one type of CPU |
| Control | Less direct control of hardware | Direct control of hardware and memory, very efficient |
The CPU only understands machine code, so other code must be translated first.
Try it
High-level or low-level?
- print("Hello")
- 10110000 01100001
- LDA 5
- if score > 10:
- Code that controls a printer's hardware directly
- Code that runs on Windows, Mac and Linux once translated
Key points
High-level and low-level languages
- High-level languages (e.g. Python, Java) use English-like words. They are easier to read, write and debug.
- One high-level line can turn into many machine code instructions.
- High-level code can run on many types of computer, once translated.
- Low-level languages are machine code (binary) and assembly language (short codes like ADD and LDA).
- Low-level code works on one type of CPU only. It gives direct control of the hardware, and can be very fast and use little memory.
Why we need translators
Before this: High-level and low-level languages
- The CPU can only run machine code.
- Programs written in any other language must be translated into machine code first.
- A translator is the program that does this.
- The two you need to know are compilers and interpreters.
Key words
Say what each one means, then tap to check.
Compilers and interpreters
| Compiler | Interpreter | |
|---|---|---|
| How | Translates the whole program at once into an executable file | Translates and runs one line at a time |
| Errors | Lists all errors at the end. Nothing runs until all are fixed. | Stops at the first error, so it's easy to find |
| Running | Fast. The file runs without the compiler, and code stays hidden. | Slower. Needs the interpreter every time. |
| Best for | Finished programs to give to users | Writing and testing code |
Try it
Run the same program with each translator. Then fix the errors and try again.
Try it
Practise an exam question. Choose a translator, then two reasons that fit.
- The finished program runs faster
- Users don't get the source code, so it is harder to copy
- Users don't need a translator to run it
- It stops at the first error, so errors are easy to find
- Changes can be tested straight away, without translating the whole program
- A company selling a game to the public.
- A student writing and testing a new program in class.
- A programmer fixing lots of small bugs, one at a time.
- An app that must run as fast as possible on customers' computers.
Key points
Compilers vs interpreters
Before this: Why we need translators
- A compiler translates the whole program at once into an executable file.
- The compiled file runs fast, doesn't need the compiler to run, and keeps the code hidden.
- A compiler lists all the errors at the end, and nothing runs until they're fixed.
- An interpreter translates and runs the code one line at a time.
- It stops at the first error, which makes errors easy to find. But it runs slower and is needed every time the program runs.
Key words
Say what each one means, then tap to check.
Exam practice
10 questions in the style of the OCR exam, 25 marks in total. Try each one, then check the mark scheme.
High-level and low-level
Hint
Think about reading the code, fixing it, and which computers it can run on.
Give means give a short, clear answer. You don't need to explain it.
2 marks: make 2 separate points.
Show mark scheme
- Easier to read / write / understand.
- Easier to debug.
- Code works on many types of computer / portable.
- Quicker to write programs.
1 mark for each, up to 2.
Hint
What can low-level code control directly? Where would that be useful?
Explain means say what happens and why. Words like “because” and “so” help.
2 marks: aim for 2 things said, such as a point and then the reason for it.
Show mark scheme
- Direct control of the hardware / memory…
- …which is needed for things like device drivers.
- Programs can run faster / use less memory…
- …which is useful for small embedded systems with little memory.
1 mark for the reason, 1 mark for explaining it.
Hint
What is the only language the CPU understands?
Explain means say what happens and why. Words like “because” and “so” help.
2 marks: aim for 2 things said, such as a point and then the reason for it.
Show mark scheme
- The CPU / computer only understands machine code (binary)…
- …so the code must be converted before it can run.
1 mark for each point.
Compilers and interpreters
Hint
Think about how much is translated at a time, what file is made, how errors are shown, and what is needed to run it.
Describe means say what it is like or what happens. Make each point a separate fact.
4 marks: make 4 separate points.
Show mark scheme
- A compiler translates the whole program at once; an interpreter translates one line at a time.
- A compiler makes an executable file; an interpreter doesn't.
- A compiler reports all errors at the end; an interpreter stops at the first error.
- Compiled code runs faster / doesn't need the translator to run; interpreted code needs the interpreter every time.
Don't accept: "A compiler is faster" on its own. Say what is faster: the compiled program runs faster.
Example answer
A compiler translates the whole program in one go, whereas an interpreter translates and runs it one line at a time. A compiler makes an executable file that can run without the compiler, whereas an interpreter makes no executable file, so the interpreter is needed every time the program runs.
For each difference: 1 mark for the compiler, 1 mark for the interpreter.
Hint
Think about the people buying the game. Do they have a translator? Should they see the code?
Explain means say what happens and why. Words like “because” and “so” help.
2 marks: aim for 2 things said, such as a point and then the reason for it.
Show mark scheme
- It makes an executable that runs without the translator / runs fast.
- Users can't see / copy the source code.
1 mark for each point.
Hint
What does an interpreter do when it reaches a line with an error?
Explain means say what happens and why. Words like “because” and “so” help.
2 marks: aim for 2 things said, such as a point and then the reason for it.
Show mark scheme
- It stops at the line where the error is…
- …so errors are easy to find and fix / you can test parts quickly.
Don't accept: "It is faster". Interpreted code runs more slowly. The benefit is finding errors.
1 mark for each point.
Mixed questions on the whole page
- One line can become many machine code instructions.
- It only works on one type of CPU.
- It uses English-like words such as print and if.
Hint
Which one is close to the CPU's own instructions? Which one is close to English?
State means give a short, clear answer. You don't need to explain it.
3 marks: make 3 separate points.
Show mark scheme
- a) High-level
- b) Low-level
- c) High-level
1 mark for each.
Hint
Python is a high-level language. What is the only language the CPU can run?
Explain means say what happens and why. Words like “because” and “so” help.
2 marks: aim for 2 things said, such as a point and then the reason for it.
Show mark scheme
- It must be translated into machine code…
- …by a translator (a compiler or an interpreter), because the CPU only understands machine code / binary.
1 mark for each point.
Hint
What is most useful while you are still finding errors? What is most useful when you give the program to other people?
Explain means say what happens and why. Words like “because” and “so” help.
4 marks: aim for 4 things said, such as a point and then the reason for it.
Show mark scheme
- Interpreter: it stops at the line with the error…
- …so errors are easy to find and fix while testing.
- Compiler: it makes an executable file that runs quickly / without the translator…
- …and the source code stays hidden from users.
Don't accept: "An interpreter is faster" on its own. An interpreted program usually runs more slowly.
Don't accept: Only explaining one of the two translators.
Example answer
While the student is testing, an interpreter stops at the line where an error is, so they can find and fix errors straight away and test parts of the program quickly. When the program is finished, a compiler makes an executable file that runs quickly without a translator, and the source code stays hidden from users.
2 marks for the interpreter, 2 marks for the compiler.
Hint
Think about the downside of each one: when you see the errors, how fast it runs, and what users need.
Give means give a short, clear answer. You don't need to explain it.
2 marks: make 2 separate points.
Show mark scheme
- Compiler: errors are only shown after the whole program is translated / it must translate again after every change / the executable only runs on one type of computer.
- Interpreter: the program runs slower / the interpreter is needed every time it runs / users get the source code, so it can be copied.
1 mark for each.