Prashnikaप्रश्निका

Inheritance, Interfaces and Polymorphism - ISC Class 12 Computer Science Questions with Answers, Page 2

44 past-paper questions on Inheritance, Interfaces and Polymorphism from ISC Class 12 Computer Science papers (2026-2017), newest first, in full. Questions 21-40 are on this page, 20 to a page. Tap "Show answer" under a question to see its answer.

Practise these questions with filters
2025 · 5 marks · ProgramOpen: A library issues books on rental basis at a 2% charge on the cost price of the…

Write the program described below.

A library issues books on rental basis at a 2% charge on the cost price of the book per day. As per the rules of the library, a book can be retained for 7 days without any fine. If the book is returned after 7 days, a fine will also be charged for the excess days as per the chart given below:
Number of excess daysFine per day (Rs.)
1 to 52.00
6 to 103.00
Above 105.00
A super class Library has been defined. Define a sub class Compute to calculate the fine and the total amount. The details of the members of both the classes are given below: Class name : Library Data members/instance variables: name : to store the name of the book author : to store the author of the book p : to store the price of the book (in decimals) Methods / Member functions: Library( ... ) : parameterized constructor to assign values to the data members void show( ) : displays the book details Class name : Compute Data members/instance variables: d : number of days taken in returning the book f : to store the fine (in decimals) Methods / Member functions: Compute( ... ) : parameterized constructor to assign values to the data members of both the classes void fine( ) : calculates the fine for the excess days as given in the table above void show( ) : displays the book details along with the number of days, fine and the total amount to be paid. Total amount is (2% of price of book * total no of days) + fine Assume that the super class Library has been defined. Using the concepts of Inheritance, specify the class Compute giving the details of constructor, void fine ( ) and void show ( ) functions. The super class, main function and algorithm need NOT be written.
Show answer

Answer

Official answer key
class Compute extends Library
{
    int d;      // number of days taken in returning the book
    double f;   // fine

    Compute(String name, String author, double p, int d)
    {
        super(name, author, p);
        this.d = d;
        f = 0.0;
    }

    void fine()
    {
        int excess = d - 7;
        if (excess <= 0)
            f = 0.0;
        else if (excess <= 5)
            f = excess * 2.0;
        else if (excess <= 10)
            f = excess * 3.0;
        else
            f = excess * 5.0;
    }

    void show()
    {
        super.show();
        System.out.println("Number of days : " + d);
        System.out.println("Fine           : " + f);
        System.out.println("Total amount   : " + ((0.02 * p * d) + f));
    }
}
Explanation: Compute extends Library, so its constructor calls super(name, author, p) to initialise the inherited data members, then sets d. fine() computes the excess days beyond the free 7-day period and applies the given tiered rate (Rs.2/day for 1-5 excess days, Rs.3/day for 6-10, Rs.5/day beyond 10; 0 if not overdue). show() calls super.show() to print the book details, then prints the days, fine and total amount, calculated as (2% of price x total days) + fine. Tested (with a stub Library class) for price=500.0, d=12: excess=5, so fine=10.0 and total=(0.02x500x12)+10=130.0, matching manual calculation (verified by running the code).
2025 · 1 mark · Assertion-reasonOpen: For overloaded method, signature matching happens at compile time. Given below…

Study the Assertion and Reason and choose the correct option.

Assertion: For overloaded method, signature matching happens at compile time.

Reason: The overloaded method must have a unique signature for its every prototype.

Given below are two statements marked Assertion and Reason. Read the two statements carefully and choose the correct option.
  • (a)Both Assertion and Reason are true and Reason is the correct explanation for Assertion.
  • (b)Both Assertion and Reason are true but Reason is not the correct explanation for Assertion.
  • (c)Assertion is true and Reason is false.
  • (d)Both Assertion and Reason are false.
Show answer

Answer

AI

Correct option: (a)

Answer: (a) Both Assertion and Reason are true and Reason is the correct explanation for Assertion. Method overloading is resolved by the compiler at compile time (static binding) precisely because every overloaded prototype has a unique signature, allowing the compiler to unambiguously match the call to the correct method at compile time itself.
2024 · 5 marks · ProgramOpen: A super class EmpSal has been defined to store the details of an employee…

Write the program described below.

A super class EmpSal has been defined to store the details of an employee. Define a subclass Overtime to compute the total salary of the employee, after adding the overtime amount based on the following criteria. • If hours are more than 40, then ₹ 5000 are added to salary as an overtime amount • If hours are between 30 and 40 (both inclusive), then ₹ 3000 are added to salary as an overtime amount • If hours are less than 30, then the salary remains unchanged The details of the members of both the classes are given below.
Class name:EmpSal
Data members/instance variables:
empnum:to store the name of the employee
empcode:integer to store the employee code
salary:to store the salary of the employee in decimal
Methods / Member functions:
EmpSal(...):parameterised constructor to assign values to data members
void show( ):to display the details of the employee
Class name:Overtime
Data members/instance variables:
hours:integer to store overtime in hours
totsal:to store the total salary in decimal
Methods / Member functions:
Overtime(...):parameterised constructor to assign values to data members of both the classes
void calSal():calculates the total salary by adding the overtime amount to salary as per the criteria given above
void show():to display the employee details along with the total salary (salary + overtime amount)
Assume that the super class EmpSal has been defined. Using the concept of inheritance, specify the class Overtime giving the details of the constructor (...), void calSal( ) and void show( ). The super class, main function and algorithm need NOT be written.
Show answer

Answer

AI
class Overtime extends EmpSal
{
    int hours;
    double totsal;

    Overtime(String n, int c, double s, int h)
    {
        super(n, c, s);
        hours = h;
        totsal = 0.0;
    }

    void calSal()
    {
        if (hours > 40)
            totsal = salary + 5000;
        else if (hours >= 30 && hours <= 40)
            totsal = salary + 3000;
        else
            totsal = salary;
    }

    void show()
    {
        super.show();
        calSal();
        System.out.println("Overtime Hours: " + hours);
        System.out.println("Total Salary: " + totsal);
    }
}
Explanation: Overtime extends EmpSal. Its constructor calls super(n, c, s) to initialise the inherited empnum, empcode and salary, then initialises hours and totsal. calSal() applies the three overtime criteria (hours>40 adds 5000, 30<=hours<=40 adds 3000, hours<30 leaves salary unchanged) to compute totsal. show() calls super.show() to display the inherited employee details, invokes calSal(), then prints the overtime hours and total salary. Tested with salary=40000: hours=45 gives Total Salary=45000.0, hours=35 gives 43000.0, and hours=20 gives 40000.0 (unchanged) - all matching the given criteria.
2024 · 1 mark · Short answerOpen: State any one purpose of using interfaces in Java programming.

Answer the following.

State any one purpose of using interfaces in Java programming.
Show answer

Answer

AI
One purpose of using interfaces in Java is to achieve multiple inheritance of type: since a Java class can extend only one class, it can implement more than one interface to inherit (and be forced to provide implementations for) the abstract behaviour/method signatures declared in each of them.
2024 · 1 mark · Short answerOpen: State any one use of interfaces in Java.

Answer the following.

State any one use of interfaces in Java.
Show answer

Answer

AI
Interfaces are used in Java to achieve multiple inheritance: a class can implement more than one interface, inheriting abstract behaviour from multiple sources without the ambiguity that arises from extending multiple classes.
2024 · 5 marks · ProgramOpen: A super class Circle has been defined to calculate the area of a circle. Define…

Write the program described below.

A super class Circle has been defined to calculate the area of a circle. Define a subclass Volume to calculate the volume of a cylinder. The details of the members of both the classes are given below: Class name : Circle Data members/instance variables: radius : to store the radius in decimals area : to store the area of a circle Methods / Member functions: Circle( ... ) : parameterized constructor to assign values to the data members void cal_area() : calculates the area of a circle ($\pi r^2$) void display( ) : to display the area of the circle Class name : Volume Data members/instance variables: height : to store the height of the cylinder in decimals volume : to store the volume of the cylinder in decimals Methods / Member functions: Volume( ... ) : parameterized constructor to assign values to the data members of both the classes double calculate( ) : to calculate and return the volume of the cylinder using the formula ($\pi r^2 h$) where, r is the radius and h is the height void display( ) : to display the area of a circle and volume of a cylinder Assume that the super class Circle has been defined. Using the concept of inheritance, specify the class Volume giving the details of the constructor(...), double calculate( ) and void display( ). The super class, main function and algorithm need NOT be written.
Show answer

Answer

AI
class Volume extends Circle
{
    double height, volume;

    Volume(double r, double h)
    {
        super(r);
        height = h;
    }

    double calculate()
    {
        cal_area();
        volume = area * height;
        return volume;
    }

    void display()
    {
        super.display();
        System.out.println("Volume of Cylinder = " + volume);
    }
}
Explanation: The parameterised constructor Volume(r,h) passes the radius up to the superclass Circle's constructor using super(r), and stores height in the subclass. calculate() invokes the inherited cal_area() (which computes and stores area = pi * radius^2 in the superclass), then multiplies this area by height to get the volume of the cylinder (pi * r^2 * h), storing it in volume and returning it. display() calls super.display() to reuse the superclass's printing of the area, then prints the volume, demonstrating inheritance (reusing radius/area/cal_area from Circle) and method overriding of display(). Tested (run for real) with a stub Circle class matching the given specification: for radius=3, height=5, the program correctly computed area = 28.274... and volume = 141.371...
2023 · 1 mark · Short answerOpen: Mention any two properties of the data members of an Interface.

Answer the following.

Mention any two properties of the data members of an Interface.
Show answer

Answer

AI
1. Data members of an interface are implicitly public, static and final, i.e. they are constants. 2. They must be initialised at the point of declaration and their values cannot be changed by the implementing class.
2023 · 1 mark · Short answerOpen: State any one purpose of using the keyword super in Java programming.

Answer the following.

State any one purpose of using the keyword super in Java programming.
Show answer

Answer

AI
The keyword super is used in a subclass's constructor to invoke (call) the constructor of its immediate superclass, so that the inherited data members get initialised by the superclass's own constructor (e.g. super(args); as the first statement).
2023 · 5 marks · ProgramOpen: A super class Bank has been defined to store the details of the customer in a…

Write the program described below.

A super class Bank has been defined to store the details of the customer in a bank. Define a subclass Interest to calculate the compound interest. The details of the members of both the classes are given below:
Class name:Bank
Data members/instance variables:
name:to store the name of the customer
acc_no:integer to store the account number
principal:to store the principal amount in decimals
Methods / Member functions:
Bank( ... ):parameterized constructor to assign values to the data members
void display( ):to display the customer details
Class name:Interest
Data members/instance variables:
rate:to store the interest rate in decimals
time:to store the time period in decimals
Methods / Member functions:
Interest( ... ):parameterized constructor to assign values to the data members of both the classes
double calculate( ):to calculate and return the compound interest using the formula $[ \text{CI} = P ( 1 + R/100 )^N - P ]$ where, P is the principal, R is the rate and N is the time
void display( ):to display the customer details along with the compound interest
Assume that the super class Bank has been defined. Using the concept of inheritance, specify the class Interest giving the details of the constructor(...), double calculate( ) and void display( ). The super class, main function and algorithm need NOT be written.
Show answer

Answer

AI
class Interest extends Bank
{
    double rate, time;

    Interest(String n, int a, double p, double r, double t)
    {
        super(n, a, p);
        rate = r;
        time = t;
    }

    double calculate()
    {
        double ci = principal * Math.pow((1 + rate / 100), time) - principal;
        return ci;
    }

    void display()
    {
        super.display();
        System.out.println("Rate of Interest = " + rate);
        System.out.println("Time Period = " + time);
        System.out.println("Compound Interest = " + calculate());
    }
}
Explanation: Interest extends Bank. Its parameterised constructor passes name, account number and principal up to the superclass via super(n,a,p), then stores rate and time in the subclass. calculate() applies the given compound-interest formula CI = P(1+R/100)^N - P using the inherited principal along with rate and time, and returns the result. display() calls super.display() to reuse the superclass's printing of the customer details, then additionally prints rate, time and the computed compound interest, demonstrating inheritance and method overriding.
2023 · 1 mark · MCQOpen: Transitive nature of inheritance is implemented through

Choose the correct option.

Transitive nature of inheritance is implemented through
  • (a)Single inheritance
  • (b)Multiple inheritance
  • (c)Hybrid inheritance
  • (d)Multilevel inheritance
Show answer

Answer

AI

Correct option: (d)

Answer: (d) Multilevel inheritance The transitive property (if A relates to B, and B relates to C, then A relates to C) is realised by Multilevel inheritance, where a subclass extends a class that is itself a subclass, so members pass down through several levels.
2023 · 1 mark · MCQOpen: The keyword that allows multi-level inheritance in Java programming is:

Answer the following.

The keyword that allows multi-level inheritance in Java programming is:
  • (a)implements
  • (b)super
  • (c)extends
  • (d)this
Show answer

Answer

AI

Correct option: (c)

Answer: (c) extends A class inherits from another class with extends; a chain such as class B extends A and class C extends B gives multi-level inheritance.
2023 · 1 mark · Short answerOpen: Define Interface with respect to data abstraction.

Answer the following.

Define Interface with respect to data abstraction.
Show answer

Answer

AI
An Interface is a Java construct that contains only abstract methods (method signatures with no body) and constants, with no implementation. It specifies WHAT a class must do without specifying HOW, thus achieving full data abstraction; any class that implements the interface must provide (define) the actual implementation of every method declared in it.
2023 · 5 marks · ProgramOpen: A super class Demand has been defined to store the details of the demands for a…

Write the program described below.

A super class Demand has been defined to store the details of the demands for a product. Define a subclass Supply which contains the production and supply details of the products. The details of the members of both the classes are given below:
Class name:Demand
Data members/instance variables:
pid:string to store the product ID
pname:string to store the product name
pdemand:integer to store the quantity demanded for the product
Methods / Member functions:
Demand(...):parameterised constructor to assign values to the data members
void display( ):to display the details of the product
Class name:Supply
Data members/instance variables:
pproduced:integer to store the quantity of the product produced
prate:to store the cost per unit of the product in decimal form
Methods / Member functions:
Supply(...):parameterised constructor to assign values to the data members of both the classes
double calculation( ):returns the difference between the amount of demand (rate $\times$ demand) and the amount produced (rate $\times$ produced)
void display( ):to display the details of the product and the difference in amount of demand and amount of supply by invoking the method calculation()
Assume that the super class Demand has been defined. Using the concept of inheritance, specify the class Supply giving the details of the constructor(...), double calculation( ) and void display( ). The super class, main function and algorithm need NOT be written.
Show answer

Answer

AI
class Supply extends Demand
{
    int pproduced;   // quantity produced
    double prate;    // cost per unit

    // parameterised constructor for both classes
    Supply(String id, String name, int d, int p, double r)
    {
        super(id, name, d);
        pproduced = p;
        prate = r;
    }

    // difference between amount of demand and amount produced
    double calculation()
    {
        return (prate * pdemand) - (prate * pproduced);
    }

    // displays all details and the difference
    void display()
    {
        super.display();
        System.out.println("Quantity produced: " + pproduced);
        System.out.println("Rate per unit: " + prate);
        System.out.println("Difference in amount of demand and supply: " + calculation());
    }
}
Supply inherits pid, pname and pdemand from Demand. Its constructor passes the first three values to the super class constructor with super(...). display() overrides the super class method: it first calls super.display() for the product details, then prints its own data and the value returned by calculation(). Tested with a small Demand class: Supply("P101", "Pen", 500, 420, 12.5) printed the product details and "Difference in amount of demand and supply: 1000.0" ($12.5 \times 500 - 12.5 \times 420 = 1000$).
2022 · 1 mark · MCQOpen: The ability of an object to take many forms is known as:

Choose the correct option.

The ability of an object to take many forms is known as:
  • (a)inheritance
  • (b)data abstraction
  • (c)overriding
  • (d)polymorphism
Show answer

Answer

AI

Correct option: (d)

Answer: (d) polymorphism Polymorphism is the ability of an object or function to take many forms.
2022 · 4 marks · ProgramOpen: A super class Godown has been defined to store the details of the stock of a…

Write the program described below.

A super class Godown has been defined to store the details of the stock of a retail store. Define a subclass Update to store the details of the items purchased with the new rate and update the stock. Some of the members of both the classes are given below: Class name: Godown Data members/instance variables: item: to store the name of the item qty: to store the quantity of an item in stock rate: to store the unit price of an item amt: to store the net value of the item in stock Member functions/methods: Godown(...): parameterized constructor to assign value to the data members void display(): to display the stock details Class name: Update Data members/instance variables: pur_qty: to store the purchase quantity pur_rate: to store the unit price of the purchased item Member functions/methods: Update(...): parameterized constructor to assign values to the data members of both the classes void update(): to update the stock by adding the previous quantity by the purchased quantity and replace the rate of the item if there is a difference in the purchase rate. Also update the current stock value as: $(quantity * unit\ price)$ void display(): to display the stock details before and after updating Assume that the super class Godown has been defined. Using the concept of inheritance, specify the class Update giving details of the constructor, void update() and void display(). The super class, main function and algorithm need NOT be written.
Show answer

Answer

AI
class Update extends Godown
{
    int pur_qty;
    double pur_rate;

    Update(String i, int q, double r, int pq, double pr)
    {
        super(i, q, r);
        pur_qty = pq;
        pur_rate = pr;
    }

    void update()
    {
        qty = qty + pur_qty;
        if (rate != pur_rate)
            rate = pur_rate;
        amt = qty * rate;
    }

    void display()
    {
        System.out.println("Stock before updating:");
        super.display();
        update();
        System.out.println("Stock after updating:");
        super.display();
    }
}
The constructor passes the stock details to Godown using super(). update() adds the purchased quantity, replaces the rate if it differs and recomputes the stock value as quantity * rate. display() shows the stock before and after the update. (Assumes rate and amt are double and qty is int in Godown.)
2022 · 1 mark · MCQOpen: The keyword used by a class to acquire the properties of an interface is:

Choose the correct option.

The keyword used by a class to acquire the properties of an interface is:
  • (a)import
  • (b)implements
  • (c)extends
  • (d)include
Show answer

Answer

AI

Correct option: (b)

Answer: (b) implements A class uses the keyword implements to acquire (implement) an interface; extends is used for class inheritance.
2020 · 2 marks · Short answerOpen: Define Interface. How is it different from a Class?

Answer the following in short.

Define Interface. How is it different from a Class?
Show answer

Answer

AI
Interface: an interface in Java is a reference type that contains only abstract method declarations (no method bodies) and constants (public static final data members). It specifies what a class must do; a class uses the keyword implements and must define all its methods. Difference from a class: a class contains data members and complete method definitions and objects can be created from it with new, whereas an interface has no instance variables, no method bodies and cannot be instantiated. A class can extend only one super class, but it can implement many interfaces, so interfaces give a form of multiple inheritance.
2020 · 5 marks · ProgramOpen: An interface Shape is defined with a method area() which returns the area of…

Write the program described below.

An interface Shape is defined with a method area() which returns the area of the implementing shape. Create the classes Circle and Rectangle which implement the interface Shape. These classes have attributes which reflect their dimensions (radius for a circle, height and width for a rectangle) which are set by their constructors. The details of the members of the interface and both the classes are given below: Interface name : Shape Member functions/methods: double area() : returns the area of the implementing shape Class name : Circle Data members/instance variables: radius : to store radius of the circle in decimal Member functions/methods: Circle(int r) : parameterized constructor to initiallize radius=r double area() : to calculate area of the circle [ area of a circle is $3.14 * radius * radius$ ] Class name : Rectangle Data members/instance variables: length : to store length of the rectangle in decimal breadth : to store breadth of the rectangle in decimal Member functions / methods: Rectangle(int l, int b) : parameterized constructor to initialize length=l, breadth=b double area() : to calculate area of the rectangle [ area of a rectangle is $length * breadth$ ] Assume that the Interface Shape has been defined. Using the concept of inheritance, specify the classes Circle and Rectangle giving details of their constructors and double area() respectively. The interface, main function and algorithm need NOT be written.
Show answer

Answer

AI
class Circle implements Shape
{
    double radius;

    Circle(int r)
    {
        radius = r;
    }

    public double area()
    {
        return 3.14 * radius * radius;
    }
}

class Rectangle implements Shape
{
    double length, breadth;

    Rectangle(int l, int b)
    {
        length = l;
        breadth = b;
    }

    public double area()
    {
        return length * breadth;
    }
}
Explanation: Both classes implement the interface Shape (the keyword implements is used for an interface), so each must define the method area(), which must be declared public as interface methods are public. The constructor of Circle stores the radius and area() returns 3.14 x radius x radius. The constructor of Rectangle stores the length and breadth and area() returns length x breadth. Tested with an interface Shape { double area(); }: Circle(7).area() gives 153.86 and Rectangle(5, 4).area() gives 20.0.
2020 · 5 marks · ProgramOpen: An interface Data is defined with a data member and a method volume( ) which…

Write the program described below.

An interface Data is defined with a data member and a method volume( ) which returns the volume of the implementing shape. A super class Base has been defined to contain the radius of a geometrical shape. Define a sub class CalVol which uses the properties of the interface Data and the class Base and calculates the volume of a cylinder. The details of the members of the interface and both the classes are given below: Interface name : Data Data member: double pi : initialize pi = 3.142 Member functions/methods: double volume( ) Class name: Base Data member/instance variable: rad : to store the radius in decimal Member functions/methods: Base(…) : parameterized constructor to initialize the data member void show( ) : displays the radius with an appropriate message Class name: CalVol Data member/instance variable: ht : to store the height in decimal Member functions/methods: CalVol(…) : parameterized constructor to initialize the data members of both the classes double volume( ) : calculates the volume of a sphere by using the formula ( pi x radius2 x height ) void show( ) : displays the data members of both the classes and the volume of the sphere with appropriate message Assume that the interface Data and the super class Base has been defined. Using the concept of inheritance, specify the class CalVol giving the details of the constructor(…), double volume( ) and void show( ). The interface, super class, main function and algorithm need NOT be written.
Show answer

Answer

AI
class CalVol extends Base implements Data
{
    double ht;

    CalVol(double r, double h)
    {
        super(r);
        ht = h;
    }

    public double volume()
    {
        return pi * rad * rad * ht;
    }

    void show()
    {
        super.show();
        System.out.println("Height = " + ht);
        System.out.println("Volume of the cylinder = " + volume());
    }
}
Explanation: CalVol extends the class Base (to reuse rad and show()) and implements the interface Data (so it must define volume(), which has to be public, and can use the constant pi = 3.142). The constructor passes the radius to the super class constructor with super(r) and stores the height. volume() returns pi x rad x rad x ht, the volume of a cylinder. show() calls super.show() to display the radius, then displays the height and the volume. Note: the question's stem says cylinder while the method description says sphere; the given formula pi x radius^2 x height is that of a cylinder. Tested with stub Base and Data classes: for radius 3.5 and height 10 the volume printed is 384.895.
2019 · 2 marks · Short answerOpen: What is an Interface? How is it different from a class?

Answer the following in short.

What is an Interface? How is it different from a class?
Show answer

Answer

AI
An interface in Java is a reference type that contains only abstract methods (method signatures with no body/implementation) and constants (implicitly public, static and final fields). It defines a contract that any class implementing it must fulfil by providing the actual code for every one of its methods. It is declared with the keyword 'interface' and a class uses it via the keyword 'implements'. Difference from a class: A class defines both the data (instance variables) and the actual implementation (method bodies) of its behaviour, and objects can be created directly from it using 'new'. An interface has no instance variables and no method bodies (only method headers) and cannot be instantiated on its own - it must be implemented by a class. Also, a class can extend only one superclass (single inheritance), whereas a class can implement any number of interfaces, so interfaces are used to achieve a form of multiple inheritance in Java.

Questions on other pages on Inheritance, Interfaces and Polymorphism

Other Computer Science chapters