Question 1 Report
When a compiler translates a program, it may detect various types of errors.
(a) Explain the difference between a syntax error and a logic error. [2]
(b) State whether each of the following would be detected by a compiler. Explain your reasoning. [4]
(i) A missing semicolon at the end of a statement
(ii) A program that calculates the area of a rectangle using length + width instead of length * width
(a) Difference between a syntax error and a logic error: [2]
A syntax error is a violation of the programming language's grammar rules. This includes misspelled keywords, missing brackets, incorrect punctuation, or wrong structure. Syntax errors prevent the program from compiling or running at all. [1]
A logic error is when the program compiles and runs without crashing but produces incorrect results because the algorithm or calculations are wrong. The code is grammatically valid but does not do what the programmer intended. [1]
(b) Whether each error would be detected by a compiler: [4]
(i) A missing semicolon: Yes, detected by the compiler. [1] A missing semicolon is a syntax error. The compiler analyses the structure of the code and recognises that the statement is incomplete or improperly terminated. It will report an error message indicating the location of the missing semicolon. [1]
(ii) Using length + width instead of length * width: No, not detected by the compiler. [1] Using + instead of * is valid syntax because both are legitimate arithmetic operators. The compiler sees nothing wrong with adding two numbers. However, the formula is logically incorrect: it calculates the perimeter-related value rather than the area. This is a logic error that only becomes apparent when testing with known values (e.g. a 5 x 3 rectangle gives 8 instead of 15). [1]
Everything you need to excel in your exams