ISC 2012 Practical Paper

COMPUTER SCIENCE

Paper – 2

(PRACTICAL)

(Reading Time: 15 minutes)

 (Planning Session AND Examination Session: Three Hours)

————————————————————————————————————

The total time to be spent on the Planning and the Examination Session is Three hours.

After completing the Planning Session, the candidate may begin with the Examination Session.

A maximum of 90 minutes is permitted to begin the Examination Session.

(Maximum Marks: 80)

————————————————————————————————————

As it is a practical examination the candidate is expected to do the following:

  1. Write an algorithm for the selected problem.    [10]
  2. Algorithm should be expressed clearly using ay standard scheme such as pseudo code or in steps which are simple enough to be obviously computable.
  3. Write a program in JAVA language. The program should follow the algorithm and should             [20]
    be logically and syntactically correct.
  4. Document the program using mnemonic names / comments, identifying and clearly                        [10]
    describing the choice of data types and meaning of variables.
  5. Code / Type the program on the computer and get a printout (hard copy). Typically this                 [10]
    should be a program that compiles and runs correctly.
  6. Test run the program on the computer using the given sample data and get a printout of                 [20]
    the output in the format specified in the problem.
  7. Viva-Voce on the Selected Problem.     [20]

Solve any one of the following Problems:

 Question 1.

A prime palindrome integer is a positive integer (without leading zeros) which is prime as well as a palindrome. Given two positive integers m and n, where m<= n, write a program to determine how many prime-palindrome integers are there in the range between m and n (both inclusive) and output them.

The input contains two positive integers m and n where m>=100 and n<= 3000. Display number of prime palindrome integers in the specified range along with their values in the format specified below:

Test your program with the sample data and some random data:

Example 1:

INPUT:

M=100

N=1000

OUTPUT: The prime palindrome integers are:

101,131,151,181,191,313,351,373,383,727,757,787,797,919,929

Frequency of prime palindrome integers: 15

Example 2:

INPUT:

M=100

N=5000

OUTPUT: Out of Range

import java.io.*;

class PrimePal
{
    public void showPrimePal() throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int m,n, c=0;
        System.out.println(“Enter the Lower Limit:”);
        m=Integer.parseInt(br.readLine());
        System.out.println(“Enter the Upper Limit:”);
        n=Integer.parseInt(br.readLine());
        if(m>n || n>3000)
            System.out.println(“Out of Range.”);
        else
        {
            System.out.println(“The Prime Palindrome integers are:”);
            while(m<=n)
            {
                if(checkPrimePal(m))
                {
                    if(c==0)
                        System.out.print(m);
                    else
                        System.out.print(“, “+m);
                    c++;
                }
                m++;
            }
            System.out.println(“\nFrequency of Prime Palindrome integers: “+c);
        }
    }
    boolean checkPrimePal(int x)
    {
        boolean flag=false;
        int c=0, temp=x;
        int rev=0,rem=0;
        for(int i=1;i<=x;i++)
        {
            if(x%i==0)
                c++;
        }
        if(c<=2)
        {
            while(x!=0)
            {
                rem=x%10;
                rev=rev*10+rem;
                x/=10;
            }
            if(rev==temp)
                flag=true;
        }
        return flag;
    }
    public static void main(String args[]) throws IOException
    {
        PrimePal ob=new PrimePal ();
        ob.showPrimePal();
    }
}

Question 2.

 Write a program to accept a sentence as input. The words in the string are to be separated by a blank. Each word must be in upper case. The sentence is terminated by either “.”,”!” or “?”. Perform the following tasks:

(i)  Obtain the length of the sentence. (measured in words)

(ii) Arrange the sentence in alphabetical order of the words.

Test your program with the sample data and some random data:

Example 1:

INPUT: NECESSITY IS THE MOTHER OF INVENTION.

OUTPUT:

Length: 6

Rearranged Sentence:

INVENTION IS MOTHER NECESSITY OF THE

Example 2:

INPUT: BE GOOD TO OTHERS.

OUTPUT:

Length: 4

Rearranged Sentence: BE GOOD OTHERS TO

import java.io.*;
class WordsInSentence
{
    public void take() throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String str, words[], stk=””;
        int i,j,c=0,flag, len;
        char ch;
        while(true)
        {
            flag=0;
            System.out.println(“Enter the sentence:”);
            str=br.readLine();
            len= str.length();
            words=new String[len];
            for(i=0;i
            {
                if(Character.isLowerCase(str.charAt(i)))
                {
                    flag=1;
                    break;
                }
            }
            if (flag==0)
                break;
            else
                System.out.println(“Enter the sentence again with all uppercase letters”);
        }
        i=0;
        while(i<len)
        {
            ch=str.charAt(i);
            if(ch==’ ‘|| ch==’.’ || ch==’?’ || ch==’!’)
            {
                words[c]=stk;
                c++;
                i++;
                stk=””;
            }
            else
            {
                stk+=ch;
                i++;
            }
        }
        for(i=0;i<c;i++)
        {
            for(j=0;j<c-1;j++)
            {
                if((words[j].compareTo(words[j+1]))>0)
                {
                    stk=words[j];
                    words[j]=words[j+1];
                    words[j+1]=stk;
                }
            }
        }
        System.out.println(“Length= “+c);
        System.out.println(“\nRearranged Sentence:\n”);
        for(i=0;i<c;i++)
            System.out.print(words[i]+” “);
    }
    public static void main(String args[]) throws IOException
    {
        WordsInSentence ob=new WordsInSentence();
        ob.take();
    }
}

Question 3.

Write a program to declare a matrix A [][] of order (MXN) where ‘M’ is the number of rows and ‘N’ is the number of columns such that both M and N must be greater than 2 and less than 20. Allow the user to input integers into this matrix. Perform the following tasks on the matrix:

  1. Display the input matrix
  2. Find the maximum and minimum value in the matrix and display them along with their position.
  3. Sort the elements of the matrix in ascending order using any standard sorting technique and rearrange them in the matrix.
  4. Output the rearranged matrix.

Test your program with the sample data and some random data:

Example 1.

INPUT:

M=3

N=4

Entered values: 8,7,9,3,-2,0,4,5,1,3,6,-4

 OUTPUT:

Original matrix:

8  7  9  3

-2  0  4  5

1  3  6  -4

Largest Number: 9

Row: 0

Column: 2

Smallest Number: -4

Row=2

Column=3

Rearranged matrix:

-4  -2  0  1

3  3  4  5

6  7  8  9

 

import java.io.*;

class Matrix
{
public void take()throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int arr[][],m,n;
int g,h,i,j,max,min,maxr,maxc,minr,minc;

while(true)
{
System.out.println(“\nEnter the number of rows :”);
m=Integer.parseInt(br.readLine());
System.out.println(“\nEnter the number of columns:”);
n=Integer.parseInt(br.readLine());
if(m<2 || n<2 || m>20 || n>20)
System.out.println(“\nEnter the number of rows and columns”);
else
break;
}
arr=new int[m][n];
for(i=0;i<m; i++)
{
for(j=0;j<n;j++)
{
System.out.println(“\nEnter Value:”);
arr[i][j]=Integer.parseInt(br.readLine());
}
}
max=arr[0][0];
min=arr[0][0];
maxr=0;
minr=0;
maxc=0;
minc=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(arr[i][j]>max)
{
max=arr[i][j];
maxr=i;
maxc=j;
}
else if(arr[i][j]< min)
{
minr=i;
minc=j;
min=arr[i][j];
}
}
}
System.out.println(“\nOriginal Matrix\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(arr[i][j]+” “);
}
System.out.println();
}

System.out.println(“\nMaximum Value=”+max);
System.out.println(“\nRow=”+maxr);
System.out.println(“\nColumn=”+maxc);
System.out.println(“\nMinimum Value=”+min);
System.out.println(“\nRow=”+minr);
System.out.println(“\nColumn=”+minc);

for(g=0;g<m;g++)
{
for(h=0;h<n;h++)
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(arr[g][h]< arr[i][j])
{
min=arr[g][h];
arr[g][h]=arr[i][j];
arr[i][j]=min;
}
}
}
}
}
System.out.println(“\nSorted Array\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(arr[i][j]+” “);
}
System.out.println();
}

}
public static void main(String args[]) throws Exception
{
Matrix ob=new Matrix();
ob.take();
}
}

Leave a Comment