← All resources

Class 10 ICSE Computer Question Paper – St. Xavier's solved.

SECTION B


Question 4

Define a class Numbers describes as below:

Data Members: n1, n2, n3, maximum and average

Member Methods:

  1. A parameterised constructor to initialise the data members.
  2. To compute the average and the maximum of the three numbers
  3. To display the details

Write the main method to create an object of the class and call the member methods

Solution: 

import java.util.*;
public class Numbers
{
    int n1, n2, n3, maximum;
    double average; //Data Members
    Numbers(int a, int b, int c)
    {       //Parameterised Constructor
        n1 = a;
        n2 = b;
        n3 = c;
    }

    void compute()
    {
        average = (n1+n2+n3)/3.0;   //Average
        maximum = n1;               //Finding maximum
        if(maximum<n2)
            maximum = n2;
        if(maximum<n3)
            maximum = n3;
    }

    void display()
    {
        System.out.println("N1 = "+n1);
        System.out.println("N2 = "+n2);
        System.out.println("N3 = "+n3);
        System.out.println("Maximum = "+maximum);
        System.out.println("Average = "+average);
    }

    public static void main()
    {
        Scanner sc = new Scanner(System.in);
        int p, q, r;
        System.out.println("Enter 3 numbers: ");
        p = sc.nextInt();
        q = sc.nextInt();       //Taking 3 numbers as input
        r = sc.nextInt();
        Numbers obj = new Numbers(p,q,r);
        //Creating object and calling parameterised constructor
        obj.compute();      //Calling respective functions
        obj.display();
    }
}

Question 5:

Using a switch statement and the concept of “Function Overloading”, write a menu driven program to do the following as per the user’s choice:

  • Accept an integer and find and display the sum of its digits
  • Accept a word and find and display the number of consonants in it.

For an incorrect choice an appropriate error message should be printed.

import java.util.*;
public class FunctionOverload
{
    void overload(int n)
    {
        int d, s=0;
        while(n!=0)
        {
            d = n%10;
            n = n/10;
            s = s+d;
        }
        System.out.println("Sum: "+s);
    }
    void overload(String s)
    {
        s = s.toUpperCase();        //Eliminating 2 case issues
        int i, l = s.length(), a = 0, c=0;
        char ch;
        for(i=0;i<l;i++)
        {
            ch = s.charAt(i);
            if(ch>='A'&&ch<='Z')    //Counting alphabets
            a++;
            if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
            c++;
        }
        System.out.println("Number of consonants: "+(a-c));
    }
    public static void main()
    {
        Scanner sc = new Scanner(System.in);
        int ch, x;  //Declaring Necessary Variables
        String st;
        FunctionOverload obj = new FunctionOverload();      //Creating object
        System.out.println("MENU");
        System.out.println("1. To find the sum of digits of a number.");
        System.out.println("2. To find the number of consonants in a string.");
        System.out.println("Enter your choice: ");
        ch = sc.nextInt();
        switch(ch)
        {
            case 1:
            System.out.println("Enter a number: ");
            x = sc.nextInt();
            obj.overload(x);
            break;      //To prevent fall through
            case 2:
            System.out.println("Enter a string: ");
            st = sc.nextLine();
            obj.overload(st);
            break;
            default:
            System.out.println("Wrong Choice.");
        }
    }
}

Question 6

Write a program to enter 2 strings. Find and remove all the common characters from both the strings. Print both the new strings.

import java.util.*;
public class RemoveCommon
{
    String remove(String x, char c)
    {
        int i, l = x.length();
        String st="";
        for(i=0;i<l;i++)
        {
            if(x.charAt(i)!=c)
                st=st+x.charAt(i);
        }
        return st;
    }

    public static void main()
    {
        Scanner sc = new Scanner(System.in);
        RemoveCommon obj = new RemoveCommon();
        String s1, s2;
        int i=0, j=0, l1, l2;
        char c1, c2;
        System.out.println("Enter 1st string: ");
        s1 = sc.nextLine();
        System.out.println("Enter 2nd string: ");
        s2 = sc.nextLine();
        l1 = s1.length();
        l2 = s2.length();
        while(i<l1)
        {
            c1 = s1.charAt(i);
            while(j<l2)
            {
                c2 = s2.charAt(j);
                if(c1==c2)      //Finding common characters
                {
                    s1 = obj.remove(s1, c1);        //removing common characters
                    s2 = obj.remove(s2, c1);        //From both strings
                    l1 = s1.length();               //Finding new length
                    l2 = s2.length();
                    i=0;
                    break;
                }
                j++;
            }
            j=0;
            i++;
        }
        System.out.println("S1: "+s1);      //Output
        System.out.println("S2: "+s2);
    }
}

Question 7

Write a program in JAVA to accept 100 natural numbers in a single dimensional array. The user enters a number and using the binary search technique, the program should print the position of that number. Print an appropriate message if the number is not present in the array. The program should allow the user to search for as many numbers as he wishes. To end the program, -1 should be entered by the user. You may assume that the array is sorted.

import java.util.*;
public class BinarySearch
{
    public static void main()
    {
        Scanner sc = new Scanner(System.in);
        int a[] = new int[100], i, max, min, mid, pos=-1, s, ch;
        //Declaring necessary variables
        System.out.println("Enter the values for the array in ascending order: ");
        for(i=0;i<100;i++)
        {       //Input 100 values for the array in ascending order
            //As binary serach can work only on a sorted array
            a[i] = sc.nextInt();
        }

        do
        {
            pos=-1;
            System.out.println("Enter the value to search: ");
            s = sc.nextInt();
            min = 0;        //Minimum Index
            max = 99;       //Maximum Index
            while(min<=max)
            {
                mid = (min+max)/2;
                if(s<a[mid])        //Implementing divide and conquer algorithm
                    max = mid-1;
                else if(s>a[mid])
                    min = mid+1;
                else
                {
                    pos = mid;  /*value of pos changes only when the search values
                    is found*/
                    break;
                }
            }
            if(pos==-1)
                System.out.println("Value is not present in the array.");
            else
                System.out.println("Value is present at: "+(pos+1));        //Position is 1 more than index
            System.out.println("Do you want to search for more? Enter -1 to discontinue or any other number to continue:" );
            ch = sc.nextInt();
        }while(ch!=-1);
    }
}

Question 8

Write a function print() that accepts two numbers and print all the numbers between them, excluding the two numbers. Using this function, write a program to print all the non-fibonacci numbers upto 100.

Fibonacci Series- 1 1 2 3 5 8 13 …

public class NonFibo
{
    void print(int a, int b)
    {
        int i;
        for(i=a+1;i<b;i++)
        {
            System.out.println(i);  //Printing the values as instructed
        }
    }
    public static void main()
    {
        NonFibo obj = new NonFibo();    //Creating object to use instance member in static method
        int i, a=0, b=1, c;     //Taking necessary variables
        while(a<=100)
        {
            if(b<100)
            obj.print(a, b);        //As after 89 next value is 144.
            else
            obj.print(a, 101);
            c = a+b;            //Generating the fibonacci series
            a = b;
            b = c;
        }
    }
}

Question 9

Write a program to accept a string. Use a function to find out if it contains an odd or even number of characters. Display “STRING REJECTED” if the number of characters is even. If it is odd, display the string in the following way:

Eg:- “COMPUTERS”

OUTPUT:

Capture12

import java.util.*;
public class StringPattern
{
    public static void main()
    {
        Scanner sc = new Scanner(System.in);
        String st;
        char ch;
        int i, l, j;
        System.out.println("Enter the string: ");
        st = sc.nextLine();     //Taking String as Input
        l = st.length();        //Calculating length
        if(l%2==0)
        {                   //If length is even then string is rejected
            System.out.println("String Rejected");
        }
        else
        {
            System.out.println("Output: ");
            for(i=1;i<=l/2+1;i++)
            {       //Printing the pattern
                for(j=0;j<l;j++)
                {
                    ch = st.charAt(j);
                    if(j>=i-1&&j<=l-i)
                    System.out.print(ch);
                    else
                    System.out.print(" ");
                }
                System.out.println();
            }
        }
    }
}

 Solution to Theory Section is coming soon. 

Contact us on #9831913909