Debugging Errors and the Section B Class Pattern (ISC Class 12 Computer Science)
Updated 18 October 2026 · Draft, not yet fact-checked
Syntax errors break Java's rules and stop compilation; run-time errors (exceptions) occur while the program runs; logical errors give wrong results because the algorithm is wrong and are found only by testing and dry runs. A Section B answer is a class with data members, a constructor, an input method, processing methods, a display or check method and main( ), documented with comments.
Follow the arrows: syntax errors stop compiling, run-time errors crash the run, and logical errors run but give wrong answers, so test and dry run.
Illustration drawn for QuestionBuddy.
Understand Debugging Errors and the Section B Class Pattern
1. Types of error
- Syntax (compile-time): break the rules of Java, so the compiler rejects the program: a missing semicolon, unmatched braces, a misspelt keyword, an undeclared variable, a type mismatch (int x = 2.5;).
- Run-time (exception): the program compiles but fails while running: division by zero (ArithmeticException), array index out of bounds (ArrayIndexOutOfBoundsException), calling a method on null (NullPointerException), wrong input type (InputMismatchException).
- Logical: the program runs but gives wrong output because the algorithm or code is wrong, such as a wrong loop condition or integer division.
2. Three small faults, traced by hand
- *Off-by-one*:
for (int i = 1; i < 5; i++) s += i;adds 1 + 2 + 3 + 4 = 10, not 1 + 2 + 3 + 4 + 5 = 15; the condition should be i <= 5. - *Integer division*:
double avg = (7 + 4) / 2;evaluates 11 / 2 in integers, giving 5, so avg = 5.0; use(7 + 4) / 2.0to get 5.5. - *Index error*: for
int[] a = new int[5];the loopfor (int i = 0; i <= 5; i++)reaches a[5], which does not exist (valid indexes are 0 to 4), so ArrayIndexOutOfBoundsException occurs at i = 5.
3. Debugging techniques. Dry runs; printing intermediate values; testing small cases; checking boundary values; the BlueJ debugger to step through code and inspect variables.
4. The Section B pattern. Each question describes a class: its name, data members and methods. Marks are given for each method, for main( ) and for documentation.
1. Class header and data members exactly as named, with the stated types.
2. A constructor that sets every data member to a legal initial value; it has no return type.
3. An input method using Scanner, respecting any stated condition.
4. Processing methods with the specified signatures; if 'recursive technique' is asked, the method calls itself and uses no loop.
5. A display or check method that calls the processing methods and prints clear messages.
6. main( ) creates an object and calls the methods in order.
7. Comments stating the purpose of each method and key variables.
5. A model class (palindrome number).
import java.util.Scanner;
public class PalNum
{
int n; // number entered
int rev; // its reverse
PalNum()
{
n = 0;
rev = 0;
}
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a positive integer:");
n = sc.nextInt();
}
// stores the reverse of n in rev
void reverse()
{
int t = n;
rev = 0;
while (t > 0)
{
rev = rev * 10 + t % 10;
t = t / 10;
}
}
void check()
{
reverse();
if (rev == n)
System.out.println(n + " is a palindrome");
else
System.out.println(n + " is not a palindrome");
}
public static void main(String[] args)
{
PalNum ob = new PalNum();
ob.accept();
ob.check();
}
}Hand trace for input 1331: t = 1331 gives rev = 1; t = 133, rev = 13; t = 13, rev = 133; t = 1, rev = 1331; t = 0 stops. rev == n, so the output is 1331 is a palindrome. For input 124: rev = 4, 42, 421; 421 != 124, so 124 is not a palindrome. (The program was not compiled here; the outputs are traced by hand.)
Debugging Errors and the Section B Class Pattern: key facts
- Types of error
Syntax compiler rejects Run-time exception while running Logical wrong result - Logical errors need dry runs.
- Section B parts
- Data members
- Constructor
- Input method
- Processing methods
- Display or check
- main
- Comments
- Marks are given for each.
- Run-time exceptions
- ArithmeticException
- ArrayIndexOutOfBoundsException
- NullPointerException
- InputMismatchException
- Know one cause each.
How to answer Debugging Errors and the Section B Class Pattern questions
Questions show a faulty snippet and ask for the error type and correction, or ask for a class to be written.
- 1Run the snippet by hand with a small input and compare with the intended result.
- 2Classify the fault: compile failure (syntax), crash while running (run-time), wrong answer (logical).
- 3Correct the line and re-trace to check.
- 4For a class, use the seven-point pattern and keep names and signatures exactly as given.
Debugging Errors and the Section B Class Pattern 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.
Run-time error
Suppose a cashier program divides the day's total by the number of customers and the number is zero on a holiday.
Name the type of error and the exception.
Try it first, then show the solutionSolution
- The program compiled and ran but failed during execution.
- Integer division by zero causes ArithmeticException.
Answer: Run-time error; ArithmeticException.
Memory hook: Works until the data breaks it.
Logical error
Imagine a shop program shows an average price of 5.0 for items priced 7 and 4, though the correct average is 5.5.
Name the error and the fix.
Try it first, then show the solutionSolution
- The program runs but the result is wrong: logical.
- Integer division (7 + 4) / 2 gives 5; divide by 2.0.
Answer: Logical error from integer division; use 2.0.
Memory hook: No message, wrong answer.
Tricks to remember Debugging Errors and the Section B Class Pattern
Short memory aids. Say each one aloud once, then use it on a question the same day.
Compile, Crash, Confuse
Syntax errors stop the compile; run-time errors crash; logical errors only confuse (wrong answer).
Use it when: Classifying errors.
Section B checklist C-I-P-D-M
Constructor, Input, Processing, Display, Main (plus comments).
Use it when: Writing a full class.
Debugging Errors and the Section B Class Pattern: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: Classify the error · 1 mark · MCQBoard level
The statement int x = 2.5; causes:
(a) a run-time error
(b) a logical error
(c) a syntax (compile-time) error
(d) no error
Try it first, then show the solutionSolution
- Assigning a double to an int without a cast is rejected by the compiler.
Answer: (c)
Example 2: Index error · 2 marks · Short answerBoard level
State the error in 'int[] a = new int[5]; for (int i = 0; i <= 5; i++) a[i] = i * 2;', its type, and the corrected condition.
Try it first, then show the solutionSolution
- At i = 5 the program tries a[5], but valid indexes are 0 to 4: ArrayIndexOutOfBoundsException, a run-time error.
- Use i < 5 (or i < a.length).
Answer: Run-time error at i = 5; use i < 5.
Example 3: Off-by-one · 4 marks · Short answerHarder board level
A loop 'for (int i = 1; i < 5; i++) s += i;' is meant to find the sum of the first five natural numbers. Find the value computed, the intended value, the type of error and the correction.
Try it first, then show the solutionSolution
- Computed: 1 + 2 + 3 + 4 = 10.
- Intended: 1 + 2 + 3 + 4 + 5 = 15.
- The loop stops one iteration early: a logical (off-by-one) error.
- Correct to i <= 5; recheck: 15.
Answer: 10 instead of 15; logical off-by-one; use i <= 5.
Example 4: Trace the model class · 4 marks · Short answerHarder board level
For the PalNum program, trace the method reverse( ) for n = 2552 and n = 1204, and give the output of check( ) in each case.
Try it first, then show the solutionSolution
- n = 2552: rev = 2, 25, 255, 2552; rev == n, so output is '2552 is a palindrome'.
- n = 1204: t = 1204: rev = 4; t = 120: rev = 40; t = 12: rev = 402; t = 1: rev = 4021; 4021 != 1204.
- Output: '1204 is not a palindrome'.
- Both traces end when t becomes 0.
Answer: 2552 is a palindrome; 1204 is not a palindrome.
Example 5: Write a Section B class · 8 marks · Long answerHOTS / competency
Write a Java class Fact with data members n (int) and f (long). Include a constructor, a method accept( ) that reads n, a method calc( ) that stores n! in f using a loop, and display( ) that prints the result, and main( ). Then trace calc( ) for n = 5 and state the output.
Try it first, then show the solutionSolution
- Header and data members: class Fact with int n and long f.
- Constructor: Fact() { n = 0; f = 1; }.
- accept(): create a Scanner, read n with nextInt().
- calc(): f = 1; for (int i = 2; i <= n; i++) f = f * i;.
- display(): print n + "! = " + f.
- main(): create an object, call accept(), calc(), display().
- Trace for n = 5: f = 1; i = 2: 2; i = 3: 6; i = 4: 24; i = 5: 120. Output: 5! = 120.
- Comments describe each method; the constructor has no return type.
Answer: A complete class; output 5! = 120.
Where students lose marks in Debugging Errors and the Section B Class Pattern
The slips examiners see most often, and what to write instead.
Mistake: Calling a wrong-answer fault a syntax error
Syntax errors stop compilation.
Wrong working
A loop that sums 1 to 4 instead of 1 to 5 is a syntax error.
Right working
It is a logical error; the program compiles and runs.
Do this: Does it compile? Does it crash? Is it wrong?
Usually costs: 1 mark.
Mistake: Missing main( ) in a Section B answer
main( ) carries marks.
Wrong working
Class with methods but no main( ).
Right working
main( ) creates an object and calls accept( ) and the processing methods.
Do this: Always write main( ).
Usually costs: 1-2 marks.
Mistake: Changing the method names given in the question
Names and signatures must match exactly.
Wrong working
Question asks for reverse( ); student writes rev( ).
Right working
Use the name reverse( ) exactly.
Do this: Copy names from the question.
Usually costs: 1 mark.
Exam tips
- Keep names, types and signatures exactly as given.
- State the type of error before correcting it.
- End Section B answers with main( ), and add short comments.
- Show a hand trace for one input if asked for output.
Practice questions on Debugging Errors and the Section B Class Pattern
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 · Very short answerBoard level
Name the exception raised when a program divides an integer by zero.
Hint
Arithmetic.
Answer
ArithmeticException.
Q2 · 2 marks · Short answerBoard level
State two techniques used for debugging a program.
Hint
By hand or by tool.
Answer
Dry runs and printing intermediate values (also testing small cases, checking boundary values, the BlueJ debugger).
Q3 · 2 marks · Short answerHarder board level
Why must the constructor in a Section B class have no return type?
Hint
Definition of a constructor.
Answer
A constructor is a special method with the class name that initialises the object when it is created; writing a return type would make it an ordinary method, so it would not be called automatically.
Q4 · 4 marks · Short answerHarder board level
A statement 'double avg = (a + b) / 2;' with int a = 9 and int b = 4 prints 6.0. Find the correct value, the type of error and the corrected statement.
Hint
Integer division.
Answer
(9 + 4) / 2 in integers is 13 / 2 = 6, giving 6.0. Correct average = 6.5. This is a logical error from integer division. Correct statement: double avg = (a + b) / 2.0;.
Q5 · 4 marks · Short answerHOTS / competency
A student writes a Section B class in which the input method also prints the result and main( ) is missing. Evaluate using the standard pattern and say what to change.
Hint
Separation of tasks.
Answer
The pattern separates input, processing and display, and requires main( ) to create an object and call the methods. Move printing into a display or check method, keep accept( ) for input only, add main( ) with an object and ordered calls, and add comments. Marks are lost for each missing component.
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 error stops compilation?Show answer
A syntax error.
2.Which error is found only by testing?Show answer
A logical error.
3.Does a constructor have a return type?Show answer
No.
4.What does main( ) do in Section B?Show answer
Creates an object and calls the methods in order.
Debugging Errors and the Section B Class Pattern: frequently asked questions
How many Section B questions must be answered?
Two of three, each worth 10 marks, as stated on the chapter page.
Is the Scanner import required?
Yes, when the program reads input with Scanner.
Are flowcharts needed in Section B?
No; the program must show the logic clearly.