Monday, 15 February 2016

ISC Computer Science Theory 2015 Part IV (Solved)

This is the Section C of computer science paper. This section tests your understanding of what you have learnt in Class XII . Always be prepared with a question on stack , methods in linked list , a program to be written using the concept of inheritance (usually the program logic to be implied here is simple) and  implementation of pre / in / post order traversal of binary tree . Here goes the questions asked last year ...

Question 11 :

WordPile is an entity which can hold maximum of 20 characters . Restriction is that characters can be added or removed from only one end . Some of the members of the class are given below :

Class Name :                                                   WordPile
Data Members/ Instance variables :

ch[ ]                                                   : character array to hold character elements 
capacity                                            : Integer variable to store maximum capacity
top                                                     : to point to the index of topmost element

Methods/Member Functions :

WordPile(int cap)          : Constructor to initialise data members capacity to cap and                                               top to -1 and create WordPile.
void pushChar(char v)  : adds character to the top of the WordPile if possible                                                         otherwise outputs a message "Word Pile is full".
char popChar()                 : Returns the deleted character from WordPile if possible .                                                 otherwise returns "//".

a) Specify class WordPile giving details of the constructor and member functions .
The main function and algorithm need not be written .                                               [8]

b) What is the name of the entity described above and state one of it's applications.                                                                                                                                                  [2]


Sol.

public class WordPile
{
  char ch[] = new char[20];
  int capacity, top;
  WordPile(int cap)
  {
    capacity = cap ; 
    top = -1;
    ch = new char[capacity];
   
  }
  
  void pushChar(char v)
  {
    if(top<capacity-1)
    
      ch[++top]=v;
     
    else
      System.out.println("WordPile is full");
    
  }
  char popChar()
  {
    if(top>=0)
      return ch[top--];
    else
      return('/');

  }
}


The entity described above is a stack . 

1) Stack is used for parsing.
2) Stack is used in conversion of an expression to infix or prefix or postfix form .
3) Stack is used in recursive function.
4) Stack is used in calling function.

(Any one of the above can be stated as an expression . Whichever serves your memory right easily go for it)

Question 12 :
A line on a plane can be represented by coordinates of two endpoints p1 and p2 as p1(x1,y1) and p2(x2,y2).
A super class Plane is defined to represent a line and a sub class Circle to find the length of the radius and the area of the circle by using the required data members of super class. Some of the members of both classes are given below 

Class Name                 :                        Plane
Data Members/Instance variables :   

x1                                  : to store x coordinate of first end point
y1                                  : to store y coordinate of first end point

Member Functions/Methods :

Plane(int nx, int ny)   :  parameterized constructor to assign the data members x1=nx ,                                         y1=ny
void Show()                : to display the coordinates 

Class Name                :                  Circle

Data Members/Instance variables :   

x2                                  : to store x coordinate of second end point
y2                                  : to store y coordinate of second end point
radius                           : double variable to store the radius of the circle
area                              : double variable to store the area of the circle 

Member Functions :

Circle(...)                      : parameterized constructor to assign values to data members                                          of both the classes

void findRadius()        : to calculate the length of the radius using the formula 
                                         

                                        assuming that x1, x2, y1 and y2 are the coordinates of two                                               ends of the diameter of a circle .

void findArea()            : to find the area of the circle using formula πr2 . The value of                                                pie is 22/7 or 3.14

void Show()                 : to display both the coordinates along with the length of radius                                         and area of the whole circle .

Specify the class Plane giving details of the constructor and void Show(). Using the concept of inheritance specify the class Circle giving details of the constructor , void findRadius() , void findArea() and void Show(). The main function and algorithm need not be written . 

Sol. 

public class Plane
{
  double x1 , y1;
  Plane (double nx , double ny)
   {
       x1=nx;
       y1=ny;
    }
 public void Show()
  {
   System.out.println(" X coordinate is : "+x1);
   System.out.println(" Y coordinate is : "+y1);
    
  }
}

public class Circle extends Plane
{
  double x2 , y2 , radius , area;
  Circle( double nx, double ny, double mx, double my)
  {
    super(nx, ny);
    x2=mx;
    y2=my;
  }
  
  void findRadius()
  {
    radius = (Math.sqrt(Math.pow((x2-x1),2)+Math.pow((y2-y1),2)))/2;
  }
  
  void findArea()
  {
    area = 3.14 * radius * radius;
  }
  
  void Show()
  {
    super.Show();
   System.out.println("Second X coordinate is : "+x2);
   System.out.println("Second Y coordinate is : "+y2);
   System.out.println("First X coordinate is : "+x1);
   System.out.println("First Y coordinate is : "+y1);
    System.out.println("Radius is : "+radius);
   System.out.println("Area is : "+area);
    
  }

}

Question 13  :

a) A linked list is formed from objects of the class
            class Nodes
           {
                int num;
                Nodes next;
            }
Write an algorithm or a method to print the sum of nodes that contain only odd integers of an existing linked list.
The method declaratiion is a s follows :
                                                                    void NodesCount(Nodes starptr)                [4]
Sol.
a. ) Algorithm to print the sum of nodes of odd integers of existing linked list
      1- Start 
       2- Set temporary pointer to start node
       3- Repeat steps 4 and 5 until pointer reaches null. Display the count . Exit.
       4- Check for odd integers and accumulate.
        5- Move pointer to next node
        6- Exit

Function/ Method to print the sum of nodes that only contain odd integers of existing linked list.

void NodesCount(Nodes starptr) 
  {
    Nodes temp = new Nodes(starptr);
    int sum = 0;
    while (temp!=null)
    {
      if(temp.num%2!=0)
      {
        sum = sum+tempp.num;
        temp=temp.next;
      }
     System.out.println("Sum is :"+sum);
    }
    

  }

[You have to write any one of the above. In my opinion the algorithm is simpler and you do not have to pay attention to syntaxes for it. ]
b) i) Give the meaning of the following expressions in Big O notation
                         O(n)
                         O(n2)
   ii) List any two cases to analyse algorithm complexity                            [1+1]

Sol.
i) O(n) gives the complexity of a single loop or condition (like if-else) or expression
   O(n2) gives the complexity of a nested loop that goes through iteration from 1 to n

ii) Best case and worst case complexities analyses algorithm complexity . 



c) Answer the following questions from the binary tree




i) Name the leaf nodes of right sub tree
ii) Write the post order traversal of left sub tree of node B including itself .
iii) State the levle numbers of nodes R and M when the root node is at level 0.
iv) Name the internal nodes of the tree.                                      [1+1+1+1]

Sol. 

i) P and E 
ii) R N M C B
iii) 2
iv) C,M,F,H


Note :

Nothing much to say for this section . In my earlier posts I have suggested you to write the default constructor even if not asked for as a good coding practice . But here you are asked to write only the class definition without the main class i.e. without creating the instance of the class so I have skipped writing the default constructor . Therefore the headers are also missing. Another point to note is in the inheritance question stress is given on the coding to implement the inheritance principle . So in the method for finding the radius you are free to use the math assembly instead of doing it step by step using simple arithmetic procedure. 

No comments:

Post a Comment