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

Time: 3 Hours                                                                                                     Max. Marks: 100

 

NOTE: There are 11 Questions in all.

 

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

·      Answer any THREE Questions each from Part I and Part II. Each of these questions carries 14 marks.

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

 

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

       

a.       Within a switch statement

                  

                   (A) Continue can be used but Break cannot be used                                                       

                   (B) Continue cannot be used but Break can be used

                   (C) Both Continue and Break can be used                                                                      

                   (D) Neither Continue nor Break can be used

       

b.      Data members which are static

                 

                  (A) cannot be assigned a value

                   (B) can only be used in static functions

                   (C) cannot be defined in a Union        

                   (D) can be accessed outside the class

            

             c.   Which of the following is false for cin?

            

(A)    It represents standard input.         

(B)    It is an object of istream class.

(C)    It is a class of which stream is an object.        

(D)    Using cin the data can be read from user’s terminal.

 

             d.   It is possible to declare as a friend

                                

(A)   a member function                        (B)  a global function

(C)  a class                                          (D)  all of the above

                             

             e.   In multiple inheritance

 

(A)     the base classes must have only default constructors   

(B)     cannot have virtual functions

(C)     can include virtual classes            

(D)    None of the above.

 

             f.    for (; ;)

 

(A)     means the test which is done using some expression is always true      

(B)     is not valid

(C)     will loop forever                          

(D)    should be written as for( )


             g.   The operator << when overloaded in a class

 

(A)   must be a member function           (B)  must be a non member function

(C)  can be both (A) & (B) above       (D)  cannot be overloaded

 

             h.   A virtual class is the same as

                  

(A)    an abstract class                          (B)  a class with a virtual function

(C)  a base class                                  (D)  none of the above.

 

PART I

Answer any THREE Questions. Each question carries 14 marks.

 

  Q.2     a.   What is encapsulation?  What are its advantages?  How can encapsulation be enforced in C++?                 (6)

 

             b.   Write a C++ program which reads in a line of text word by word in a loop using cin statement and outputs on the screen the words in the reverse order using cout statement.  For example, if the input is ‘Jack Prat could eat no fat’, then the output is ‘fat no eat could prat Jack’.                           (8)

 

  Q.3     a.   How are member functions different from other global functions?                         (2)

                  

             b.   Define a class Word which can hold a word where the length of the word is specified in the constructor.  Implement the constructor and the destructor.                                                     (6)

       

             c.   Using the class defined above, define another class Line of 10 words. Implement the constructor.                                                               (6)

 

  Q.4           Differentiate and give examples to bring out the difference between                         

 

(i)                  function overloading and function overriding

(ii)                function template and inline functions

(iii)               default constructor and copy constructor

(iv)              public and private access specifiers                         (3.5 x 4 = 14)

 

  Q.5     a.   The keyword ‘virtual’ can be used for functions as well as classes in C++.  Explain the two different uses.  Give an example each.                         (6) 

 

             b.   Define a class Array which can hold 10 elements of type int.  Define a member function

                   int Get(int index);

                   which returns the indexth element of the array if index is between 0 to 9 and throws an exception if index is out of bounds.  Catch the exception in the main program and print an error.                        (8)

 

  Q.6     a.   Define a class Bag(duplicate values permitted) of 50 integers.  The values of the integers is between 1 and 10.  Overload the subscript operator[].                                                               (6)

 

             b.  Write a global function which uses the above operator and prints the number and the number of times it appears (frequency).  For example, if the Bag Smallbag contains{2, 3, 2, 1, 3, 2} then the output will be                                                            (8)

 

                   Number                                               Frequency

                  

                   1                                                         1                                                                                       

                   2                                                         3

                   3                                                         2                                                                

PART II

Answer any THREE Questions. Each question carries 14 marks.

  Q.7     a.   What is the output of the following program segment?

                   void main(){

                          char buffer[80]; cout<< “Enter a line with *: ”<<endl;

                          cin.get(buffer, 8, ‘*’);

                          cout << “The buffer contains ” << buffer << endl;}

                   Input : Hello World*       

 

             b.   What does the poem.txt contain?

                   void main()

                   {

                   ofstream outfile(“poem.txt”);

                   outfile << “Mary had a lamb”;

                   outfile.seekp(11, ios::beg);

                   outfile << “little”;

                   }                                                        

 

             c.   Given

                         int * entry = new int [10];

                              

                   Write code to fill the array with 10 numbers entered through the keyboard.

 

             d.   What is the output of the following program segment?

                   void main() {

                       int i = 6;

                       double d = 3.0;

                       char * str = “OOP”;

                       cout.setf(ios::left);cout.setf(ios::fixed);

                       cout << setfill(‘#’) << setw(5) << str;

                       cout << setprecision(1) << i + d;

                   }

 

             e.   What is the output of the following program segment?

                      void main ( )

                                { int xyz = 333;

                                 cout << endl <<

                                 “The output is \n” << endl

                                  ; cout << “x\

                                    y\

                                z\

                                = ”

                                << xyz;

                   }

             f.    When do you rethrow any (caught) exception?  Give the syntax.

 

             g.   Point out the error and correct it

                   class X( static int a; char b; int star() {return a * b } };                                  (2 x 7)                                                                                                                                                                                  

       

  Q.8           Assume that the cell users are two kinds – those with a post paid option and those with a prepaid option.  Post paid gives a fixed free talk time and the rest is computed at the rate of Rs.1.90 per pulse.  Prepaid cards have a fixed talk time.                                                                                   

 

                   Define a class Cell_user as a base class and derive the hierarchy of classes.  Define member functions and override them wherever necessary to

(i)                  retrieve the talk time left for each user.

(ii)                print the bill in a proper format containing all the information for the post paid user.  (14)

 

  Q.9     a.   Explain the I/O stream hierarchy in C++.                                                            (4)

 

             b.   Define a class License that has a driving license number, name and address.  Define constructors that take on parameter that is just the number and another where all parameters are present.   (6)

 

             c.   Create a file and store the License information.                                                   (4)

 

Q.10           a.                                                        Define a class template Pair which can have a pair of values.  The type of the values is the parameter of the template.  Define a member function Add() which adds the two values and returns the sum.                   (10)

 

             b.   Check if the template works for char* where Add() must concatenate the two strings.  If not find a solution.                                                                                                                        (4)

 

Q.11                                                                      Write short notes on any FOUR:-

 

(i)                  Static members of a class.

(ii)                Inheritance.

(iii)               Nested class.

(iv)              Access control.

(v)                Friend.                                                                                 (3.5 x 4 = 14)