Which of the following is a runtime error rather than a logical error?

2022

Which of the following is a runtime error rather than a logical error?

  1. A.

    Using ‘=’ instead of ‘==’ to determine whether two values are equal

  2. B.

    Divide by zero

  3. C.

    Failing to initialize counter and total variables before the body of a loop

  4. D.

    Using commas instead of the two required semicolons in a for-loop header

Attempted by 519 students.

Show answer & explanation

Correct answer: B

Concept

Program errors fall into three families distinguished by WHEN they are caught. A syntax error breaks the language grammar and is caught at compile/parse time, before the program runs at all. A runtime error occurs while the program is executing — an illegal operation makes the runtime raise an exception, crash, or enter undefined behaviour. A logical error breaks neither grammar nor execution: the program compiles and runs to completion, but the wrong logic yields an incorrect result that you can only find by checking the output.

Application

The question asks which choice fails during execution rather than producing a wrong result from clean logic. Test each option against the "when is it caught?" rule:

  • "Divide by zero": division by zero is undefined, so the fault can only appear at the instant the operation executes — the runtime raises an exception (or undefined behaviour). This is caught during execution, so it is a runtime error.

  • Using ‘=’ in place of ‘==’: the code compiles and runs to completion but takes the wrong branch — a wrong result from clean execution, i.e. a logical error.

  • Not initialising counter/total variables: the loop runs without crashing but accumulates from garbage and returns a wrong value — again a logical error.

  • Commas instead of the two required semicolons in a for-header: this violates the for-loop grammar, so the compiler rejects it before execution — a syntax error, caught at compile time.

Cross-check

Only one option fails AT EXECUTION time; the two logical errors run to completion and the comma case never even compiles. Hence the single runtime error is "Divide by zero".

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Mppsc Assistant Professor