Prashnikaप्रश्निका

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

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

Practise these questions with filters
2019 · 5 marks · ProgramOpen: A super class Record contains names and marks of the students in two different…

Write the program described below.

A super class Record contains names and marks of the students in two different single dimensional arrays. Define a sub class Highest to display the names of the students obtaining the highest mark. The details of the members of both the classes are given below: Class name : Record Data member/instance variable: n[ ] : array to store names m[ ] : array to store marks size : to store the number of students Member functions/methods: Record(int cap) : parameterized constructor to initialize the data member size = cap void readarray() : to enter elements in both the arrays void display( ) : displays the array elements Class name: Highest Data member/instance variable: ind : to store the index Member functions/methods: Highest(…) : parameterized constructor to initialize the data members of both the classes void find( ) : finds the index of the student obtaining the highest mark and assign it to ‘ind’ void display( ) : displays the array elements along with the names and marks of the students who have obtained the highest mark Assume that the super class Record has been defined. Using the concept of inheritance, specify the class Highest giving the details of the constructor(…),void find( ) and void display( ). The super class, main function and algorithm need NOT be written.
Show answer

Answer

AI
class Highest extends Record
{
    int ind;

    Highest(int cap)
    {
        super(cap);
        ind = 0;
    }

    void find()
    {
        ind = 0;
        for (int i = 1; i < size; i++)
        {
            if (m[i] > m[ind])
                ind = i;
        }
    }

    void display()
    {
        super.display();
        find();
        System.out.println("Student(s) with the highest mark:");
        for (int i = 0; i < size; i++)
        {
            if (m[i] == m[ind])
                System.out.println(n[i] + "\t" + m[i]);
        }
    }
}
Explanation: the parameterised constructor passes cap up to the superclass via super(cap) so that Record's constructor allocates the n[] and m[] arrays of the given size, then initialises ind. find() scans the inherited marks array m[] (indices 0 to size-1) and stores in ind the index of the largest mark. display() first calls super.display() to reuse the superclass's printing of all the array elements, then calls find() and prints the name(s) and mark of the student(s) with the highest mark (handling ties, i.e. more than one student sharing the highest mark). Tested (run for real, with a small Record superclass and main added only for testing) with names/marks {AMIT:78, SUMIT:95, ROHIT:95, MOHIT:60}: the program correctly identified and printed both SUMIT and ROHIT (95) as the students with the highest mark.
2018 · 5 marks · ProgramOpen: A super class Number is defined to calculate the factorial of a number. Define…

Write the program described below.

A super class Number is defined to calculate the factorial of a number. Define a sub class Series to find the sum of the series $S = 1! + 2! + 3! + 4! + \dots + n!$ The details of the members of both the classes are given below: Class name : Number Data member/instance variable: n : to store an integer number Member functions/methods: Number(int nn) : parameterized constructor to initialize the data member n=nn int factorial(int a) : returns the factorial of a number (factorial of $n = 1 \times 2 \times 3 \times \dots \times n$) void display( ) : displays the data members Class name: Series Data member/instance variable: sum : to store the sum of the series Member functions/methods: Series(…) : parameterized constructor to initialize the data members of both the classes void calsum( ) : calculates the sum of the given series void display( ) : displays the data members of both the classes Assume that the super class Number has been defined. Using the concept of inheritance, specify the class Series giving the details of the constructor(…),void calsum( ) and void display( ). The super class, main function and algorithm need NOT be written.
Show answer

Answer

AI
class Series extends Number
{
    long sum;

    Series(int nn)
    {
        super(nn);
        sum = 0;
    }

    void calsum()
    {
        sum = 0;
        for (int i = 1; i <= n; i++)
            sum = sum + factorial(i);
    }

    void display()
    {
        super.display();
        System.out.println("Sum of the series = " + sum);
    }
}
Explanation: the parameterised constructor Series(nn) passes nn up to the superclass Number's constructor via super(nn), which stores it in the inherited data member n, and initialises sum to 0. calsum() loops i from 1 to n and adds the inherited factorial(i) (i.e. i!) to sum on each iteration, computing $S = 1! + 2! + \dots + n!$. display() calls super.display() to reuse the superclass's printing of n, then prints sum. Tested (run for real, with a small Number superclass added only for testing purposes): for n=5, the program correctly computed sum = 1!+2!+3!+4!+5! = 1+2+6+24+120 = 153.
2017 · 5 marks · ProgramOpen: A super class Product has been defined to store the details of a product sold…

Write the program described below.

A super class Product has been defined to store the details of a product sold by a wholesaler to a retailer. Define a sub class Sales to compute the total amount paid by the retailer with or without fine along with service tax. Some of the members of both the classes are given below: Class name : Product Data member/instance variable: name : stores the name of the product code : integer to store the product code amount : stores the total sale amount of the product (in decimals) Member functions/methods: Product(String n, int c, double p) : parameterized constructor to assign data members name=n, code=c and amount = p void show( ) : displays the details of the data members Class name : Sales Data member/instance variable: day : stores number of days taken to pay the sale amount tax : to store the service tax (in decimals) totamt : to store the total amount (in decimals) Member functions/methods: Sales(…) : parameterized constructor to assign values to data members of both the classes void compute( ) : calculates the service tax @ 12·4% of the actual sale amount calculates the fine @ 2·5% of the actual sale amount only if the amount paid by the retailer to the wholesaler exceeds 30 days calculates the total amount paid by the retailer as (actual sale amount + service tax + fine) void show( ) : displays the data members of super class and the total amount Assume that the super class Product has been defined. Using the concept of inheritance, specify the class Sales giving the details of the constructor(…), void compute( ) and void show( ). The super class, main function and algorithm need NOT be written.
Show answer

Answer

AI
class Sales extends Product
{
    int day;
    double tax, totamt;

    Sales(String n, int c, double p, int d)
    {
        super(n, c, p);   // assigns name, code and amount of the super class
        day = d;
        tax = 0.0;
        totamt = 0.0;
    }

    void compute()
    {
        double fine = 0.0;
        tax = amount * 12.4 / 100;          // service tax @ 12.4%
        if (day > 30)
            fine = amount * 2.5 / 100;      // fine @ 2.5% only if paid after 30 days
        totamt = amount + tax + fine;
    }

    void show()
    {
        super.show();
        System.out.println("Total amount: " + totamt);
    }
}
Explanation: the constructor uses super(n, c, p) to set the data members of Product and then sets day. compute() calculates the service tax, the fine only when day > 30, and the total amount as amount + tax + fine. show() calls the super class show() and then displays totamt. Tested by running with amount 1000: 25 days gives total 1124.0 and 40 days gives total 1149.0.
2017 · 2 marks · Short answerOpen: What is an interface? How is it different from a class?

Answer the following question.

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 method declarations (no method bodies) and constants (public static final fields). It specifies what a class must do, and any class that implements it (using the keyword implements) must define all its methods. Difference from a class: a class contains data members and complete method definitions and can be instantiated with new, whereas an interface has no instance variables, no method bodies and cannot be instantiated. A class can inherit from only one class (extends) but can implement many interfaces, so interfaces give a form of multiple inheritance.

Questions on other pages on Inheritance, Interfaces and Polymorphism

Other Computer Science chapters