Name the type of error (syntax, runtime or logical error) in each case given below: (i) Math.sqrt(36 - 45) (ii) int a; b; c;
Topic: Types of errors
Answer
(i) Logical error — the statement compiles and runs, but the square root of a negative number gives NaN (not a number) rather than a meaningful result. (ii) Syntax error — variables in a single declaration must be separated by commas: int a, b, c;
Distinguish the three types by WHEN each is discovered. (i) Nothing is wrong with the grammar, so the compiler accepts it and the program runs — it simply produces a meaningless answer. That is the definition of a logical error. (Java returns NaN rather than throwing an exception, which is why it is not a runtime error.) (ii) The semicolons break the rules of declaration, so the compiler refuses the program outright — a syntax error, caught before execution.