Thursday, 7 January 2016

ISC Computer Science Class XII 2015 Part I Theory (Solved)

                    This is the section where all questions are compulsory . Each question individually carries 1 or 2 marks . Answers must be to the point without giving much unnecessary details. 


Question 1

a) Simplify : (A+C).(A+A.D)+A.C+C                            [2]

Sol.  (A+C).(A+A.D)+A.C+C
       =(A+C).A+A.C+C
       =(A.A+A.C)+(A.C+C)
      = (A+A.C)+(A.C+C)
      =A+C

(Since the  name of the laws used is not specifically asked for you can excuse yourself from being so meticulous . Just for your knowledge in line 2 Redundance Law and in line 4 Idempotent and Redundance Laws are used. )


b) Draw a logic circuit for (A+B).(C+D).C                             [2]

Sol. 

c ) Verify the following proposition with the help of a truth table                      
                       
                            P   (~P  Q) = P    Q     

Sol. 


d)  State De Morgan's Law and verify it using a truth table .           [2]

Sol.  De Morgan's Law states that the complement of sum of the variables is same as the product of the individual complements of the variables and vice versa.
                              i) (A+B)'=A'.B'
                             ii) (A.B)'= A'+B'

Truth table for the proof of i) is as follows



(Since only 2 marks are allotted for the question , proof for any one of the equations is sufficient. I leave the truth table for proving the second equation as an exercise for you . )

e) Answer the questions related to the circuit below : 
i) Give the output if X=1 and Y=0
ii) Name the basic gates represented by the above diagram.
                                                                                                                            [2]

Sol  . i)    1                                                                                                                                             
           ii)   XOR gate                                                                                                                                                                                                                                                                                                     
Question 2.

a) Define computational complexity . Calculate the complexity using Big 'O' notation for the following code segment 

for(int k=0;k<n;k++)
        s+=k;                                                                                                                       [2]

Sol.  Computational complexity gives the practical difficulty of solving problems as measured by time , number of steps or memory space required about finite combinational objects . It is the growth rate or measurement of an algorithm.

Complexity of the above piece of code is O(n).

b) Covert the following infix notation into postfix form 
        X+(Y-Z)+((W+E)*F)/J                                                                                          [2]

Sol.     
             Stack                                                                 Output
               +                                                                         X
               +(                                                                        XY
               +(-                                                                       XYZ
               + +                                                                       XYZ-
               +(                                                                         XYZ-+
               +((                                                                        XYZ-+W
               +((+                                                                     XYZ-+WE
               +(                                                                         XYZ-+WE+
               +(*                                                                       XYZ-+WE+F
                + /                                                                       XYZ-+WE+F*J
                +                                                                          XYZ-+WE+F*J/
                 null                                                                     XYZ-+WE+F*J/+

c) Differentiate between this and super keyword.                                  [2]

Sol.     "this" keyword is used to refer current class instance variable. 
            "super" keyword on the other hand is used to refer immediate parent class instance variable.


d) The array D[-2...10][3...8] contains double type elements.If base address is 4110, find the address of D[4][5] when array is stored in Column Major Wise.                                                 [2]
                
Sol. Column Major Wise memory address of an element A[I][J] is given as                                        A[I][J] = B+W*[(I-Lr)+M*(J-Lc)]

Here B=4110
         W=64
         I=4
         Lr= -2
         J=5
         Lc=3
         M= 10-(-2) +1 
             =13

hence , D[4][5] = 4110 + 64*(4-(-2))+13*(5-3)
                          = 4110 + 64*(6+ 26)
                          = 4110 + 64*32
                          = 4110 +2048
                          =  6158

e) State any two characteristics of a binary tree.                                  [2]

Sol . i) Binary tree is a non linear data structure where every node contains an unique  value. 
       ii) Every node contains at the most two sub nodes . Therefore a binary node at level d has                         maximum 2d  nodes . 


Question 3 

a) The following function is a part of some class . Assume 'x' and 'y' are positive integers greater than 0 . Answer the  given questions along with dry run or working. 

void somefun(int x, int y )
{
    if (x>1)
      { 
         if(x%y==0)
           {
              System.out.print(y+" ");
                somefun(x/y,y)
            }
        else
              somefun(x,y+1)
         }
}

i) what will be returned by somefun(24,2)                                                            [2]

Sol.

Function
Condition 1
Condition
Checking Value
Output
Somefun(24,2)
24>1 True
24%2==0
True
2
Somefun(24/2,2)
12>1 True
12%2==0
True
2
Somefun(12/2,2)
6>1 True
6%2==0
True
2
Somefun(6/2,2)
3>1 True
3%2==0
False
Somefun(3,2+1)
Somefun(3,3)
3>1 True
3%3==0
True
3
Somefun(3/3,3)
1>1 False




Output : 2 2 2 3

ii)  What will be returned by somefun(84,2)                                                                [2]

Sol.

Function
Condition 1
Condition
Checking Value
Output
Somefun(84,2)
84>1 True
84%2==0
True
2
Somefun(84/2,2)
42>1 True
42%2==0
True
2
Somefun(42/2,2)
21>1 True
21%2==0
False
Somefun(21,2+1)
Somefun(21,3)
21>1 True
21%3==0
True
3
Somefun(21/3,3)
7>1 True
7%3==0
False
Somefun(7.3+1)
Somefun(7,4)
7>1 True
7%4==0
False
Somefun(7.4+1)
Somefun(7,5)
7>1 True
7%5==0
False
Somefun(7,5+1)
Somefun(7,6)
7>1 True
7%6==0
False
Somefun(7,6+1)
Somefun(7,7)
7>1 True
7%7==0
True
7
Somefun(7/7,7)
1>1 False




Output : 2 2 3 7

iii) State in one line what does the function somefun(x,y) do apart from recursion.                [1]

Sol.It generates Prime Factors. 



b) The following function is a class which checks of a positive integer is an Armstrong number by returning true or false . (A number is said to be armstrong if the sum of cubes of all it's digits is equal to the original number). The function does not use modulus to extract digits . There are some places in the code marked by ?1? , ?2? , ?3? , ?4? , ?4? , ?5? which may be replaced by a statement / expression so that the function works properly . 
boolen Armstrong(int N)
{
    int sum =  ?1?;
    int num = N;
    while (num>0)
        {
               int f = num/10;;
               int s = ?2?;
               int digit = num - s;
               sum + = ?3?;
               num = ?4?;
        }

   if(?5?)
        return true;
   else 
        return false;
}

i)  What is the expression at ?1?
ii) What is the expression at ?2?
iii) What is the expression at ?3?
iv) What is the expression at ?4?
v) What is the expression at ?5?                                                                   [1*5=5]


i) 0
ii) f*10;
iii) Math.pow(digit,3);  OR digit*digit*digit;
iv)f;
v)sum==N

                                                                                                                                                 



Friday, 1 January 2016

ISC Specimen Computer Science Paper Group C (Part IV)

             Wish you all a happy 2016 .  In this section the Java topics from Class XII will be covered . So questions based on concepts of inheritance , string , binary tree will be framed in this section . Here goes an example of a Group C section . 

Question 11
In a computer game, a vertical column and a pile of rings are displayed. The objective of the game is
to pile up rings on the column till it is full. It can hold 10 rings at the most. Once the column is full,
the rings have to be removed from the top till the column is empty and the game is over. Define the
class RingGame with the following details:
Class Name : RingGame
Data members
ring[] : array to hold rings (integer)
max : integer to hold maximum capacity
of
ring array
upper : integer to point to the uppermost
element
Member functions
RingGame(int m) : constructor to initialize, max = m &
upper to -1.
void jump-in(int) : adds a ring to the top of the
column, if
possible, otherwise, displays a
message “Column full. Start
removing rings”.
void jump-out() : removes the ring from the top, if
column is not empty, otherwise
outputs a message,
“Congratulations the game is over “.
Specify the class RingGame giving details of the constructor and functions void jump-in(int) and void
jump-out(). Also define the main function to create an object and call methods accordingly to enable
the task.

[ 10 ]

import java.util.*;
import java.lang.*;
import java.io.*;


  class RingGame
    {
     
     
       int max;
       int[] ring;
        int upper;

        RingGame(int m)
        {
            max = m;
            ring = new int[m];
            upper=-1;
        }

      void jumpin(int i)
        {
            if (upper==max-1)   // -1 because the indices are 0 1 2 for 3 elements . upper indicates the                                                    //    index so it starts from 0 . index==max will throw array out of bounds                                               //    exception .
            {
                System.out.println("Column full ! Start removing rings.");

            }

            else
            {
                ring[++upper] = i;  // pre incremetal operator ++upper used so that the first element is                                                           //   inserted at index 0 .
             
            }
        }

      public  void jumpout()
        {
         
            if (upper == -1)   // index of an array usually starts from 0 so -1 index indicates there is no                                               //  other element in this array.
            {
                System.out.println("Congratulations ! the game is over .");

            }

            else
            {
               ring[upper]=ring[upper--];  // element last inserted is popped out
             
            }
           
        }


   
   public static void main (String[] args)
{
       
           RingGame obj = new RingGame(3);
           
            obj.jumpin(5);
            obj.jumpin(2);
            obj.jumpin(1);
            obj.jumpin(7);  // will not be inserted
         
            obj.jumpout();
            obj.jumpout();
            obj.jumpout();
            obj.jumpout();   // game is over

}

  }

Question 12.
A class Author contains details of the author and another class BookList contains details of the books
written by him. The details of the 2 classes are given below:
Class name : Author
Data members
authorno : stores the author’s number
name : stores the author’s name
Member functions
Author() : default constructot
Author(…..) : parameterized constructor to assign
values to author number and name.
void show() : to display the author’s details.
Class name : Booklist
Data members
bookno : long type variable to store the book
number.
bookname : stores the book name
price : float variable to store price
edition : integer type variable to store the
edition number
Member functions
Booklist(…..) : parameterized constructor to assign
values to data members of both the
classes.
void show() : to display all the details.
6
Specify the class Author giving details of the constructors and member function void show(). Using
the concept of inheritance, specify the class Booklist giving details of the constructor and the
member function void show(). Also define the main function to create an object and call methods
accordingly to enable the task.
[ 10 ]





import java.util.*;
import java.lang.*;
import java.io.*;



     class Author
    {
        int authorno;
        String name;
        public Author()
        {
             // If you donot define this constructor , null value will be assigned to name and author                          //number will be 0 by default.
        }

        public Author(int num, String name)
        {
            authorno = num;
            this.name = name;
        }

        public void show()
        {
            System.out.println("The Author's Details are as follows");
            System.out.println("Author Name - " + name + "  & Author Number is  " + authorno);

        }
    }

     class BookList extends Author
    {
        long bookno;
        String bookname;
        float price;
        int edition;
        public BookList(long no, String name, float price, int ed)
        {
            bookno = no;
            bookname = name;
            this.price=price;
            edition = ed;
        }

        public void show()
        {
            System.out.println("The Details of the Book is as follows");
            System.out.println("Book No. - " + bookno + "  Book Name : " + bookname + " Price is " + price + " INR  Edition No. " + edition);

        }
  public static void main(String[] args)
  {
            Author a;   // a is used as a reference here
            a= new Author(1,"Dan Brown");
            a.show();    // displays the method of the parent class
            a = new BookList(13324, "Digital Fortress", 599, 1);    // a is referenced to the child class BookList
            a.show(); // displays the method of the child class
         
  }
}

Output is as follows 
=================
The Author's Details are as follows
Author Name - Dan Brown  & Author Number is  1
The Details of the Book is as follows
Book No. - 13324  Book Name : Digital Fortress Price is 599 INR  Edition No. 1






Question 13.
a) A Linked List is formed from the objects of the class,
class Node
{
intnum;
Node next;
}
Write an algorithm of a method for inserting a node in the end of a list. The method
declaration is given below:
voidinsertnode(Node start) [ 4 ]

Sol.
      void insertnode(Node start) {
            if (start == null)
                  return;
            else {
                  start.next = null;
                  if (head == null) {
                        head = start;
                        tail = start;
                  } else {
                        tail.next = start;
                        tail = start;
                  }
            }
      }

b) State the complexity of the following algorithms:

i) Linear Search
ii) Binary search
iii) Selection sort
[ 3 ]


Sol.
i) O(n)
ii) O(log n) 
iii) O(n2)


c) List the nodes in the tree given below using :
i) Preorder Traversal
ii) Postorder Traversal
iii) Inorder Traversal




Sol.

Preorder Traversal -   FGIJKLHPMON
Postorder Traversal -  IKLJGONMPHF
Inorder Traversal -     IGKJLFPHOMN



(Note : If you like to save time it is best to attempt the last 2 questions in this sample paper. The inheritence question hardly has anything to code and the last question will take less than 5 min to answer provided you know the linked list algorithm)

Wednesday, 23 December 2015

ISC Specimen Computer Science Paper Section B (Part III)

In this section you are required to answer two questions out of three . You are to create a class where the data members to be stored and methods/member functions to be defined are clearly instructed in the question. You just have to implement the same using Java syntax and have to define a main  which creates the class object and  calls the required function/s. In this section you will be tested on your basic programming skill in Java like constructor , recursion etc . Many of the functions will require you to program mathematical series like the factorial series , arithmetic or geometric progression sequence , sum of squares or cubes of n natural numbers and  so on. Here is a sample of the section.               

                                                     
Question 8.
A perfect square is an integer which is the square of another integer. For example, 4,9,16… are
perfect squares. Design a class Perfect with the following description:
Class Name : Perfect
Data members
n : stores an integer number
Member functions
Perfect() : default constructor
Perfect(int) : parameterized constructor to assign value
                      to n
void perfect_sq() : to display the first 5 perfect squares larger
                               than
                              ‘n’ (if n=15, the next 3 perfect squares are
                                 16,25,36).
                                 
void sum_of() : to display all combinations of consecutive
                           integers whose sum is = n. (The number
                          n=15 can be expressed as :
                        1 2 3 4 5
                        4 5 6
                        7 8
Specify the class Perfect giving details of the constructors, void perfect_sq() and void sum_of(). Also
define the main function to create an object and call methods accordingly to enable the task.
[ 10 ]


Sol.
import java.util.*;
class  Perfect
{
int n;
Perfect()  //Default Constructor
{
n=0;
}
        Perfect (int n)  //Parameterised constructor
     {
    this.n=n;         // Since I assigned the same name of variable as the member function , this operator                            is  used . You are free to use any other name like num

        }

void perfect_sq()
{
    int k ;
    for(int i=1; i<=n/2; i++)
     {
          if(i*i==n)
      {
              k=i+1;
              break;
       }
          else if(i*i>n)
          {
               k=i;
              break;
            }
         else
               continue;
      }
System.out.println("The next five perfect nos are as follows");

       StringBuilder sb= new StringBuilder();
     for (int i=k;i<=k+5;i++)
      {
     
     sb.append(i*i).append("");
     

        
       }

System.out.println( sb.ToString());
}

void sum_of()
{
System.out.println("The combination of the consecutive integers whose value is "+n);
for (int i = 1; i < n; i++)
 {
StringBuffer sb = new StringBuffer();
int sum = i;
sb.append(i).append(" ");
for (int j = i + 1; sum < n; j++)
 {
sb.append(j).append(" ");
sum = sum + j;
if (sum == n)
    System.out.println(sb.toString());

}
}


}
}

class call_main
{
   public static void main(String args[])
      {
Perfect obj = new Perfect();  //Default constructor is called here. n value set to 1.
obj.perfect_sq();
obj.sum_of();
Perfect ob = new Perfect ob(15);   //Calls the parameterized constructor
obj.perfect_sq();
obj.sum_of();
}
}


Dry Run :

The next five perfect nos are as follows
16 25 36 49 64















Question 9.
A class RecFact defines a recursive function to find the factorial of a number. The details of the class
are given below:
Class Name : RecFact
Data Members
n : stores the number whose factorial is
          required.
r : stores an integer
Member functions
RecFact() : default constructor
voidreadnum() : to enter values for n and r.
int factorial(int) : returns the factorial of the number using
                               the
                              recursive technique.
voidfactseries() : to calculate and display the value of
                                                                                                          n!
                                                                                                   -----------------
                                                                                                     r! * (n-r)!
Specify the class RecFact giving the details of the constructor and member functions void readnum(),
int factorial(int) and void factseries(). Also define the main function to create an object and call
methods accordingly to enable the task.                                                                                          [10]

Sol.
import java.util.*;

class RecFact
{
int n;
int r;
RecFact()
  {
      n=0;
      r=0;
   }
void readnum()
  {
 
   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
   System.out.println("Input an integer whose factorial is to be computed");
    n = Integer.ParseInt(br.readLine());
   System.out.println("Input an integer");
   r= Integer.ParseInt(br.readLine());
   }

int factoral(int fact)
{
     int retfact=1;
     int ifact=fact;
     if(ifact==0 || ifact==1)
     {
         return retfact;
      }

     else
   {
       retfact=ifact*factorial(ifact-1);
    }
     
}
void factseries()
   {
       int numerator= factorial(n);
       int factr = factorial(r);
       int diffact= factorial(n-r);
       int denom= factr*diffact;
      float result= (float) numerator/denom ;
     System.out.println("The value of the series is : " +result);
 
    }

}

class FactMain
{
      public static void main(String args[])
      {
          RecFact obj = new RecFact();
          obj.readnum();
     
         obj.factseries();
       }
}

 Output  :

Input an integer whose factorial is to be computed
5
Input an integer
3
The value of the series is 10.

(I am sure you must have observed it basically computes the value of nCr or possible combinations of r objects from a set of n objects.)






Question 10.
In Piglatin a word such as KING is replaced by INGKAY, while TROUBLE becomes OUBLETRAY and so
on. The first vowel of the original word becomes the start of the translation, any preceding letters
being shifted towards the end and followed by AY.
Words that begin with a vowel or which do not contain any vowel are left unchanged.
Design a class Piglatin using the description of the data members and member functions given
below:
Class name : PigLatin
Data members
txt : to store a word
len : to store the length
Member functions

PigLatin() : constructor to initialize data members
voidreadstring() : to accept the word input in uppercase
void convert() : converts the word into its piglatin form and
displays the word.( changed or unchanged)
void consonant() : counts and displays the number of
consonants
present in the given word.
Specify the class Piglatin giving details of the constructor, void readstring(), void convert() and void
consonant(). Also define a main function to create an object and call methods accordingly to enable
the task.
[ 10 ]


Sol.
class Piglatin
{
   String txt;
   int len;
   Piglatin()  //  It is a good programming practice to provide a default constructor when you create                          another constructor . Though this step is completely optional.
  {
 
  }

 Piglatin(string txt)
{
   this.txt = txt;
  len = txt.length();

}

 void readstring()
{
  InputStreamReader rd = new InputStreamReader();
  BufferedReader br = new BufferedReader(rd);
System.out.println("Enter any word");
String str= br.readline();
str= str.toUpperCase();

  Piglatin pgobj = new Piglatin(str);


}

void convert()
{
   int pos=-1;
   char ch ;
   for(int i = 0; i<len ; i++)
   {
      ch = txt.charAt(i)'
        if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
            {
                 pos = i;
                 break;
             }
   }

     if(pos!= -1)
     {
         String s1 = txt.substring(pos);
         String s2 = txt.substring(0,pos);
         String pglatin = s1+s2+"AY";
         System.out.println("The piglatin conversion is "+pglatin);
     
      }

       else
             System.out.println("Piglatin conversion not possible for the input");
}

     void consonant()
{
      int num = 0;
      char ch;

      for (int i = 0; i<len ; i++)
       {
           ch= txt.charAt(i);
          if(ch!='A' || ch!='E' || ch!='I' || ch!='O' || ch!='U')
               num=num+1;
        }
    System.out.println("The number of consonants in "+txt+"  is "+num);

}

}

class showmain()
 {
    public static void main(String args[])
       {
            Piglatin obj = new Piglatin();
           obj.readnum();
           obj.convert();
           obj.consonant();
     
       }
 }


Output

Enter any word
king
The piglatin conversion is INGKAY
The number of consonants in KING is 3