Correct option is D
A
logical error occurs when the program compiles and runs without crashing but produces incorrect results due to flaws in the logic of the code. The following are logical errors:
(a) Using '=' instead of '==': This assigns a value instead of comparing it, which leads to incorrect program logic.
(b) Divide by zero: This is not a logical error but a runtime error.
(c) Failing to initialize variables: This leads to incorrect computations and is a logical error.
However:
(d) Using commas instead of semicolons in a for loop header: This is a
syntax error, as the compiler will not allow such a construct. Syntax errors are different from logical errors since they prevent the program from compiling.
Information Booster:
1.
Types of Errors:
·
Syntax Error: Mistakes in the structure or grammar of the code.
·
Logical Error: Errors in the reasoning of the program, leading to incorrect output.
·
Runtime Error: Errors that occur during the execution of the program (e.g., divide by zero).
2.
Logical Errors:
· Misuse of operators (e.g., '=' vs. '==').
· Incorrect initialization or updating of variables.
· Flawed algorithm logic.
3.
Avoiding Logical Errors:
· Use proper test cases to validate the program's output.
· Debugging tools and code reviews can help identify logical flaws.
Additional Knowledge:
·
Commas in for loop headers: While commas can be used for multiple variable initialization, they cannot replace semicolons in the loop structure.
·
Common causes of syntax errors:
· Missing brackets or parentheses.
· Using incorrect keywords or operators.