Skip to content
Computer ScienceISC Class 11 · Chapter 5

Objects: ISC Class 11 Computer Science Notes

Updated 15 October 2026 · Fact-checked

An object combines data, its attributes, with behaviour, its methods, and is an instance of a class. ISC Class 11 Computer Science covers class versus object, modelling a calculator or date as objects, the Java Virtual Machine with javac and java, compile-time and run-time errors, and exception handling with try, catch, throw, throws and finally.

What Objects is about

Everything a Java program does happens through objects: a date that can move to the next day, a calculator that can add and remember, a bank account that refuses a withdrawal. This chapter looks closely at what objects are, how Java runs programs that use them, and what happens when something goes wrong.

The CISCE 2028 syllabus (Section B, topic 5) asks for: (a) objects as data (attributes) + behaviour (methods); object as an instance of a class; the difference between object and class; (b) analysis of real-world programming examples in terms of objects and classes (calculator, date, number), and changing behaviour by adding, deleting or modifying methods; (c) the basic concept of a virtual machine; the Java Virtual Machine (JVM); compilation and execution of Java programs (javac and java); portability of .class files; and (d) compile-time and run-time errors; the basic concept of an exception, the Exception class, try-catch, throw, throws and finally; and how an exception object is passed up until a matching catch is found.

Three complete programs on this page (a date, a calculator and an exception demonstration) were compiled and run; their outputs are shown exactly.

Weightage: Objects is topic 5 of the ISC Class XI Computer Science syllabus, in Section B - Java. CISCE weights Section B at 30 of the 70 marks in Paper I (Theory). Class XI is examined by the school, so your school sets the paper within this weightage.Source: CISCE ISC Computer Science syllabus, examination year 2028, Class XI unit weightage

Key concepts

Object = data + behaviour; class vs object

An object is a software entity with:

1. State (data, attributes): values held in its data members (instance variables), such as day, month and year of a date.
2. Behaviour (methods): operations it can perform, which usually read or change its state, such as nextDay().
3. Identity: each object is distinct, even if two have the same values.

An object is an instance of a class. The class defines what data and methods every object will have; each object holds its own values.

BasisClassObject
MeaningBlueprint or templateInstance of the class
NatureLogical entityPhysical entity in memory
MemoryNo memory for instance data when declaredMemory allocated when created with new
Created byclass keywordnew operator (calls a constructor)
NumberDeclared onceMany objects from one class
ExampleMyDatenew MyDate(28, 2, 2028)

Message passing: objects interact by calling each other's methods (sending messages), for example a.nextDay().

Worked example

In MyDate a = new MyDate(28, 2, 2028); identify the class, the object reference, the constructor call and the state. [2 marks]

Try it first, then show the solutionSolution
  1. Class: MyDate; reference variable: a; new MyDate(28, 2, 2028) creates the object and calls the constructor.
  2. State: day = 28, month = 2, year = 2028.

Answer: Class MyDate; reference a; constructor call new MyDate(28, 2, 2028); state day 28, month 2, year 2028.

Real-world example: a Date object

A date is a natural object: its data are day, month and year; its behaviour includes checking for a leap year, finding the number of days in the month, moving to the next day and displaying itself.

Leap-year rule: a year is a leap year if it is divisible by 4 and not by 100, or if it is divisible by 400 (2000 was a leap year; 1900 and 2100 are not).

// A Date object: data (day, month, year) + behaviour (leap-year test, next day, display).
public class MyDate
{
    private int day, month, year;

    public MyDate(int d, int m, int y)
    {
        day = d;
        month = m;
        year = y;
    }

    public boolean isLeap()
    {
        return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    }

    public int daysInMonth()
    {
        int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if (month == 2 && isLeap())
            return 29;
        return days[month - 1];
    }

    public void nextDay()                 // changes the object's own state
    {
        day++;
        if (day > daysInMonth())
        {
            day = 1;
            month++;
            if (month > 12)
            {
                month = 1;
                year++;
            }
        }
    }

    public String toString()
    {
        return day + "/" + month + "/" + year;
    }

    public static void main(String[] args)
    {
        MyDate a = new MyDate(28, 2, 2028);
        MyDate b = new MyDate(31, 12, 2027);
        System.out.println(a + " leap year? " + a.isLeap());
        a.nextDay();
        System.out.println("next day: " + a);
        a.nextDay();
        System.out.println("next day: " + a);
        b.nextDay();
        System.out.println("day after 31/12/2027: " + b);
    }
}

Output:

28/2/2028 leap year? true
next day: 29/2/2028
next day: 1/3/2028
day after 31/12/2027: 1/1/2028

Note how nextDay() changes the object's own state: calling it twice on a moves 28/2/2028 to 29/2/2028 and then to 1/3/2028, because 2028 is a leap year. The toString() method lets an object be printed directly.

Worked example

What would nextDay() produce for the date 28/2/2027? [1 mark]

Try it first, then show the solutionSolution
  1. 2027 is not divisible by 4: February has 28 days, so the next day rolls over to 1 March.

Answer: 1/3/2027.

Real-world example: a Calculator; changing behaviour

A calculator object has state (a memory value) and behaviour (add, subtract, divide, power, store, recall). Its behaviour can be changed without touching the rest of the program:

1. Adding a method gives new behaviour (power, store, recall below).
2. Modifying a method changes how existing behaviour works (divide now guards against division by zero).
3. Deleting a method removes behaviour; any code that called it will no longer compile, which tells you where it was used.

// A Calculator object whose behaviour is changed by adding and modifying methods.
public class Calculator
{
    private double memory;                 // state: value kept in memory

    public double add(double a, double b)      { return a + b; }
    public double subtract(double a, double b) { return a - b; }

    // modified method: division now guards against division by zero
    public double divide(double a, double b)
    {
        if (b == 0)
        {
            System.out.println("Cannot divide by zero");
            return 0;
        }
        return a / b;
    }

    // added methods: new behaviour without changing the old ones
    public double power(double base, int exp)
    {
        double result = 1;
        for (int i = 1; i <= exp; i++)
            result = result * base;
        return result;
    }

    public void store(double v) { memory = v; }
    public double recall()      { return memory; }

    public static void main(String[] args)
    {
        Calculator c = new Calculator();
        System.out.println(c.add(12.5, 7.5));
        System.out.println(c.subtract(10, 25));
        System.out.println(c.divide(9, 4));
        System.out.println(c.divide(9, 0));
        System.out.println(c.power(2, 10));
        c.store(c.power(3, 4));
        System.out.println("memory = " + c.recall());
    }
}

Output:

20.0
-15.0
2.25
Cannot divide by zero
0.0
1024.0
memory = 81.0

A number can also be modelled as an object: data = the value; methods = isPrime(), sumOfDigits(), reverse(), isPalindrome(). Thinking 'which object owns this behaviour?' is the heart of object oriented design.

Worked example

Suppose a method square(double x) is added to Calculator. Does code that uses add() need to change? Why? [1 mark]

Try it first, then show the solutionSolution
  1. Adding a method extends the class; existing methods and their callers are unaffected.

Answer: No. Adding a method only adds behaviour; existing calls such as add() still work unchanged.

Virtual machine, JVM and portability

A virtual machine is software that behaves like a computer: it has its own instruction set and runs programs written for it, on top of a real (physical) machine.

Java's two-step execution:

1. Compilation: javac MyDate.java checks the source for errors and translates it into bytecode, stored in MyDate.class. Bytecode is the instruction set of the JVM, not of any real processor.
2. Execution: java MyDate starts the Java Virtual Machine (JVM), which loads the class, verifies the bytecode for safety, and executes it by interpreting it or compiling it on the fly into native machine code (just-in-time compilation).

Portability of .class files: a JVM exists for Windows, Linux, macOS and other systems. The same .class file runs on any of them without recompiling: 'write once, run anywhere'. Only the JVM is platform-specific.

Other JVM jobs: memory management with garbage collection (freeing objects no longer referenced) and security checks.

Worked example

A student compiles Hello.java on Windows and copies Hello.class to a Linux computer. What is needed to run it, and which command? [1 mark]

Try it first, then show the solutionSolution
  1. Bytecode is platform-independent; it needs a JVM on Linux.

Answer: A JVM (Java runtime) installed on Linux; run it with java Hello.

Compile-time and run-time errors; exceptions

Compile-time (syntax) errors are detected by javac; no .class file is produced until they are fixed. Examples: missing semicolon, undeclared variable, mismatched braces, wrong type assignment (int x = "hello";), calling a method that does not exist.

Run-time errors occur while the program is running, after successful compilation. Examples: division of an int by zero, array index out of range, using a null reference, converting "12a" to an int.

Logical errors: the program runs but gives a wrong answer (using + instead of −); neither javac nor the JVM can detect them; only testing reveals them.

An exception is an object representing an abnormal event at run time. When it occurs, the JVM creates an exception object and throws it; normal flow stops.

The Exception class: java.lang.Exception is the parent of most exceptions a program can handle, for example ArithmeticException (divide by zero), ArrayIndexOutOfBoundsException, NullPointerException, NumberFormatException, InputMismatchException and IOException. Each carries a message, available through getMessage().

Checked vs unchecked: checked exceptions (such as IOException, or a plain Exception you throw) must be caught or declared with throws, or the code will not compile; unchecked ones (RuntimeException subclasses such as ArithmeticException) need not be.

Worked example

Classify each as compile-time, run-time or logical: (i) int n = 10 / 0; executed; (ii) Sytem.out.println(5); (iii) area = l + b; for a rectangle. [2 marks]

Try it first, then show the solutionSolution
  1. (i) Compiles, but division by zero happens when run: run-time (ArithmeticException).
  2. (ii) Misspelt class name: compile-time. (iii) Runs but wrong formula: logical.

Answer: (i) Run-time; (ii) compile-time; (iii) logical.

try, catch, finally, throw, throws and exception propagation

try: encloses code that may throw an exception.

catch (Type e): handles an exception of that type (or its subclasses); several catch blocks may follow one try, more specific types first.

finally: a block that always runs after try (and catch), whether or not an exception occurred; used for clean-up (closing files).

throw: a statement that throws an exception object explicitly: throw new Exception("message");

throws: a clause in a method header declaring that the method may pass certain exceptions to its caller: static int checkedAge(int age) throws Exception.

Propagation: if a method does not catch an exception, it ends at once and the exception object is passed up to its caller, then to that caller's caller, and so on (up the call stack), until a matching catch is found. If none is found, the JVM prints the exception and stops the program.

// try-catch-finally, throw, throws, and how an exception travels up the call stack
// until a matching catch block is found.
public class ExceptionDemo
{
    // 'throws' declares that this method may pass an exception to its caller
    static int checkedAge(int age) throws Exception
    {
        if (age < 0)
            throw new Exception("Age cannot be negative: " + age);   // 'throw' creates and sends it
        return age;
    }

    static int level3(int x)
    {
        return 100 / x;                    // ArithmeticException when x is 0
    }

    static int level2(int x)
    {
        return level3(x) + 1;              // no catch here: the exception passes upward
    }

    public static void main(String[] args)
    {
        try
        {
            System.out.println("level2(5) = " + level2(5));
            System.out.println("level2(0) = " + level2(0));
            System.out.println("this line is skipped");
        }
        catch (ArithmeticException e)
        {
            System.out.println("Caught in main: " + e.getMessage());
        }
        finally
        {
            System.out.println("finally always runs");
        }

        int[] marks = {70, 85, 90};
        try
        {
            System.out.println(marks[3]);
        }
        catch (ArrayIndexOutOfBoundsException e)
        {
            System.out.println("Bad index: " + e.getMessage());
        }

        try
        {
            System.out.println("age = " + checkedAge(16));
            System.out.println("age = " + checkedAge(-3));
        }
        catch (Exception e)
        {
            System.out.println("Caught: " + e.getMessage());
        }
    }
}

Output:

level2(5) = 21
Caught in main: / by zero
finally always runs
Bad index: Index 3 out of bounds for length 3
age = 16
Caught: Age cannot be negative: -3

In the first try, level3 divides by zero; level3 and level2 have no catch, so the ArithmeticException travels up to main, where it is caught; the remaining line in try is skipped, and finally runs.

Worked example

In ExceptionDemo, list the methods the ArithmeticException passes through and where it is handled. [2 marks]

Try it first, then show the solutionSolution
  1. It is thrown in level3 (100 / 0), which has no catch; it passes to level2, which has no catch.
  2. It reaches main, whose catch (ArithmeticException e) handles it.

Answer: level3 → level2 → main, where it is caught; 'this line is skipped' never prints, and finally runs.

Objects in real life

Each concept, met in an everyday situation. Read the situation, answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.

Objects with state

Suppose your phone's alarm app lets you create several alarms, each with its own time, label and on/off switch.

Model this with a class and objects.

Try it first, then show the solutionSolution
  1. Class Alarm with data members time, label, enabled, and methods setTime(), toggle(), ring().
  2. Each alarm you create is an object with its own values (6:30 'School', on; 7:00 'Weekend', off).

Answer: One Alarm class; each alarm is an object with its own state and the shared behaviour.

Memory hook: Same buttons, different settings.

Exception handling

Imagine an ATM program asks for an amount and you type 'abc' instead of a number.

What should a well-written program do?

Try it first, then show the solutionSolution
  1. Converting 'abc' to a number throws NumberFormatException (or InputMismatchException with Scanner).
  2. The program should catch it, show 'Please enter a valid amount' and ask again, instead of crashing.

Answer: Catch the exception and recover gracefully, so the machine keeps working.

Memory hook: Catch it, explain it, carry on.

Portability

Suppose you write a quiz game in Java on a Windows laptop and your friend wants to play it on a Linux computer.

Do you need to send the source code and recompile?

Try it first, then show the solutionSolution
  1. javac produced platform-independent bytecode (.class or a .jar).
  2. Your friend needs only a JVM; the same .class files run unchanged.

Answer: No. Send the .class files; any JVM runs them: write once, run anywhere.

Memory hook: Bytecode travels; the JVM translates.

Topics in this chapter

Objects: syntax and key facts

Object creation
ClassName ref = new ClassName(arguments);
Compile and run
  1. 1.javac Name.java
  2. 2.Name.class (bytecode)
  3. 3.java Name (JVM loads, verifies, executes)
Exception handling skeleton
try { risky code } catch (Type e) { handle } finally { always runs }
Throwing and declaring
throw new Exception("msg"); ... returnType m(...) throws Exception
Leap year
(y % 4 == 0 && y % 100 != 0) || y % 400 == 0

Objects: diagrams to practise

Practise each one on paper: draw it from the checklist, then tick off every label.

Java compilation and execution

Explain the JVM and portability of .class files.

Draw

  1. Box 'MyDate.java (source)' → arrow 'javac' → box 'MyDate.class (bytecode)'.
  2. From the .class box, three arrows to three JVMs: 'JVM on Windows', 'JVM on Linux', 'JVM on macOS', each leading to 'output'.
  3. Inside one JVM box: class loader → bytecode verifier → interpreter / JIT compiler → garbage collector.

Label

  • source
  • javac
  • bytecode
  • JVM
  • platforms

Watch out: One .class file, many JVMs: that picture is the portability answer.

Exception propagation (call stack)

Explain how an exception is passed up until caught.

Draw

  1. Stack of boxes from bottom to top: main (has catch) → level2 (no catch) → level3 (100 / 0).
  2. Red arrow starting at level3 labelled 'ArithmeticException object' going down through level2 to main's catch.
  3. Mark 'skipped statements' and 'finally runs'.

Label

  • call stack
  • exception object
  • matching catch
  • finally

Watch out: Show that methods without a catch end immediately as the exception passes through.

How the school exam asks Objects

The question patterns to expect, and the method that works for each.

Question typeHow it is askedMethod
Theory (1-2 marks)Class vs object; what is bytecode or the JVM; compile-time vs run-time error; throw vs throws.Short definitions with a one-line Java example.
Output and error spotting (2-5 marks)Predict output of code with try-catch-finally; classify errors; trace nextDay() or other methods.Trace line by line; remember that finally always runs and skipped lines do not print.
Class design (10 marks)Design a class for a real-world object (date, number, calculator) with given methods and main.Data members, constructor, each method exactly as specified, and a main that tests them.

Tricks to remember Objects

Short memory aids. Say each one aloud once, then use it on a question the same day.

  • Object = S + B + I

    State, Behaviour, Identity.

    Use it when: What an object is.

  • throw throws one; throws warns

    throw (statement) actually throws an object; throws (in the header) only warns the caller.

    Use it when: throw vs throws.

  • Finally = always

    Whether the try succeeds, fails or is caught, finally runs.

    Use it when: finally block.

  • Compile: javac checks; run: JVM crashes

    Syntax errors stop javac; exceptions stop the running program unless caught.

    Use it when: Types of errors.

Objects: exam-level solved questions

Ordered from exam level to HOTS. Cover the solution, write your own, then compare step by step.

Example 1: Bytecode · 1 mark · MCQExam level

The output of javac is:
(a) machine code for the current processor
(b) bytecode in a .class file
(c) a .java file
(d) an executable .exe file

Try it first, then show the solutionSolution
  1. javac translates source into bytecode for the JVM.

Answer: (b) bytecode in a .class file.

Example 2: finally · 1 mark · Assertion-reasonHarder exam level

Assertion: A finally block runs even when the catch block handles an exception.
Reason: A finally block runs only when no exception occurs.
(a) Both Assertion and Reason are true and Reason is the correct explanation of Assertion.
(b) Both Assertion and Reason are true but Reason is not the correct explanation of Assertion.
(c) Assertion is true but Reason is false.
(d) Both Assertion and Reason are false.

Try it first, then show the solutionSolution
  1. finally always runs, with or without an exception (Assertion true, Reason false).

Answer: (c)

Example 3: Predict the output · 2 marks · Short answerExam level

Give the output of: static int f(int n) { try { if (n == 0) throw new ArithmeticException("zero"); return 10 / n; } catch (ArithmeticException e) { System.out.println("catch " + e.getMessage()); return -1; } finally { System.out.println("finally " + n); } } called as System.out.println("f(2) = " + f(2)); System.out.println("f(0) = " + f(0));

Try it first, then show the solutionSolution
  1. f(2): try returns 5, but finally prints first: 'finally 2', then 'f(2) = 5'.
  2. f(0): throw → catch prints 'catch zero' and returns −1; finally prints 'finally 0'; then 'f(0) = -1'.

Answer: finally 2 / f(2) = 5 / catch zero / finally 0 / f(0) = -1 (each on a new line, as the compiled check printed).

Example 4: Errors and exceptions · 5 marks · Short answerHarder exam level

Distinguish between compile-time and run-time errors with two examples each. Name the exception thrown by (i) 7 / 0 with ints, (ii) accessing a[5] when a has 5 elements, (iii) Integer.parseInt("12a").

Try it first, then show the solutionSolution
  1. Compile-time: found by javac before running (missing semicolon; undeclared variable).
  2. Run-time: occur during execution after successful compilation (division by zero; invalid index).
  3. (i) ArithmeticException.
  4. (ii) ArrayIndexOutOfBoundsException (valid indices are 0-4).
  5. (iii) NumberFormatException.

Answer: Compile-time errors are caught by javac; run-time errors arise during execution. (i) ArithmeticException; (ii) ArrayIndexOutOfBoundsException; (iii) NumberFormatException.

Example 5: Trace exception propagation · 5 marks · Short answerHOTS / competency

In ExceptionDemo, explain line by line what happens when level2(0) is called inside main's first try block, and give the lines printed by that try-catch-finally.

Try it first, then show the solutionSolution
  1. level2(5) runs first: level3(5) = 20, plus 1 = 21; 'level2(5) = 21' prints.
  2. level2(0) calls level3(0): 100 / 0 throws ArithmeticException; level3 has no catch, so it ends.
  3. level2 has no catch either; it ends and the exception passes to main.
  4. main's catch (ArithmeticException e) matches: prints 'Caught in main: / by zero'; the line 'this line is skipped' never runs.
  5. finally prints 'finally always runs'.

Answer: Output: level2(5) = 21; Caught in main: / by zero; finally always runs (from the compiled program).

Example 6: Design a Number class · 10 marks · Long answerHOTS / competency

Design a class Number with a private int value; a constructor; methods boolean isPrime(), int sumOfDigits(), int reverse() and boolean isPalindrome(); and main() that tests 131 and 120. Give the output.

Try it first, then show the solutionSolution
  1. isPrime: false if value < 2; loop i from 2 while i × i ≤ value; if value % i == 0 return false; return true.
  2. sumOfDigits: repeated % 10 and / 10 on a copy of value.
  3. reverse: rev = rev × 10 + n % 10 on a copy.
  4. isPalindrome: return reverse() == value.
  5. main: create new Number(131) and new Number(120); print all four results for each.
  6. Output (compiled program): 131 → prime true, digit sum 5, reverse 131, palindrome true; 120 → prime false, digit sum 3, reverse 21, palindrome false.

Answer: See the compiled program:

// A Number object: data = value; behaviour = prime test, digit sum, reverse, palindrome test.
public class Number
{
    private int value;

    public Number(int v)
    {
        value = v;
    }

    public boolean isPrime()
    {
        if (value < 2)
            return false;
        for (int i = 2; i * i <= value; i++)
            if (value % i == 0)
                return false;
        return true;
    }

    public int sumOfDigits()
    {
        int n = value, s = 0;            // work on a copy; value is unchanged
        while (n > 0)
        {
            s += n % 10;
            n /= 10;
        }
        return s;
    }

    public int reverse()
    {
        int n = value, rev = 0;
        while (n > 0)
        {
            rev = rev * 10 + n % 10;
            n /= 10;
        }
        return rev;
    }

    public boolean isPalindrome()
    {
        return reverse() == value;
    }

    public static void main(String[] args)
    {
        int[] tests = {131, 120};
        for (int i = 0; i < tests.length; i++)
        {
            Number num = new Number(tests[i]);
            System.out.println(tests[i] + ": prime " + num.isPrime() + ", digit sum " + num.sumOfDigits()
                               + ", reverse " + num.reverse() + ", palindrome " + num.isPalindrome());
        }
    }
}
Output:
131: prime true, digit sum 5, reverse 131, palindrome true
120: prime false, digit sum 3, reverse 21, palindrome false

Case-study practice (Section D style)

Read the situation, then answer every part. In a school exam set on the board pattern this is a 5-mark question split into 2 + 2 + 1 marks.

The marks-entry program

Suppose a teacher's Java program reads students' marks as text from a form and stores them in an array of 40 elements. Sometimes a mark is typed as '8O' (letter O instead of zero), and once the program tried to store the 41st student's mark. The program crashed both times.

  1. (i) · 2 marks

    Name the exception in each case and say whether it is a compile-time or run-time problem.

    Answer

    Answer: '8O' → NumberFormatException (from Integer.parseInt); 41st mark → ArrayIndexOutOfBoundsException (index 40 on a 40-element array). Both are run-time exceptions.

    1 mark each.

  2. (ii) · 2 marks

    Write the try-catch structure that would let the program report the bad mark and continue.

    Answer

    Answer: try { mark = Integer.parseInt(text); } catch (NumberFormatException e) { System.out.println("Invalid mark: " + text); mark = 0; } (and continue with the next student).

    1 mark try, 1 mark catch.

  3. (iii) · 1 mark

    Where should clean-up such as closing the input file be placed, and why?

    Answer

    Answer: In a finally block, because it runs whether or not an exception occurs.

    1 mark.

Where the marks are

How a typical answer is split into step marks. Show every step: a right final answer alone rarely earns full marks.

Predict the output of try-catch-finally (2 marks) (2 marks)

StepMarks
Order of finally before the caller's print for f(2)1
catch and finally lines for f(0)1

Compile-time vs run-time errors; naming exceptions (5 marks) (5 marks)

StepMarks
Distinction with examples2
Each exception named (three)3

Number class (10 marks) (10 marks)

StepMarks
Data member and constructor1
isPrime3
sumOfDigits and reverse3
isPalindrome1
main and correct output2

Where students lose marks in Objects

The slips examiners see most often, and what to write instead.

  • Mistake: Thinking finally runs only when there is no exception

    finally runs in every case, even after a return in try or catch.

    Do this: Trace finally before the caller's next statement.

    Usually costs: 1 mark.

  • Mistake: Mixing up throw and throws

    throw is a statement that throws an object; throws is part of a method header.

    Do this: throw new X(...); versus m() throws X.

    Usually costs: 1 mark.

  • Mistake: Writing that javac produces machine code

    javac produces bytecode; the JVM runs it.

    Do this: Source → bytecode (.class) → JVM.

    Usually costs: 1 mark.

  • Mistake: Changing a copy instead of the object in methods like reverse()

    Destroying the data member's value breaks later methods.

    Do this: Work on a local copy (int n = value;).

    Usually costs: 1-2 marks.

Practice questions on Objects

Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.

  1. Q1 · 1 mark · MCQExam level

    Which keyword is used in a method header to declare that it may pass an exception to its caller?
    (a) throw
    (b) throws
    (c) catch
    (d) finally

    Hint

    Header, not statement.

    Answer

    (b) throws.

  2. Q2 · 1 mark · Very short answerExam level

    What is the JVM?

    Hint

    Software computer.

    Answer

    The Java Virtual Machine: software that loads, verifies and executes Java bytecode on a particular platform.

  3. Q3 · 2 marks · Short answerExam level

    Give two differences between a class and an object.

    Hint

    Blueprint, memory.

    Answer

    A class is a blueprint (logical entity) declared once; an object is an instance (physical entity) created with new, many from one class. A class needs no memory for instance data; each object gets its own memory for its data members.

  4. Q4 · 1 mark · Very short answerHarder exam level

    Using the leap-year rule in MyDate, which of 1900, 2000 and 2100 are leap years?

    Hint

    Century years need 400.

    Answer

    Only 2000 (the compiled check printed false true false).

  5. Q5 · 1 mark · Very short answerHarder exam level

    What does nextDay() give for 30/4/2026, and why?

    Hint

    April has 30 days.

    Answer

    1/5/2026, because April has 30 days, so day 31 rolls over to 1 May.

  6. Q6 · 5 marks · Short answerHarder exam level

    Explain how an exception object is passed up the call stack, with an example of three methods.

    Hint

    No catch → caller.

    Answer

    When a statement throws an exception, the current method stops. If the method has a matching catch, it handles the exception there. If not, the method ends and the exception object is passed to the method that called it; this repeats up the chain of callers until a matching catch is found. If main also lacks one, the JVM prints the stack trace and terminates. Example: main calls a(), a() calls b(), and b() divides by zero; b and a have no catch, so the ArithmeticException reaches main's catch.

  7. Q7 · 5 marks · Short answerHOTS / competency

    Write a method double safeDivide(int a, int b) that throws an Exception with the message 'Divisor is zero' when b is 0 and returns a / (double) b otherwise. Show how main should call it.

    Hint

    throw inside, throws in the header, try in main.

    Answer

    static double safeDivide(int a, int b) throws Exception { if (b == 0) throw new Exception("Divisor is zero"); return a / (double) b; } In main: try { System.out.println(safeDivide(7, 2)); System.out.println(safeDivide(7, 0)); } catch (Exception e) { System.out.println(e.getMessage()); } This prints 3.5 and then 'Divisor is zero'.

  8. Q8 · 2 marks · Short answerHarder exam level

    Which exceptions are thrown by (i) String s = null; s.length(); and (ii) Integer.parseInt("12a")?

    Hint

    Null; format.

    Answer

    (i) NullPointerException; (ii) NumberFormatException (message: For input string: "12a"), as the compiled check confirmed.

  9. Q9 · 5 marks · Short answerHOTS / competency

    Explain, with the Calculator example, how the behaviour of an object can be changed by adding, modifying and deleting methods.

    Hint

    power, divide, remove.

    Answer

    Adding: power(), store() and recall() give the calculator new abilities without touching add() or subtract(). Modifying: divide() was changed to check for zero and print a message instead of returning Infinity; callers keep the same call but get safer behaviour. Deleting: if subtract() were removed, every call to it would fail to compile, showing which code depended on it, so deletion must be done with care. In each case the change is confined to the class, an advantage of OOP.

  10. Q10 · 2 marks · Short answerHOTS / competency

    Why can a .class file compiled on one operating system run on another, while a C program usually must be recompiled?

    Hint

    Bytecode vs native code.

    Answer

    javac produces bytecode for the JVM, and each operating system has its own JVM that understands the same bytecode. A C compiler produces native machine code for one processor and operating system, so it must be recompiled for another.

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. 1.Object consists of?Show answer

    State (data), behaviour (methods) and identity.

  2. 2.javac produces?Show answer

    Bytecode in a .class file.

  3. 3.What runs bytecode?Show answer

    The JVM.

  4. 4.Exception for int division by zero?Show answer

    ArithmeticException.

  5. 5.Block that always runs?Show answer

    finally.

  6. 6.Uncaught exception in a method?Show answer

    Passed up to the caller until a matching catch is found.

Best order to study it

  1. Object = data + behaviour; class vs object.
  2. Date example: state-changing methods.
  3. Calculator and number examples; changing behaviour.
  4. Virtual machine, JVM, javac and java; portability.
  5. Compile-time, run-time and logical errors; exceptions.
  6. try, catch, finally, throw, throws; propagation.

Quick revision checklist

The night before the exam: can you do each of these without looking?

  • Object: state + behaviour + identity; instance of a class.
  • Class: blueprint; object: created with new, own memory.
  • javac → bytecode (.class) → java (JVM) on any platform.
  • Errors: compile-time (javac), run-time (exceptions), logical (wrong output).
  • try / catch (specific first) / finally (always); throw an object; throws in header.
  • Uncaught exceptions travel up the call stack to a matching catch.

Objects: frequently asked questions

Is an exception the same as an error?

An exception is an abnormal event during execution that a program can catch and handle (for example ArithmeticException). Java also has an Error class for serious problems (such as running out of memory) that programs normally do not handle. Compile-time errors are a different matter: they stop compilation.

Does finally run if there is a return statement in try?

Yes. finally runs before the method actually returns, which is why 'finally 2' prints before 'f(2) = 5' in the worked example.

Why must I declare throws Exception but not throws ArithmeticException?

Exception (and IOException) are checked exceptions: the compiler insists they are caught or declared. ArithmeticException is unchecked (a RuntimeException), so declaring it is optional.

Based on: CISCE ISC 2028 Computer Science syllabus (Classes XI and XII); CISCE Regulations for the ISC Examination Year 2028 (Class XI internal examination)