Packages, Syntax, Runtime and Logical Errors, and Comments in Java (ICSE Class 9)
Updated 18 October 2026 · Fact-checked
A package is a group of related classes; java.lang is imported automatically and java.util holds Scanner. Errors are of three kinds: syntax errors are rule violations found by the compiler, runtime errors stop a running program with an exception, and logical errors give wrong results without any message. Comments written with // or /* */ are notes that the compiler ignores.
Notice that each error appears at a different stage: the compiler finds syntax errors, running finds runtime errors, and only testing the output finds logical e
Illustration drawn for QuestionBuddy.
Understand Packages, Types of Errors and Comments in Java Programs
Packages. A package is a collection of related classes stored together, like a folder of files. Packages organise Java's large library and avoid name clashes.
- java.lang: basic classes such as System, String and Math; imported automatically.
- java.util: utility classes, including Scanner.
- java.io: input and output with files and streams.
- java.awt and javax.swing: graphical user interfaces.
- java.net: networking.
Use import java.util.*; for all classes of a package or import java.util.Scanner; for one class.
Syntax errors (compile-time). Violations of the grammar of Java, found by the compiler; the program does not compile. Examples: a missing semicolon, a misspelt keyword, unmatched braces, an undeclared variable, a missing import for Scanner.
Runtime errors. The program compiles but fails while running with an exception. Examples: dividing an int by zero (ArithmeticException: / by zero) and typing text where nextInt() expects a number (InputMismatchException). Dividing a decimal by zero does not throw an exception; it gives Infinity.
Logical errors. The program compiles and runs but produces wrong results because the logic or formula is wrong. The computer shows no message; the error is found by testing with values whose answers you know. Example: avg = a + b + c / 3 instead of (a + b + c) / 3.
Debugging. Finding and correcting errors. Test with simple values, print intermediate results and trace by hand.
Comments. Notes for readers, ignored by the compiler. A single-line comment starts with // and ends with the line. A multi-line comment lies between /* and */. Comments explain the purpose, record the author, and can switch off a line during testing. A statement written inside a comment is not executed and is not checked for errors.
Neighbouring topics: initialisation, parameters and the Scanner input methods are on the previous page of this chapter.
Packages, Types of Errors and Comments in Java Programs: key facts
- Three kinds of error
Syntax found by the compiler, no .class file Runtime program stops with an exception Logical runs but gives wrong results - Say when each is detected.
- Comments
- Single-line: two slashes to the end of the line
- Multi-line: slash-star to star-slash over many lines
- ignored by the compiler
- Not executed.
- Packages
java.lang automatic java.util Scanner java.io files java.awt and javax.swing graphics - import java.util.*; imports all classes.
Packages, Types of Errors and Comments in Java Programs 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.
Kinds of mistakes in a recipe
Suppose a recipe is written with a spelling error in the word "boil", then a step says to divide the milk by zero spoons, and a third step says "add 2 spoons of salt" where 2 spoons of sugar were meant.
Match the three mistakes with the three kinds of errors.
Try it first, then show the solutionSolution
- The spelling error is caught when the recipe is read: a syntax error.
- The impossible instruction stops the cooking midway: a runtime error.
- The salt makes a wrong dish with no warning: a logical error.
Answer: Spelling mistake is a syntax error, impossible step a runtime error and wrong ingredient a logical error.
Memory hook: Read, run, result.
Tricks to remember Packages, Types of Errors and Comments in Java Programs
Short memory aids. Say each one aloud once, then use it on a question the same day.
Compile, Crash, Confuse
Syntax errors stop compilation, runtime errors crash, logical errors give confusing wrong answers.
Use it when: Three kinds of error.
lang is free
java.lang is imported by default; java.util needs an import.
Use it when: Packages.
Comment is invisible
The compiler skips everything inside a comment.
Use it when: Comment questions.
Packages, Types of Errors and Comments in Java Programs: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: Which package · 1 mark · MCQBoard level
A program must read and write files. Which package would it import?
(a) java.io (b) java.lang (c) java.awt (d) java.net
Try it first, then show the solutionSolution
- java.io holds the classes for input and output with files and streams.
Answer: (a) java.io
Example 2: Classify the errors · 4 marks · Short answerHarder board level
Classify each error: (i) System.out.println("Hi" with the closing bracket missing; (ii) int r = 10 / x; where x is 0; (iii) perimeter of a rectangle written as 2 * l + b; (iv) using Scanner without the import line.
Try it first, then show the solutionSolution
- (i) Syntax error: the statement is not grammatically complete. (1 mark)
- (ii) Runtime error: ArithmeticException: / by zero while the program runs. (1 mark)
- (iii) Logical error: the program runs but gives a wrong perimeter. (1 mark)
- (iv) Syntax (compile-time) error: the compiler cannot find the symbol Scanner. (1 mark)
Answer: (i) syntax (ii) runtime (iii) logical (iv) syntax
Example 3: A logical error in an average · 3 marks · Short answerHarder board level
A student writes:
int a = 60, b = 90, c = 30;
double avg = a + b + c / 3;
System.out.println(avg);Find the output, the correct average and the corrected statement.
Try it first, then show the solutionSolution
- c / 3 = 10 is done first, so avg = 60 + 90 + 10 = 160, printed as 160.0. (1 mark)
- The correct average is (60 + 90 + 30) / 3 = 60. (1 mark)
- Corrected statement: double avg = (a + b + c) / 3.0; which prints 60.0. (1 mark)
Answer: Output 160.0; correct average 60.0 using double avg = (a + b + c) / 3.0;
Example 4: Case study: comments in a program · 4 marks · Case-basedHOTS / competency
Read the code:
int a = 4; // first value
/* int b = 5;
a = a + b; */
a = a * 2; // double it
System.out.println(a);(i) State the output. (1)
(ii) How many statements are executed? (1)
(iii) The variable b is not declared outside the comment. Why is there no error? (2)
Try it first, then show the solutionSolution
- (i) a = 4, then a = 8, so the output is 8.
- (ii) Three: int a = 4; a = a * 2; and the println.
- (iii) The lines between /* and */ form a comment, which the compiler ignores completely, so b is never compiled or checked.
Answer: (i) 8 (ii) three statements (iii) the compiler ignores everything inside a comment
Example 5: Division by zero · 3 marks · Short answerHarder board level
Find what happens in each case with int x = 0; and double y = 0;:
System.out.println(10 / x);
System.out.println(10.0 / y);Try it first, then show the solutionSolution
- 10 / x divides an int by int zero: the program stops with ArithmeticException: / by zero. (1 mark)
- 10.0 / y divides a decimal by zero: no exception is thrown and Infinity is printed. (1 mark)
- Since the first statement stops the program, the second line is not reached in the same run; run separately it prints Infinity. (1 mark)
Answer: The first throws ArithmeticException: / by zero; the second, run alone, prints Infinity.
Where students lose marks in Packages, Types of Errors and Comments in Java Programs
The slips examiners see most often, and what to write instead.
Mistake: Calling every error a syntax error
Only one kind is remembered.
Wrong working
Divide by zero is a syntax error
Right working
Divide by zero is a runtime error
Do this: Syntax means the compiler finds it.
Usually costs: 1 mark
Mistake: Importing java.lang
The default is forgotten.
Wrong working
import java.lang.*; is needed for String
Right working
It is imported automatically
Do this: Only other packages need import.
Usually costs: 1 mark
Mistake: Executing code in a comment
Comment limits are missed.
Wrong working
Statements between /* and */ run
Right working
They are ignored by the compiler
Do this: Everything inside is skipped.
Usually costs: 1 mark
Exam tips
- Give one example with every kind of error.
- State who finds the error: the compiler, the running program or the tester.
- Name java.lang as the default package.
- Mention that Infinity, not an exception, results from decimal division by zero.
Practice questions on Packages, Types of Errors and Comments in Java Programs
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
The package that contains the Scanner class is:
(a) java.lang (b) java.util (c) java.io (d) java.netHint
Utility classes.
Answer
(b) java.util
Q2 · 1 mark · Very short answerBoard level
Write the symbol used for a single-line comment and a statement that is switched off by it.
Hint
Two slashes.
Answer
// is placed before the text; for example // System.out.println(x); is not executed.
Q3 · 3 marks · Short answerHarder board level
Classify: (i) using a variable that was never declared; (ii) typing abc for a nextInt() input; (iii) writing the average of three marks as m1 + m2 + m3 / 3.
Hint
Compile, run, result.
Answer
(i) syntax (cannot find symbol) (ii) runtime (InputMismatchException) (iii) logical
Q4 · 3 marks · Short answerHarder board level
Find the error, correct it and state the output:
int p = 100, r = 5, t = 2; int si = p * r * t / 100 System.out.println("SI = " + si);Hint
Look at the end of each statement.
Answer
A semicolon is missing after the SI statement (syntax error). After correction the output is SI = 10 because 100 x 5 x 2 = 1000 and 1000 / 100 = 10.
Q5 · 3 marks · Short answerHOTS / competency
A student writes the perimeter of a rectangle as 2 * l + b and a teacher says "test with simple values". Test it for l = 5 and b = 3 and explain what kind of error it is.
Hint
Compare with 2 times the sum.
Answer
2 * 5 + 3 = 13 but the correct perimeter is 2 * (5 + 3) = 16. The program runs without any message but gives a wrong result, so it is a logical error. Correct: 2 * (l + b).
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.Which package holds Scanner?Show answer
java.util
2.Which package is imported by default?Show answer
java.lang
3.Which error is found by testing with known answers?Show answer
A logical error.
4.Exception for int divided by zero?Show answer
ArithmeticException
5.Symbols of a multi-line comment?Show answer
/* and */
6.What is debugging?Show answer
Finding and correcting errors in a program.
Packages, Types of Errors and Comments in Java Programs: frequently asked questions
Is a missing semicolon a runtime error?
No. It is a syntax error found when compiling.
Do comments take memory when the program runs?
No. The compiler ignores them.
Can a program have a logical error and still compile?
Yes. Logical errors cannot be detected by the compiler.