
Syntax and logic errors questions and answers
Q1. What is meant by a ‘reserved word’?
A1. A reserved word is a word that is used by the programming language - you are not allowed to use it as an identifier.
Q2. What type of error might be displayed when you try to compile a program?
A2. Syntax error.
Q3. What is a ‘logical error’?
A3. This is the type of error produced when a program compiles correctly but produces an incorrect answer.
Q4. Give an example of a logical error.
A4. You wrote c = a +b in a program. It compiles correctly but gives the wrong answer – because you meant to write c = a – b.
Q5. What kind of error would you get if you used the keyword WHILE but spelt it as WIHLE?
A5. This is a syntax error.
Q6. What kind of error would you get if you used the keyword REPEAT but spelt it as REPEET?
A6. This is another syntax error.
Q7. What kind of error would you get if you wanted to do COST + TAX in a program but you accidentally wrote COST * TAX?
A7. This is a logic error.
Q8. What kind of error would you get if you wanted to do (SCORE + OFFSET) / 5 in a program but you accidentally wrote SCORE + OFFSET / 5?
A8. This is another logic error.
Q9. What are 'translator diagnostics'?
A9. This is the part of the translation process that checks your program code against a set of syntax rules, to see if you have broken any of the rules. It will either translate your code if you haven't or report any problems if you have.
Q10. How do you go about finding logic errors?
A10. Logic errors have to be picked up by writing a really good test plan and then methodically testing your code. It is very tempting to just run your code successfully, see that you get some results and then assume everything is okay! The purpose of testing is to check that the results you get are the results you predicted you would get before you ran the code. you have to try really hard to make your code fail. If you can't then you can start to think that it is okay. If you do find a logic error, you have to set about tracking the problem down. You can do this sometimes just by looking through the code again. You can also use dry running your code and trace tables, which is like you acting as a computer and running your code on paper, line by line, and seeing how variables and data structures change, for example. There are also a range of tools in programming IDEs that can help you e.g. breakpoints, variable watches, watch tables and stepping and so on.