Code: C-11 / T-22                                        Subject: OJBECT ORIENTED PROGRAMMING

Time: 3 Hours                                                                       Flowchart: Alternate Process: December 2005                              Max. Marks: 100

 

NOTE: There are 9 Questions in all.

·      Question 1 is compulsory and carries 20 marks. Answer to Q. 1. must be written in the space provided for it in the answer book supplied and nowhere else.

·      Out of the remaining EIGHT Questions answer any FIVE Questions. Each question carries 16 marks.

·      Any required data not explicitly given, may be suitably assumed and stated.

Q.1       Choose the correct or best alternative in the following:                                         (2x10)

       

a.       Identify the operator that is NOT used with pointers

                   (A)  ->                                                (B)  &

(C)    *                                                 (D)  >>                                                                

b.      Consider the following statements

                                                                          char *ptr;

                                                                          ptr = “hello”;

         cout << *ptr;                                                                                                                

                                                                       What will be printed?                                                               

(A)    first letter                                      (B)  entire string

(C)  it is a syntax error                         (D)  last letter                                                            

             c.   In which case is it mandatory to provide a destructor in a class?

(A)    Almost in every class                   

(B)    Class for which two or more than two objects will be created

(C)    Class for which copy constructor is defined    

(D)    Class whose objects will be created dynamically

             d.   The members of a class, by default, are

(A)    public                                          (B) protected

(C)  private                                         (D)  mandatory to specify  

             e.   Given a class named Book, which of the following is not a valid constructor?         

(A)     Book ( ) { }                                 (B)  Book ( Book b) { }

(C)  Book ( Book &b) { }                   (D)  Book (char* author, char* title) { }

             f.    Which of the statements is true in a protected derivation of a derived class from a base class?

(A)     Private members of the base class become protected members of the derived class    

(B)     Protected members of the base class become public members of the derived class

(C)     Public members of the base class become protected members of the derived class     

(D)    Protected derivation does not affect private and protected members of the derived class.

             g.   Which of the following statements is NOT valid about operator overloading?

(A)     Only existing operators can be overloaded.    

(B)     Overloaded operator must have at least one operand of its class type.

(C)     The overloaded operators follow the syntax rules of the original operator.

(D)    none of the above.                       

 

 

 

             h.   Exception handling is targeted at

(A)    Run-time error                              (B) Compile time error

(C) Logical error                                 (D) All of the above.

             i.    A pointer to the base class can hold address of

(A)   only base class object                  

(B)   only derived class object

(C) base class object as well as derived class object         

(D) None of the above

             j.    Function templates can accept

(A)  any type of parameters                

(B)  only one parameter

(C)  only parameters of the basic type

(D)  only parameters of the derived type

 

 

Answer any FIVE Questions out of EIGHT Questions.

Each question carries 16 marks.

 

  Q.2     a.   What is the difference between passing a parameter by reference and constant reference?               (2)

       

             b.   Define the following terms and give an example to show its realization in C++

(i)   Encapsulation                                (ii) Class variables and class functions

(iii) Repeated inheritance                      (iv) Overloading                             (3.5 x 4)

 

  Q.3     a.   Define a class Point which is a three dimensional point having x, y and z coordinates.  Define a constructor which assigns 0 if any of the coordinates is not passed.                                                      (6)

 

             b.   Define a class Sphere which has a radius and a center.  Use the class Point defined in (b) to define the center.  Define the constructor.  Define a member function Volume (which is given as )  which computes the volume.  Use the class in main() to create an instance of sphere.  Compute the volume and print it.                                                                    (10)                                                           

 

  Q.4     a.   What is the access specification that should precede a friend function?               (2)

 

             b.   Define a class Vector which can hold 20 elements of type int.  Write a member function read() which reads the values from the input stream and assigns them to the Vector object.                               (6)

 

             c.   Overload the operator + for the class Vector defined above which adds two vectors.  Define the addition operator as a friend function.  The result must be returned as an object of the class Vector.            (8)       

 

  Q.5           An organization has two types of employees:  regular and adhoc.  Regular employees get a salary which is basic + DA + HRA where DA is 10% of basic and HRA is 30% of basic.  Adhoc employees are daily wagers who get a salary which is equal to Number * Wage.

                   (i)    Define the classes shown in the following class hierarchy diagram:

                  

 

 
 

 

 

 

 

 

 

 

 

 

 

 

 


                   (ii)  Define the constructors.  When a regular employee is created, basic must be a parameter.  When adhoc employee is created wage must be a parameter.

                   (iii)  Define the destructors.

                   (iv) Define the member functions for each class.  The member function days ( ) updates number of the Adhoc employee.

                   (v)   Write a test program to test the classes.                                      (4+4+2+4+2)

 

  Q.6     a.   Write a function template for the function Power ( ) which has two parameters base and exp and returns .  The type of base is the parameter to the template and exp is int.  If exp is negative then it must be converted to its positive equivalent. For example,  and  must both return 8.             (6)

 

             b.   Instantiate the template function defined above for int type.                                 (2)

 

             c.   What is exception handling?                                                                               (2)

 

             d.   What are the keywords on which exception handling is built?  Explain each one of them.  Give an example.                                                            (6)

 

  Q.7     a.   Explain the following:

                   (i)    ios class                                       (ii)  setf( ) function

                   (iii)  rdstate() function                           (iv)  writing of objects to files                   (8)  

 

             b.   Write a program which asks for a file name from the keyboard, opens a file with that name for output, reads a line from the keyboard character by character and writes the line onto the file.  (8)

 

  Q.8           Differentiate between

(i)                  int* q = p; and n = *p;

(ii)                Constructor and destructor

(iii)               Interface and implementation of a class

(iv)              public and private member of a class

(v)                Overloaded pre-increment operator and overloaded post increment operator

(vi)              Abstract and concrete class

(vii)             get ( ) and getline ( ) function

(viii)           Composition and inheritance                                                     (16)

 

  Q.9     a.   What is the wrong with the following program?

                   #include <iostream.h>

                   void main ( )

                   {

                       class Test  {

                          int x;

                        public: int getX ( ) {return x;}

                                   void setX ( int a) { x = a;}

                                   void showX{ cout << getX ( ) << endl;}

                      };

                   }

       

             b.   What is the output of the following program, if any?

                   #include <iostream.h>

                   class A {

                        int a;

                      public: A( int aa) { a = aa;}

                                   void print ( ) { cout << “ A = “ << a; }

                   };

                   class B : public A {

                          int b;

                   public: B ( int aa, int bb) : A (aa) { b = bb;}

                           void print ( ) { cout << “ B = “ << b; }

                   };

                   void main ( )

                   { A objA(10);

                      B objB(30, 20);

                      objA = obj B;

                      objA.print ( );

                   }

 

             c.   What is wrong with the following code?

                   int *p = new int;

                   int *p = new int;

                   cout << “p = ”  << p << “, q = “ << q << “, p+q = “ << p+q << endl;

 

             d.   How many times is the copy constructor called in the following code?

                   class copy_const{

                   };

                   copy_const f(copy_const u)

                   { copy_const v(u);

                      copy_const w = v;

                       return w;

                   }

                   main ( )

                   {                                                        

                     copy_const x;

                     copy_const y = f (f (x));

                   }                                                                                                                 (4 x 4)