Syntax, Run-time and Logical Errors, and Comments in Java (ICSE Class 10)
Updated 17 October 2026 · Fact-checked
A syntax error breaks the rules of Java and is reported by the compiler, so the program does not run. A run-time error appears while the program is executing and stops it (for example integer division by zero). A logical error is a mistake in the method: the program runs but gives a wrong result. Comments, written with // for one line or /* ... */ for many lines, are ignored by the compiler.
Understand Types of Errors and Comments in Java
1. Syntax error (compile-time error). The code breaks the grammar rules of Java: a missing semicolon, a misspelt keyword, an undeclared variable, a wrong type assignment such as int a = 5.5;, mismatched brackets. The compiler lists the errors and creates no bytecode until they are corrected.
2. Run-time error. The program compiles but fails during execution because an operation cannot be carried out: int r = 10 / 0; (ArithmeticException: / by zero), reading a word where a number is expected, using an array index that does not exist, calling a method on a null reference. The program stops at that statement.
3. Logical error. The program compiles and runs without any message, but the answer is wrong because the programmer's steps are wrong: area = l + b; instead of l * b, or a + b / 2 instead of (a + b) / 2. Neither the compiler nor the JVM reports it; it is found only by testing with known data (a dry run).
*Special case*: real-number division by zero does not stop the program: 210.0 / 0 gives Infinity while 210 / 0 with two ints is a run-time error.
In short: a syntax error is found at compilation and the program does not run; a run-time error is found during execution and the program stops midway; a logical error is found only by testing and the program runs with wrong output.
Comments. A comment is explanatory text ignored by the compiler. It helps readers and can switch off a statement while testing.
- Single line: starts with
//and goes to the end of the line. - Multi-line: starts with
/*and ends with*/and may cover many lines. It cannot be nested inside another multi-line comment.
Comment symbols inside a string literal are just text: "// not a comment" prints as written.
Types of Errors and Comments in Java: key facts
- Three types of errors
Syntax found at compilation Run-time found during execution Logical found by testing, output is wrong - Give an example of each in a 3-mark answer.
- Comments
- Single line: // text | Multi-line: starts with slash-star and ends with star-slash
- The compiler ignores comments.
- Common run-time errors
- Integer division by zero
- Number expected but text typed
- Array index out of range
- Method called on null
- The program stops at that statement.
- Division by zero
- int / 0: run-time error | double / 0: Infinity
- 210.0 / 0 prints Infinity.
How to answer Types of Errors and Comments in Java questions
Use this routine for error-finding questions.
- 1Read the code once as the compiler would: check semicolons, brackets, spellings, declarations and type matches. Any fault here is a syntax error.
- 2If it would compile, run it on the given data: does any statement fail (division by zero, bad index)? That is a run-time error.
- 3If it runs fully, compare the result with what is wanted; a wrong formula or condition is a logical error.
- 4Write the correction next to each error.
- 5For output with comments, delete every comment first and then trace the rest.
Types of Errors and Comments in Java in real life
The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.
Three kinds of mistakes in a recipe
Suppose a cooking recipe is written down: a sentence missing a verb cannot be understood at all (syntax), a step that says "pour from an empty jug" cannot be carried out (run-time), and a step that says "add 2 spoons of salt" instead of sugar makes a wrong dish without anyone stopping you (logical).
Which of the three mistakes will a spell-checker style tool catch before cooking?
Try it first, then show the solutionSolution
- The tool checks grammar before use: only the broken sentence, the syntax mistake.
- The empty jug shows up during cooking; the wrong ingredient shows up only when the dish is tasted.
Answer: Only the syntax mistake is caught beforehand, as the compiler does.
Memory hook: Compiler catches grammar, execution catches the impossible, testing catches the wrong.
Tricks to remember Types of Errors and Comments in Java
Short memory aids. Say each one aloud once, then use it on a question the same day.
Before, during, after
Syntax is found before running, run-time during running, logical after seeing the output.
Use it when: When-detected questions.
Runs but wrong = logical
No message and wrong answer: logical.
Use it when: Classifying an error.
Slash-slash vs slash-star
// ends with the line; /* needs */ to end.
Use it when: Comment output questions.
Types of Errors and Comments in Java: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: Classify the errors · 3 marks · Short answerBoard level
Name the type of error in each:
(i) int x = 10 (semicolon missing)
(ii) int r = 10 / 0;
(iii) A program to find the area of a rectangle uses area = l + b;
Try it first, then show the solutionSolution
- (i) Missing semicolon breaks the grammar: syntax error, found by the compiler. (1 mark)
- (ii) Compiles, but integer division by zero fails during execution: run-time error (ArithmeticException). (1 mark)
- (iii) Compiles and runs but the formula is wrong: logical error. (1 mark)
Answer: (i) Syntax (ii) Run-time (iii) Logical
Example 2: Find and correct the errors · 3 marks · Short answerHarder board level
Find the errors and correct them:
int a = 5
double d = 2.5;
a = d;
String s = 'hello';Try it first, then show the solutionSolution
- Line 1 has no semicolon: int a = 5; (1 mark)
- a = d stores a double in an int (possible lossy conversion): a = (int) d; (1 mark)
- A string needs double quotes; single quotes are for one character: String s = "hello"; (1 mark)
Answer: All three are syntax (compile-time) errors; corrected: int a = 5; a = (int) d; String s = "hello";
Example 3: Output with comments · 2 marks · Short answerHarder board level
Find the output:
int x = 2;
// x = 10;
/* x = x + 5;
x = x * 3; */
x += 4;
System.out.println(x);Try it first, then show the solutionSolution
- The // line and everything between /* and */ are comments and are ignored. (1 mark)
- Only x = 2 and x += 4 run, so x = 6. (1 mark)
Answer: 6
Example 4: Case study: a wrong average · 4 marks · Case-basedHOTS / competency
A student writes this to find the average of three subjects with marks m1 = 60, m2 = 70, m3 = 80 (all int):
double avg = m1 + m2 + m3 / 3;
System.out.println(avg);(i) What is printed? (2)
(ii) Which type of error is this and does the compiler report it? (1)
(iii) Write the correct statement and its output. (1)
Try it first, then show the solutionSolution
- (i) m3 / 3 is done first: 80 / 3 = 26 (int division). Then 60 + 70 + 26 = 156, stored as 156.0, so 156.0 is printed.
- (ii) It is a logical error; the compiler does not report it because the code is grammatically correct.
- (iii) double avg = (m1 + m2 + m3) / 3.0; gives 210 / 3.0 = 70.0.
Answer: (i) 156.0 (ii) logical error, not reported (iii) (m1 + m2 + m3) / 3.0 gives 70.0
Example 5: Division by zero: int versus double · 3 marks · Short answerHarder board level
With int total = 210 and int count = 0, state what happens for each statement:
(i) System.out.println(total / count);
(ii) System.out.println(210.0 / count);
Try it first, then show the solutionSolution
- (i) Both operands are int; dividing an int by zero is not allowed: the program stops with ArithmeticException (/ by zero) at run time. (1 mark)
- (ii) 210.0 is a double, so the division is real-number division; division by zero gives Infinity and the program continues. (1 mark)
- The compiler reports neither statement because both are valid syntax. (1 mark)
Answer: (i) Run-time ArithmeticException (ii) prints Infinity
Where students lose marks in Types of Errors and Comments in Java
The slips examiners see most often, and what to write instead.
Mistake: Calling a wrong-output program a syntax error
Syntax errors stop compilation; a program that runs but gives a wrong answer has a logical error.
Wrong working
area = l + b; is a syntax error.
Right working
area = l + b; is a logical error.
Do this: Ask: did it compile and run?
Usually costs: 1 mark
Mistake: Saying int division by zero is reported by the compiler
It is valid syntax; the failure happens during execution.
Wrong working
10 / 0 gives a compile-time error.
Right working
10 / 0 compiles; it throws ArithmeticException at run time.
Do this: Compile-time means the grammar is wrong.
Usually costs: 1 mark
Mistake: Ending a multi-line comment with // or forgetting */
A /* comment continues until */; without it the compiler reports an error.
Wrong working
/* total of marks //
Right working
/* total of marks */
Do this: Match every /* with */.
Usually costs: 1 mark
Exam tips
- In a classification question give one reason after the name, such as "reported by the compiler".
- Examples given in the answer (missing semicolon, division by zero, wrong formula) earn the mark for illustration.
- In output questions remove comments before tracing.
- Remember that a logical error needs test data to be found.
Practice questions on Types of Errors and Comments in Java
Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.
Q1 · 1 mark · MCQBoard level
An error found by the compiler is called a:
(a) logical error (b) run-time error (c) syntax error (d) none of theseHint
Before the program runs.
Answer
(c) syntax error
Q2 · 1 mark · Very short answerBoard level
Write the symbols that start and end a multi-line comment.
Hint
Slash-star and star-slash.
Answer
/* to start and */ to end.
Q3 · 3 marks · Short answerHarder board level
Name the type of error in each case:
(i) System.out.println("Hi") (no semicolon)
(ii) int[] a = new int[3]; a[3] = 1;
(iii) avg = a + b / 2; when the average of a and b is wantedHint
Compile, execute or wrong result?
Answer
(i) Syntax error (ii) Run-time error (index 3 does not exist in an array of size 3) (iii) Logical error (should be (a + b) / 2)
Q4 · 2 marks · Short answerHarder board level
Find the output:
int n = 4; // n = 9; System.out.println(n); System.out.println("// not a comment");Hint
Symbols inside quotes are text.
Answer
4
// not a commentQ5 · 1 mark · Assertion-reasonHOTS / competency
Assertion (A): A logical error is not reported by the Java compiler.
Reason (R): A program with a logical error is grammatically correct but produces wrong results.
(a) Both A and R are true and R is the correct explanation of A. (b) Both A and R are true but R is not the correct explanation of A. (c) A is true but R is false. (d) A is false but R is true.Hint
What does the compiler check?
Answer
(a) Both are true and R explains A.
Test your recall
Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.
1.When is a syntax error detected?Show answer
During compilation.
2.Give an example of a run-time error.Show answer
Dividing an int by zero.
3.Which kind of comment can run over several lines?Show answer
A multi-line comment, written between /* and */.
4.Does the compiler ignore comments?Show answer
Yes.
5.What does 210.0 / 0 give?Show answer
Infinity
6.Which error cannot be found by the compiler or JVM?Show answer
A logical error.
Types of Errors and Comments in Java: frequently asked questions
What is the difference between a syntax error and a logical error?
A syntax error breaks the rules of the language and stops compilation; a logical error follows the rules but gives wrong output.
Is a missing semicolon a run-time error?
No, it is a syntax error found when compiling.
Can comments be nested?
A multi-line comment cannot contain another multi-line comment; the first */ ends it.