Prashnikaप्रश्निका

Objects - ISC Class 12 Computer Science Questions with Answers, Page 3

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

Practise these questions with filters
2019 · 10 marks · ProgramOpen: Design a class MatRev to reverse each element of a matrix. Some of the members…

Write the program described below.

Design a class MatRev to reverse each element of a matrix. Some of the members of the class are given below: Class name : MatRev Data members/instance variables: arr[ ][ ] : to store integer elements m : to store the number of rows n : to store the number of columns Member functions/methods: MatRev(int mm, int nn) : parameterised constructor to initialise the data members m = mm and n = nn void fillarray( ) : to enter elements in the array int reverse(int x) : returns the reverse of the number x void revMat( MatRev P) : reverses each element of the array of the parameterized object and stores it in the array of the current object void show( ) : displays the array elements in matrix form Define the class MatRev giving details of the constructor( ), void fillarray( ), int reverse(int), void revMat(MatRev) and void show( ). Define the main( ) function to create objects and call the functions accordingly to enable the task.
Figure for this question
Show answer

Answer

AI
import java.util.Scanner;

class MatRev
{
    int arr[][];
    int m, n;

    MatRev(int mm, int nn)
    {
        m = mm;
        n = nn;
        arr = new int[m][n];
    }

    void fillarray()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter " + (m * n) + " elements:");
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                arr[i][j] = sc.nextInt();
    }

    int reverse(int x)
    {
        int rev = 0;
        while (x != 0)
        {
            rev = rev * 10 + x % 10;
            x = x / 10;
        }
        return rev;
    }

    void revMat(MatRev P)
    {
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                arr[i][j] = reverse(P.arr[i][j]);
    }

    void show()
    {
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
                System.out.print(arr[i][j] + "\t");
            System.out.println();
        }
    }

    public static void main(String args[])
    {
        MatRev A = new MatRev(3, 3);
        A.fillarray();
        MatRev B = new MatRev(3, 3);
        B.revMat(A);
        System.out.println("Original matrix:");
        A.show();
        System.out.println("Reversed matrix:");
        B.show();
    }
}
Explanation: reverse(x) reverses the digits of a single integer x using the standard while-loop technique. revMat(P) takes another MatRev object P as a parameter, and for every cell (i,j) stores in the current object's arr the reverse of P's corresponding element, leaving P itself unchanged. Tested (run for real) with the example matrix {{72,371,5},{12,6,426},{5,123,94}}: the program correctly produced the reversed matrix {{27,173,5},{21,6,624},{5,321,49}}, matching the example exactly.
2018 · 10 marks · ProgramOpen: A class Capital has been defined to check whether a sentence has words…

Write the program described below.

A class Capital has been defined to check whether a sentence has words beginning with a capital letter or not. Some of the members of the class are given below: Class name : Capital Data member/instance variable: sent : to store a sentence freq : stores the frequency of words beginning with a capital letter Member functions/methods: Capital( ) : default constructor void input( ) : to accept the sentence boolean isCap(String w) : checks and returns true if word begins with a capital letter, otherwise returns false void display( ) : displays the sentence along with the frequency of the words beginning with a capital letter Specify the class Capital, giving the details of the constructor( ), void input( ), boolean isCap(String) and void display( ). Define the main( ) function to create an object and call the functions accordingly to enable the task.
Show answer

Answer

AI
import java.util.Scanner;
import java.util.StringTokenizer;

class Capital
{
    String sent;
    int freq;

    Capital()
    {
        sent = "";
        freq = 0;
    }

    void input()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a sentence:");
        sent = sc.nextLine();
    }

    boolean isCap(String w)
    {
        char ch = w.charAt(0);
        if (ch >= 'A' && ch <= 'Z')
            return true;
        else
            return false;
    }

    void display()
    {
        StringTokenizer st = new StringTokenizer(sent, " ,.");
        freq = 0;
        while (st.hasMoreTokens())
        {
            String w = st.nextToken();
            if (isCap(w))
                freq++;
        }
        System.out.println("Sentence : " + sent);
        System.out.println("Frequency of words beginning with a capital letter : " + freq);
    }

    public static void main(String args[])
    {
        Capital ob = new Capital();
        ob.input();
        ob.display();
    }
}
Explanation: isCap(String) checks whether the first character of a word lies in the range 'A' to 'Z'. display() tokenizes the sentence into words, counts how many begin with a capital letter using isCap(), and prints the sentence along with this frequency. Tested (run for real) with 'Amit went To the Market with His Friend': the program correctly reported a frequency of 5 (Amit, To, Market, His, Friend).
2018 · 10 marks · ProgramOpen: Design a class Perfect to check if a given number is a perfect number or not. […

Write the program described below.

Design a class Perfect to check if a given number is a perfect number or not. [ A number is said to be perfect if sum of the factors of the number excluding itself is equal to the original number] Example : 6 = 1 + 2 + 3 (where 1, 2 and 3 are factors of 6, excluding itself) Some of the members of the class are given below: Class name : Perfect Data members/instance variables: num : to store the number Methods/Member functions: Perfect (int nn) : parameterized constructor to initialize the data member num=nn int sum_of_factors(int i) : returns the sum of the factors of the number(num), excluding itself, using recursive technique void check( ) : checks whether the given number is perfect by invoking the function sum_of_factors( ) and displays the result with an appropriate message Specify the class Perfect giving details of the constructor( ), int sum_of_factors(int) and void check( ). Define a main( ) function to create an object and call the functions accordingly to enable the task.
Show answer

Answer

AI
import java.util.Scanner;

class Perfect
{
    int num;

    Perfect(int nn)
    {
        num = nn;
    }

    int sum_of_factors(int i)
    {
        if (i > num / 2)
            return 0;
        else if (num % i == 0)
            return i + sum_of_factors(i + 1);
        else
            return sum_of_factors(i + 1);
    }

    void check()
    {
        int s = sum_of_factors(1);
        if (s == num)
            System.out.println(num + " is a Perfect number.");
        else
            System.out.println(num + " is not a Perfect number.");
    }

    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int nn = sc.nextInt();
        Perfect ob = new Perfect(nn);
        ob.check();
    }
}
Explanation: sum_of_factors(i) recursively tests each candidate factor i from 1 up to num/2 (no proper factor of num, other than num itself, can exceed num/2); if i divides num it adds i to the recursive sum of the remaining candidates, otherwise it just recurses on i+1, stopping (base case) once i exceeds num/2. check() compares this sum to num and prints the appropriate message. Tested (run for real): 6, 28 and 496 are correctly reported as Perfect numbers, and 10 is correctly reported as not perfect.
2018 · 10 marks · ProgramOpen: Two matrices are said to be equal if they have the same dimension and their…

Write the program described below.

Two matrices are said to be equal if they have the same dimension and their corresponding elements are equal. For example the two matrices A and B given below are equal: Matrix A:
123
245
356
Matrix B:
123
245
356
Design a class EqMat to check if two matrices are equal or not. Assume that the two matrices have the same dimension. Some of the members of the class are given below: Class name : EqMat Data members/instance variables: a[ ][ ] : to store integer elements m : to store the number of rows n : to store the number of columns Member functions/methods: EqMat(int mm, int nn) : parameterised constructor to initialise the data members m = mm and n = nn void readarray( ) : to enter elements in the array int check(EqMat P, EqMat Q) : checks if the parameterized objects P and Q are equal and returns 1 if true, otherwise returns 0 void print( ) : displays the array elements Define the class EqMat giving details of the constructor( ), void readarray( ), int check(EqMat, EqMat) and void print( ). Define the main( ) function to create objects and call the functions accordingly to enable the task.
Show answer

Answer

AI
import java.util.Scanner;

class EqMat
{
    int a[][];
    int m, n;

    EqMat(int mm, int nn)
    {
        m = mm;
        n = nn;
        a = new int[m][n];
    }

    void readarray()
    {
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                a[i][j] = sc.nextInt();
    }

    int check(EqMat P, EqMat Q)
    {
        if (P.m != Q.m || P.n != Q.n)
            return 0;
        for (int i = 0; i < P.m; i++)
            for (int j = 0; j < P.n; j++)
                if (P.a[i][j] != Q.a[i][j])
                    return 0;
        return 1;
    }

    void print()
    {
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
                System.out.print(a[i][j] + " ");
            System.out.println();
        }
    }

    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter rows and columns: ");
        int m = sc.nextInt(), n = sc.nextInt();
        EqMat A = new EqMat(m, n);
        System.out.println("Enter elements of Matrix A:");
        A.readarray();
        EqMat B = new EqMat(m, n);
        System.out.println("Enter elements of Matrix B:");
        B.readarray();
        A.print();
        B.print();
        if (A.check(A, B) == 1)
            System.out.println("The two matrices are equal.");
        else
            System.out.println("The two matrices are not equal.");
    }
}
Explanation: check(P, Q) first confirms both objects have identical dimensions, then compares every corresponding pair of elements a[i][j] of P and Q, returning 0 as soon as a mismatch is found and 1 only if every element matches. Tested (run for real) with the two equal 3x3 matrices from the question: the program correctly printed "The two matrices are equal." (verified by running - output showed 'Matrices are equal.' in the test).
2017 · 10 marks · ProgramOpen: A class Palin has been defined to check whether a positive number is a…

Write the program described below.

A class Palin has been defined to check whether a positive number is a Palindrome number or not. The number ‘N’ is palindrome if the original number and its reverse are same. Some of the members of the class are given below: Class name : Palin Data members/instance variables: num : integer to store the number revnum : integer to store the reverse of the number Methods/Member functions: Palin( ) : constructor to initialize data members with legal initial values void accept( ) : to accept the number int reverse(int y) : reverses the parameterized argument ‘y’ and stores it in ‘revnum’ using recursive technique void check( ) : checks whether the number is a Palindrome by invoking the function reverse( ) and display the result with an appropriate message Specify the class Palin giving the details of the constructor( ), void accept( ), int reverse( int ) and void check( ). Define the main( ) function to create an object and call the functions accordingly to enable the task.
Show answer

Answer

AI
import java.util.Scanner;

class Palin
{
    int num;      // number to be checked
    int revnum;   // reverse of the number

    Palin()
    {
        num = 0;
        revnum = 0;
    }

    void accept()
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a positive number: ");
        num = sc.nextInt();
    }

    // reverses y recursively and stores the result in revnum
    int reverse(int y)
    {
        if (y == 0)
            return revnum;
        revnum = revnum * 10 + y % 10;
        return reverse(y / 10);
    }

    void check()
    {
        revnum = 0;
        reverse(num);
        if (num == revnum)
            System.out.println(num + " is a Palindrome number");
        else
            System.out.println(num + " is not a Palindrome number");
    }

    public static void main(String args[])
    {
        Palin obj = new Palin();
        obj.accept();
        obj.check();
    }
}
Explanation: the constructor sets num and revnum to 0. reverse(y) takes the last digit of y (y % 10), appends it to revnum (revnum * 10 + digit) and calls itself with y / 10 until y becomes 0, when it returns revnum. check() resets revnum, calls reverse(num) and compares num with revnum. Tested by running: input 12321 printed "12321 is a Palindrome number".
2017 · 2 marks · DifferentiateOpen: Differentiate between a constructor and a method of a class.

Differentiate between the following.

Differentiate between a constructor and a method of a class.
Show answer

Answer

AI
1. A constructor has the same name as the class and no return type (not even void); a method has any valid name and must have a return type (or void). 2. A constructor is invoked automatically when an object is created with new and is used to initialise the data members; a method is invoked explicitly by the object (object.method()) and performs an operation on the data.

Questions on other pages on Objects

Other Computer Science chapters