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

Sunday, 6 December 2015

ISC specimen Computer Science paper , Group A (part II)

Q 4.a) Given the Boolean function F(P,Q,R,S)= Σ(0,1,3,4,5,6,7,9,10,11,13,15)
Using Karnaugh’ map to reduce the function F, using SOP form. Draw the logic gate diagram
of the reduced SOP form. You may use gates with more than 2 inputs. Assume that the variables and their complements are available as inputs.
                                                                                                                                              [ 4 + 1 = 5 ]

Sol. The Karnaugh' map for the given expression is as follows


Quad 1 (m0 +m1 +m4 +m5 )= P'R'
Quad 2 (m4  +   m5   +  m6 +   m7  )= P'Q
Octet 1 (  m1 + m3  +   m5  + m7  +m9  +m11  +m13  +m15  )= S
 Diad 1 (m10  +   m11 )=PQ'R 
   
                                                                                                                                                                                   The reduced expression as per Karnaugh' map = S+P'R'+P'Q+PQ'R

(N.B. From the octet you get S , from the quatret shown in light grey in the figure you get P'R' from the second quatret shown in darker shade you will get P'Q and finally from the diad you get PQ'R)


The logic diagram of the reduced sum of products is as follows,



Q 4b)  Given the Boolean function F(P,Q,R,S)= π(3,8,10,12,13,14,15)
Using Karnaugh’ map to reduce the function F, using POS form. Draw the logic gate diagram of the reduced POS form. You may use gates with more than 2 inputs. Assume that the
variables and their complements are available as inputs.                                              4+1=5                                               



 Sol: The Karnaugh' map of the given Boolean function is as follows






Quad 1 (M12M13M14M15 )= (P+Q)
Diad 1 (M8M12)=(P+R'+S')
Diad 2 (M10 M14 ) =( P+R+S')
M3  =  (P'+Q'+R+S)
 

The reduced expression based on the Karnaugh map is as follows 
 
(P'+Q'+R+S)(P+Q)(P+R'+S')(P+R+S')

The logic gate diagram of the above is given as follows 



Q 5) The main safe in the nationalizes bank can be opened by means of a unique password consisting of
three parts. Different parts f the password are held by the chairman, Regional Manager, Bank
Manager and Head Cashier of the bank, respectively.
In order to open the safe any one of the following conditions must be satisfied:
The password of the chairman, together with passwords of any two other officials, must be entered.
OR
The password of all three bank officials, excluding the chairman, must be entered.
The inputs are:
INPUTS
A Denotes the chairman’s password
B Denotes the Regional Manager’s password
C Denotes the Bank Manager’s password
D Denotes the Head Cashier’s password
Output
X Denotes the safe can be opened[ 1 indicates YES ad 0 indicates NO in all cases]
a) Draw the truth table for the inputs and outputs given above and write the SOP expression
for X(A,B,C,D)..
b) Reduce X(A,B,C,D) using Karnaugh’s map, if possible.
Draw the logic gate diagram for the reduced SOP expression for X(A,B,C,D) using AND and
OR gates. You may use gates with two or more inputs. Assume that the variable and their
complements are available as inputs.
                                                                                                                                         [ 5 x 2 = 10]

Sol a) : The truth table of the above problem is as follows,                                                                    
The Sum of Products expression based on the above truth table  is                                                            
F(A,B,C,D)=Σ(7,11,13,14,15)

b)  The Karnaugh's map of the above sum of products is as follows ,                                                              
                                           
Diad 1 (m7  + m15  ) = BCD
Diad 2 (m13  +m15  )= ABD
Diad 3 (m15  +m14  )= ABC
Diad 4 (m11   + m15  )= ACD
 
 The reduced expression according to the Karnaugh's map is  
                    ABD+ABC+BCD+ACD

The logic gate diagram for the above expression is as given below.
                                                  
[NB: It is always better to attempt questions like above. the Karnaugh's map for this question is much easier. You may initially find the question too lengthy and decide to opt this out , but please I would rather suggest you to go through it at least once .  It's a no-brainer and just a set of instructions to create a truth table based on the variables and the rest is just like question 4 . Only thing you do not have to draw and solve the Karnaugh's map twice if you attempt Q4. Please let me know if you want me to do a tutorial on K-map because you have to compulsorily attempt this in your examination.]

Q 6. a) Draw the truth table and logic circuit diagram for a Decimal to Binary Encoder. [ 5 ]
b) Given F(X,Y,Z)=Σ(1,3,7) Verify : F(X,Y,Z)= π(0,2,4,5,6) [ 2 ]
c) Simplify the following expression by using Boolean laws. Show the working and also
mention the laws used: X’Y’Z’ + XYZ’ +XY’Z’ + X’YZ’ [ 3 ]

Sol. a)      The truth table for a Decimal to Binary encoder is as follows




The circuit diagram for the same is as follows                                                                    
b) F(X,Y,Z)=Σ(1,3,7)
F'(X,Y,Z)=(Σ(1,3,7))'
                    = Σ(0,2,4,5,6)
                                          =( m0+m2+m4+m5+m6 )'
                                          = m0´m2´m4´m5´m6´ 
                                    =  M0M2M4M5M6
                              = π(0,2,4,5,6)


c)X'Y'Z'+XYZ'+XY'Z'+X'YZ'
=X'Y'Z'+X'YZ'+XYZ'+XY'Z'
=X'Z'(Y+Y')+XZ'(Y+Y')
                            =X'Z'.1+XZ'.1  (By complement)          
 =Z'(X'+X)                    
                            =Z'.1 (By complement)                 
=Z'     



Q 6)       a) Define cardinal form of an expression and canonical form of an expression. Give an example for each  [3]

b) Which gate is equivalent to : (NOR) OR (XOR) [ 3 ]

c) Define a Half Adder. Draw the truth table and logic diagram of a half adder. [ 4 ]

a) In cardinal form of expression terms that form a function may contain one , two or any number of literals.There are two types of cardinal forms - sum of products and product of sum . An example for each type is given as 
i) F(A,B,C) =AB'C'+ABC'+A'B'C (Sum of products form)
ii) F(A,B,C)=(A+B'+C')(A+B+C')(A'+B'+C)  (Product of sum form)

Boolean expressions that are expressed as sum of min terms or product of max terms are said to be in canonical form .  An example  of   each  is   given                                                                                    i) F(A,B,C)= Σ(0,1,3,4,5)  (Sum of min terms)
ii) F(A,B,C)= π(2,6,7)  (Product of max terms)


b) A NOR gate is algebraically implemented as A'B'
An XOR gate is implemented as A'B+AB'
The given expression is algebraically implemented as = (A'B') + (A'B+AB')
=A'B'+A'B+AB'
=A'(B'+B)+AB'
=A'+AB'

Now let us consider truth table of the above expression 


                         Therefore from the above truth table it is evident that the given combination of gates works as a nand gate which gives a zero output when all the inputs are positive.

c)  Half Adder is an example of a simple functional digital circuit built from two logic gates . The half adder adds two one-bit binary numbers (A, B) .  The output is the sum of two bits (S) and the carry (C). The truth table of the half adder is as follows 



                            The logic diagram of half adder is given below,

                 
              
  

Sunday, 15 November 2015

Specimen ISC Computer Science Theory (Solved) - (Part 1)

1a. State the two Distributive laws of Boolean Algebra. Prove any one of them with the help of a truth table .  [2]

Sol: The two Distributive Laws of Boolean Algebra are as follows
        i) X.(Y+Z)=(X.Y)+(X.Z)
       ii) X+(Y.Z)=(X+Y).(X+Z)

Truth table for i is as follows

Truth table for ii is as follows

(N.B. :You can choose any one of the above proofs)




Q1b.  Find the truth table to verify the expression p->q is equivalent to ~p->~q   [2]

Sol: The truth table for the given expression is as follows




      Hence proved.


Q 1c. Find the complement of the following - [(XY)'.X][(XY').Y]                         [2]

Sol: Complement of the above expression is as follows
[{(XY)'.X}.{(XY)'.Y}]'
=[(XY)'.X]'+[(XY)'.Y]'
=[(XY)']'+X'+[(XY)']'+Y'
=(X'+Y')'+X'+(X'+Y')'+Y'
=X.Y+X'+X.Y+Y'
=X.Y+X'+Y'


Q 1d. Simplify the following Boolean expression using laws of Boolean Algebra. At each step clearly state the law used for simplification.
                                                    Z(Z+X).X(Y+Y')                                                     [2]

Sol:                     Z(Z+X).X(Y+Y')
                         =Z(Z+X).X.1 (Complement law)
                         =(Z.Z+Z.X).X
                         =(Z+Z.X).X     (Idempotent Law)
                         =Z.X+Z.X.X
                         =Z.X+Z.X        (Idempotent Law)
                         =Z.X                  (Idempotent Law)


Q 1e. Given F(X,Y,Z)=XZ+XY+YZ. Write the function in canonical sum of products.        [2]

Sol.     XZ+XY+YZ
         =XZ(Y+Y')+XY(Z+Z')+YZ(X+X')     (A+A'=1 )
         =XYZ+XY'Z+XYZ+XYZ'+XYZ+X'YZ
         =XYZ+X'YZ+XY'Z+XYZ'   (A+A+A=A)




Q 2a. What do LIFO and FIFO stand for?                                                                               [2]

Sol:  LIFO stands for Last In First Out and FIFO stands for First In First Out. Both describes the way in which data is extracted from a certain data structure. In stack data is extracted in LIFO manner while in queue FIFO is followed.


Q 2b. For an array of real nos x[-6...8,-12.....20] , find the address of x[5][4] if x[1][1] is stored in location 100 in the column major order. Assume that each element requires 4 bytes.            [2]

Sol. The formula for memory calculation of a particular address using column major order is as follows

a[i][j]=B+W*[(i-Lr)+M*(j-Lc)]

B=100
W=4
i=5
Lr=1
M=8-(-6)=14
j=4
Lc=1.

Memory Address= 100+4*[(5-1)+14*(4-1)]
                            = 284




Q 2c. Convert the following infix expression to it's postfix form       b*[(a/d)-(c*(e-f))]         [2]

Sol.
                  Stack                                                  Output
                                                                              b
                *[(                                                         ba
               *[(/                                                         bad
               *[                                                            bad/
               *[-(                                                         bad/c
               *[-(*(                                                      bad/ce
               *[-(*(-                                                     bad/cef
               *[-(*                                                        bad/cef-
               *[-                                                           bad/cef-*
              *                                                               bad/cef-*-
             NULL                                                       bad/cef-*-*
                                                                   
So the postfix form is bad/cef-*-*


Q 2d. Define a binary tree.                                        [2]

Sol.  Binary tree is a tree data structure in which each node has at the most two children node referred to as the left node and the right node. The topmost node is called the root node and the last nodes are referred to as the leaf nodes. It is represented as the following
         



Q 2e. State difference between abstract class and interface .                      [2]

Sol:



Q 3a.  a) The following function is a part of some class. It returns the value 1 when the number is an
Armstrong number, otherwise it returns 0.
class Armstrong
{
/* An armstrong number is a number which is equal to the sum of the cube of its individual digits*/
int arms(int n)
{
int digit=0, sum=0;
int rem=n;
while(?1?)
{
digit=?2?;
sum=sum+?3?;
rem=?4?;
}
if(?5?)
return 1;
else
return 0;
}
}
i) What is the expression / value at ?1?
ii) What is the expression / value at ?2?
iii) What is the expression / value at ?3?
iv) What is the expression / value at ?4?
v) What is the expression / value at ?5?                                            [1*5=5]


Sol.
i) while(rem>0)
ii) digit=rem%10;
iii) sum= sum+(digit*digit*digit);
iv) rem=rem/10;
v) if(sum==n)


Q 3b. Give output of the following function where x an y are arguments greater than 0. Show the dry
run/ working.
int strange(int x , int y)
{
//Assuming x>0 y>0
if(x>y)
{
x=x-y;
return strange(x,y);
}
else
return x;
}
i) What will the function strange(20,5) return ?
ii) What will the function strange(15,6) return ?
iii) In one line state the type of function used.                                          [2+2+1]


Sol.
i) Dry run for strange(20,5) is as follows;
ii) Dry run for strange(15,6) is as follows:
  

iii) In the above question strange(x,y) is a recursive function.