0% found this document useful (0 votes)
27 views33 pages

2018 Winter Model Answer Paper

Uploaded by

lahotipawan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views33 pages

2018 Winter Model Answer Paper

Uploaded by

lahotipawan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
Important Instructions to examiners:
1) The answers should be examined by key words and not as word-to-word as given in the model
answer scheme.
2) The model answer and the answer written by candidate may vary but the examiner may try to
assess the understanding level of the candidate.
3) The language errors such as grammatical, spelling errors should not be given more Importance
(Not applicable for subject English and Communication Skills).
4) While assessing figures, examiner may give credit for principal components indicated in the
figure. The figures drawn by candidate and model answer may vary. The examiner may give
credit for any equivalent figure drawn.
5) Credits may be given step wise for numerical problems. In some cases, the assumed constant
values may vary and there may be some difference in the candidate’s answers and model
answer.
6) In case of some questions credit may be given by judgement on part of examiner of relevant
answer based on candidate’s understanding.
7) For programming language papers, credit may be given to any other program based on
equivalent concept.
8) Program or code snippet shall be considered as an Example.

Q. Sub Answer Marking


No Q.N. Scheme
1. A) Attempt any six of the following: 12
a) Write structure of C++ program. 2M
Ans. Structure of a C++ program
INCLUDE HEADER FILES correct
structur
CLASS DECLARATION e
2M
MEMBER FUNCTION DEFINITIONS
MAIN FUNCTION PROGRAM
OR
Description:-
1. Include header files: Programmer include all header files which are
require to execute given program such as iostream.h
2. Class declaration: Programmer declares all classes which are
necessary for given program.
3. Member Function definitions: Programmer declares and defines
member functions of a class.
4. Main Functions: Programmer creates object and call various
functions declared within various classes.

Page 1 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
b) Define pointer variable. Give its syntax. 2M
Ans. Definition: Correct
Pointer is a variable that holds memory address of another variable of definitio
similar data type. n 1M
Syntax to declare pointer variable: Correct
data_type *pointer_variable; syntax
1M
c) State any two access specifier with example. 2M
Ans. Access specifier:
1. private: List two
2. protected: correct
3. public access
specifier
Example: 1M
class sample
{ Any
private: example
int a; 1M
protected:
int b;
public:
void display()
{
cout<<a<<b;
}
};
d) Define constructor. State any two types of constructor. 2M
Ans. Definition:
A constructor is a special member function whose task is to initialize the Correct
objects of its class. definitio
Types of constructor: n 1M
1) Default constructor
2) Parameterized constructor List Any
3) Copy Constructor two
4) Constructor with default value types
5) Multiple constructor/overloaded constructor 1M

Page 2 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
e) Define polymorphism. List types of polymorphism. 2M
Ans. Definition: Correct
Polymorphism means ability to take more than one form that means a definitio
program can have more than one function with same name but different n 1M
behavior.
Types of polymorphism: List two
1) Compile time polymorphism types
2) Runtime polymorphism 1M
f) Write any two advantages of inheritance. 2M
Ans. Advantages of inheritance: Any two
1) Use of inheritance in a program gives reusability of code. relevant
2) Inheritance avoids duplication of code in program. advanta
3) Inheritance reduces length of code. ges 1M
4) Inheritance reduces time to compile the lengthy code by reusing it. each
g) Explain the concept of this pointer. 2M
Ans. Concept of this pointer:
C++ use a unique keyword called “this” to represent an object that Correct
invokes a member function. This unique pointer is automatically passed explanat
to a member function when it is invoked. “this” is a pointer that always ion 2M
points to the object for which the member function is called.
h) Modify the given code to make its constructor with default 2M
argument
class add
{
private :
int a:
Public : add (int x)
{
a = x:
}
}; Correct
Ans. class add { code 2M
private:
int a; Any
public: default
add (int x=1) value
{ shall be
a=x; consider
} ed
};

Page 3 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
1. B) Attempt any two of the following: 8
a) Explain the concept of overloaded constructor in a class with 4M
suitable example.
Ans. Overloaded constructor:
When more than one constructor function is defined in a same class Correct
then it is called as overloaded constructor. All constructors are defined Explana
with the same name as the class name they belong to. Each of the tion 2M
constructors contains different number of arguments or different data
type of arguments. Depending upon the number of arguments and their
data type, the compiler executes appropriate constructor.
Example:-
#include<iostream.h>
#include<conio.h>
class integer
{
int m, n; Correct
public: Example
integer() 2M
{
m = 0;
n = 0;
}// constructor 1
integer(int a, int b) {
m = a;
n = b;
cout<<"value of m="<<a;
cout<<"value of n="<<b;
} // constructor 2
}
void main() {
clrscr();
integer i1;
integer i2(20,40);
getch();
}
In the above example, constructor is overloaded by defining two
constructors in the same class. Both the definitions are different with
respect to number of arguments. The first constructor does not accept
any argument and the second constructor accepts two integer
arguments.

Page 4 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
b) List different types of inheritance with suitable diagram. 4M
Ans. Types of inheritance:
1) Single inheritance: A derived class is derived from only one base
class.
Diagram:

Any
four
types of
inherita
nce with
suitable
2) Multiple inheritance: A derived class is derived from more than one diagram
base classes. 1M each
Diagram:

3) Hierarchical inheritance: More than one derived classes are derived


from single class.
Diagram:

4) Multilevel inheritance: A derived class is derived from a derived


class (intermediate base class) which in turn derived from a single base
class.
Diagram:

Page 5 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432

5) Hybrid inheritance: Combination of single, multiple, multilevel and


hierarchical inheritance.
Diagram:

c) Differentiate between constructor and destructor (any four points). 4M


Ans. Constructor Destructor
A constructor is a special A destructor is a special member
member function whose task is function whose task is to destroy
to initialize the objects of its the objects that have been created
class. by constructor.

It constructs the values of data It does not construct the values Any
members of the class. for the data members of the class. four
relevant
It is invoked automatically when It is invoked implicitly by the points
the objects are created. compiler upon exit of a 1M each
program/block/function.
Constructors are classified in Destructors are not classified in
various types such as : any types.
Default constructor
Parameterized constructor
Copy constructor
Overloaded constructor

A class can have more than one A class can have at the most one
constructor. constructor.

Constructor accepts parameters. Destructor never accepts any


Also it can have default value parameter.
for its parameter.

Page 6 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
Syntax: Syntax:
classname() destructor name is preceded with
{… tilde.
… ~classname()
} {….
….
}
Example: Example:
ABC() ~ABC()
{ {
… ….
} }

2. Attempt any four of the following: 16


a) Describe memory allocation for object with diagram. 4M
Ans. The memory space for object is allocated when it is declared & not
when the class is specified. The member functions are created &placed Relevant
in memory space only once when they are defined as a part of a class Explana
definition. Since all the objects belonging to that class use the same tion 2M
member functions, no separate space is allocated for member functions.
Separate memory locations for the objects are essential because the
(data) member variables will hold different data values for different
objects.

Correct
Diagram
2M

Page 7 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
In the above diagram, member functions 1 and 2 are stored in the
common memory space as they require access by all objects. Each
object (object 1, object 2, object 3) has its own separate memory space
for its member variables.
b) Explain virtual function with suitable example. 4M
Ans. Virtual Function:
A virtual function is a member function that is declared within a base
class and redefined by its derived class. When base class and its derived
class both contain same name and prototype member function then Correct
derived class function overrides base class function. Base class pointer Explana
is used to refer member functions of its class as well as its derived class. tion 2M
When base pointer is used to refer to functions, it ignores the contents of
the pointer and selects the member function that matches the function
call. When both the classes contain same name and prototype function,
base pointer executes a function from base class without considering the
address inside the pointer. To execute derived class version of the
overridden function virtual keyword is used with base class function.
When a function is made virtual, compiler checks the address stored
inside the pointer. If the pointer points to base class then function from
base class is executed. If it contains address of derived class then
function from derived class is executed. Run time polymorphism
requires virtual function to execute same name function from base class
and derived class depending on address stored inside the pointer.

Example:

#include<iostream.h>
class Base
{
public:
virtual void show( ) Correct
{ Example
cout<<”\n show base”; 2M
}
};
class Derived : public Base
{
public:
void show( )
{

Page 8 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
cout<<”\n show derived”;
}
};
void main( )
{
Base B,*bptr;
Derived D;
bptr=&B;
bptr->show( );
bptr=&D;
bptr->show( );
}

In above example, both base and derived class contains same name
function as show. By creating a pointer of base class one can invoke
desired show function by storing address of respective object in pointer.

c) Explain different visibility modes and its effect in inheritance. 4M


Ans. Different visibility modes are:
1) Private Visibility
2) Protected modes
3) Public 1M

Effect in inheritance:

Relevant
explanat
Private members of base class are not inherited directly in any visibility ion of
mode. three
1) Private visibility mode modes
In this mode, protected and public members of base class become 1M each
private members of derived class.

Page 9 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
2) Protected visibility mode
In this mode, protected and public members of base class become
protected members of derived class.
3) Public visibility mode
In this mode, protected members of base class become protected
members of derived class and public members of base class become
public members of derived class

d) List any six characteristics of OOP. Also list any two OOP 4M
languages.
Ans. Characteristics of OOP:
1) Emphasis is on data rather than procedure. Any six
2) Programs are divided into objects. characte
3) Data structures are designed such that they characterize the objects. ristics
1/2
4) Functions that operate on the data of an object are tied together in M
the data structure. each
5) Data is hidden and cannot be accessed by external functions.
6) Objects may communicate with each other through functions.
7) New data and functions can be easily added whenever necessary.
8) It follows bottom-up approach in program design.

OOP Languages:
1) Simula
2) Smalltalk Any two
3) Objective C OOP
4) C++ languag
5) Ada es ½ M
6) Object Pascal each
7) Turbo Pascal
8) Eiffel
9) Java
e) Demonstrate the concept of friend function with example. 4M
Ans. Friend function:
The private members of a class cannot be accessed from outside the
class but in some situations two classes may need access of each other’s Explana
private data. So a common function can be declared which can be made tion 2M
friend of more than one class to access the private data of more than one
class. The common function is made friendly with all those classes
whose private data need to be shared in that function. This common
function is called as friend function. Friend function is not in the scope

Page 10 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
of the class in which it is declared. It is called without any object. The
class members are accessed with the object name and dot membership
operator inside the friend function. It accepts objects as arguments.
Example:
#include <iostream.h>
#include<conio.h>
class xyz;
class abc
{ Any
int a; Example
public: 2M
void get1()
{
cin>>a;
}
friend void add(abc,xyz);
};
class xyz
{
int x;
public:
void get1()
{
cin>>x;
}
friend void add(abc,xyz);
};
void add(abc a1,xyz x1)
{
cout<<a1.a+x1.x;
}
void main()
{
abc a1;
xyz x1;
a1.get1();
x1.get1();
add(a1,x1);
getch();
}

Page 11 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
f) Implement a program to declare a class city with data members city 4M
name and state. Accept and display data for 1 object using pointer
to object.
Ans. #include<iostream.h>
#include<conio.h>
class city class
{ definitio
char city_name[20],state[20]; n with
public: function
void accept() s 2M
{
cout<<"\nEnter city data:";
cout<<"\nName:";
cin>>city_name;
cout<<"\nState:";
cin>>state;
}
void display()
{
cout<<"\nCity data is:"; Main
cout<<"\nName:"<<city_name; function
cout<<"\nState:"<<state; with
} pointer
}; to object
void main() concept
{ 2M
city c,*ptr;
clrscr();
ptr=&c;
ptr->accept();
ptr->display();
getch();
}
3. Attempt any four of the following: 16
a) Explain memory management operator with example. 4M
Ans. There are two memory management operators in C++:
1. new
2. delete

Page 12 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
These two memory management operators are used for allocating and
de-allocating memory blocks. C++ allow dynamic allocation techniques
when it is not known in advance how much of memory space is needed.

New operator: Explana


The new operator in C++ is used for dynamic storage allocation. This tion with
operator can be used to create object of any type. example
of new
Syntax: 2M
pointer variable = new datatype;
In the above statement, new is a keyword and the pointer variable is a
variable of type datatype.

Example:
1. int *a = new int;
2. *a = 10;
or
3. int *a = new int(10);

In the above example, the new operator allocates sufficient memory to


hold the object of data type int and returns a pointer to its starting point.
The pointer variable holds the address of memory space allocated.
Explana
Delete operator: tion with
The delete operator in C++ is used for releasing memory space when the example
object is no longer needed. Once a new operator is used, it is efficient to of delete
use the corresponding delete operator for release of memory. 2M

Syntax:
delete pointer_variable;

Example:
Delete p;
b) Explain the concept of constructor with default arguments with 4M
example.
Ans. C++ allows defining a constructor with default arguments. Programmer
can declare a parameterized constructor in which a parameter Explana
(argument) can have default value. tion 2M
When a constructor with default argument is invoked it initializes data
members either with default value or with value passed with function

Page 13 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
call. If function call does not contain value for default argument then
default value is used to initialize data member but if value is pass for
default argument then passed value is used for initialization.
If a constructor requires three arguments and one of them is default
argument then a function call to constructor requires only two values to
execute.

Example:
class ABC Example
{ 2M
int a,b;
public:
ABC(int x,int y=2)
{
a=x;
b=y;
cout<<a<<b;
}
};

void main()
{
ABC p(10);// first call to constructor
}

In the above example, constructor ABC has two arguments. One of the
arguments has default value.
In first call, constructor executes and it initializes variable with value 10
and b with default value 2.
In second call, constructor executes and it initializes variable with value
10 and b with default value 20.
c) Explain constructor in derived class using one example. 4M
Ans. When a class is declared, a constructor can be declared inside
the class to initialize data members. When a base class contains a
constructor with one or more arguments then it is mandatory for the Explana
derived class to have a constructor and pass arguments to the base class tion 2M
constructor. When both the derived and base classes contain
constructors, the base constructor is executed first and then the
constructor in the derived class is executed.

Page 14 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
The constructor of derived class receives the entire list of values as its
arguments and passes them on to the base constructors in the order in
which they are declared in the derived class.

General form to declare derived class constructor:


Derived-constructor (arglist1,arglist(D)):Base1(arglist1)
{
Body of derived class constructor
}
Derived constructor declaration contains two parts separated with colon
(:). First part provides declaration of arguments that are passed to the
derived constructor and second part lists the function calls to the base
constructors.
Example:

#include<iostream.h> Example
#include<conio.h> 2M
class base
{
int x;
public:
base(int a)
{
x=a;
cout<<"Constructor in base. x="<<x;
}
};
class derived: public base
{
int y;
public:
derived(int a,int b):base(a)
{
y=b;
cout<<"Constructor in derived.y="<<y;
}
};
int main()
{
clrscr();

Page 15 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
derived ob(2,3);
getch();
return 0;
}
In the above example, base class constructor requires one argument and
derived class constructor requires one argument. Derived class
constructor accepts two values and passes one value to base class
constructor.

d) Use the concept of operator overloading to overload unary ‘-‘ 4M


operator to negate value of variables.
Note: Any other correct logic shall be considered

Ans. # include <iostream.h>


#include<conio.h>
class unary
{ logic for
int x, y, z; operator
public: overload
void getdata (int a, int , int c); ing 3M
void display (void);
void operator - (); // overload unary minus.
}; Functio
void unary :: getdata (int a, int b, int c) n Call
{ 1M
x = a;
y = b;
z = c;
}
void unary :: display (void)
{
cout<< x << " " << y << " " << z << "\n";
}
void unary ::operator - ()
{
x = -x ;
y = -y ;
z = -z ;
}

Page 16 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
main ( )
{
clrscr();
unary u;
u.getdata(20, -30, 40);
cout<< " u : " ;
u. display ( ) ;
-u;
cout<< " u : " ;
u. display ( ) ;
}

e) Explain pointer to derived class with example. 4M


Ans. Pointers can be used to point to the base class objects and objects of
derived class. Pointers to objects of base class are compatible with
pointers to objects of a derived class. Single pointer variable can be
made to point objects belonging to different classes. Explana
If B is base and D is derived class then pointer declared as a pointer to tion 2M
B can also be a pointer to D.
Example:
B *cptr; // pointer to of class
B b; //Base object
D d; // Derived object
cptr=&b; //cptr store address of object b of base class
cptr=&d; //cptr store address of object d of derived class

It's not possible to access the public members of the derived class D by
using cptr.Using base class pointer to object cptr, only those members
inherited from B can be accessed and not the members that originally
belong to D. For a member of D has the same name as any of the
member of B, then reference to that member by cptr will always access
the base class member. While C++ allows a base pointer to point to any
object derived from that base, the pointer cannot be directly used to
access all the members of the derived class.
Example:
#include<iostream.h>
#include<conio.h>
class base
{
int a;

Page 17 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
public:
void get()
{ Example
cout<<"\nEnter a value for A"; 2M
cin>>a;
}
void put()
{
cout<<"\nThe Value for A "<<a;
}
};
class derived : public base
{
int a;
public:
void get()
{
cout<<"\nEnter a value for A";
cin>>a;
}
void put()
{
cout<<"\nThe Value for A "<<a;
}
};
void main()
{
base *bptr,b;
derived d;
clrscr();
cout<<"\nPointing to the base class ";
bptr=&b;
bptr->get();
bptr->put();
cout<<"\nPointing to the derived class ";
bptr=&d;
bptr->get();
bptr->put();
getch();
}

Page 18 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
f) Differentiate between function definition inside and outside the 4M
class (any four points).
Ans. Sr. Inside function definition Outside function definition
No
1 A member function of a class A member function of a class Any
is defined inside the class. is declared inside class and as four
defined outside the class. relevant
2 The declaration is followed by After a member function is points
the definition of a function declared inside the class, it 1M each
inside the class definition. must be defined (outside the
class) in the program.
3 The definition of member The definition of member
function inside the class is function outside the class
like normal function differs from normal function
definition. definition, as the function
name in the function header is
preceded by the class name
and the scope resolution
operator (: :).
4 No need of scope resolution The scope resolution operator
operator. informs the compiler what
class the member belongs to.
5 The syntax for defining a The syntax for defining a
member function inside the member function outside the
class is class is
Return_type function Return_typeclass_name ::
name(parameter_list) function_name
{ (parameter_list)
// Body of the member {
function // body of the member function
} }
6 Example: Example:
class item class item
{ {
int number; int number;
float cost; float cost;
public: public:
void putdata(void) void getdata (int a float
{ b);//declaration
cout<< number <<endl; };

Page 19 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
cout<< cost <<endl; void item :: getdata(int a, float
} b)
}; {
number = a;
cost = b;
}

4. Attempt any four of the following: 16


a) Implement single inheritance for following fig. Accept and display 4M
data for 1 table.

Note: Any other correct logic shall consider

Ans. #include<iostream.h>
class furniture
{ Class
protected: furnitur
char material[20]; e 1M
int price;
}; Class
class table :public furniture table 1M
{
int height ; accept
float sur_area; and
public: display
void getdata() data 2M
{
cout<<"enter material";

Page 20 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
cin>>material;
cout<<"enter price";
cin>>price;
cout<<"enter height";
cin>>height;
cout<<"enter surface area";
cin>>sur_area;
}

void putdata()
{
cout<<" material is"<<material<<endl;
cout<<"price is"<<price<<endl;
cout<<" height is "<<height<<endl;
cout<<"surface area is "<<sur_area<<endl;
}
};
void main()
{
table t1;
t1.getdata();
t1.putdata();
}
b) Describe constructor with syntax and example. 4M
Note: Any constructor type shall be considered
Ans. A constructor is a special member function whose task is to initialize the
objects of its class. It is special because its name is same as the class Explana
name. The constructor is invoked whenever an object of its associated tion 2M
class is created. It is called constructor because it constructs the value
data members of the class.

Syntax:
class_name( ) Correct
{ syntax
Constructor body 1M
}

Example:
class ABC
{

Page 21 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
int a;
public: Example
ABC( )//constructor declaration 1M
{
a=0;
}
};

void main()
{
ABC x;
}
c) Explain insertion and extraction operators in C++ with example. 4M
Ans. Insertion operator:
The operator << is called as insertion operator works with cout to Explana
inserts the contents of the variable on screen (for output). tion with
Example: example
cout<<”Welcome to C++”; //Message is displayed on screen as it is. for
OR insertion
cout<<x;// Value of x will be printed on console / screen. 2M

Extraction operator:
The operator >>is called as extraction operator or get from extracts the
value from keyboard and assigns it to the variable on its right.
Extraction operator is used with cin statement to accept input from user For
(keyboard). Extracti
Example: on
cin>>number1; operator
2M
d) List characteristics of static data member and static member 4M
function.
Ans. Characteristics of static member variable are:
i) It is initialized to zero when the first object of its class is created. No
other initialization is permitted. Charact
ii) Only one copy of that member is created for the entire class and is eristics
shared by all the objects of that class, no matter how many objects are of static
created. data
iii) It is visible only within the class, but its lifetime is the entire member
program. 2M

Page 22 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
iv) Static variables are normally used to maintain values common for all
objects. Static
Characteristics of Static member function are: member
i)A static member function can only have access to other static data function
members and functions declared in the same class. 2M
ii)A static member function can be called using the class name with a
scope resolution operator instead of object name as follows:
class_name::function_name;
e) Explain hybrid inheritance with example. 4M
Ans. Hybrid inheritance is also referred as mixed inheritances. As the name
suggests it is a combination of all the kinds of inheritance mechanisms, Explana
namely single inheritance, multiple inheritance, multilevel inheritance tion 2M
and hierarchical inheritance.

Example:

#include<iostream.h>
class A
{
protected:
int a; Example
}; 2M
class B:public virtual A
{
protected:
int b;
};
class C:public virtual A
{
protected:
int c;
};
class D:public B,public C

Page 23 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
{
public:
void getdata()
{
cin>>a>>b>>c;
}
void putdata()
{
cout<<a<<b<<c;
}
};
void main()
{
D d;
d.getdata();
d.putdata();
}
f) Write a program to search a number from an array using pointer to 4M
array.
Ans. #include<iostream.h>
#include<conio.h>
void main()
{
int a[5],i,*a1,no,flag=1;
clrscr();
a1=&a[0];
cout<<"\nEnter array elements :"<<endl;
for(i=0;i<5;i++) Correct
{ logic 2M
cout<<"\n\t Enter "<<i<<" element:";
cin>>*a1;
a1++;
}
cout<<"Enter element to be searched::"; Correct
cin>>no; Syntax
a1=&a[0]; 2M
for(i=0;i<5;i++)
{
if(*a1==no)
{

Page 24 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
cout<<"\n\t Number is present...";
flag=1;
break;
}
else
{
flag=0;
a1++;
}
}
if(flag==0)
{
cout<<"\n\t Number is not present.... ";
}
getch();
}
5. Attempt any four of the following: 16
a) Write any four rules for operator overloading. 4M
Ans. Rules for operator overloading:
1. Only existing operators can be overloaded. New operators cannot be
created.
2. The overloaded operator must have at least one operand that is of
user defined type.
3. We cannot change the basic meaning of an operator i.e. we cannot
redefine the plus(+) operator to subtract one value from the other.
4. Overloaded operators follow the syntax rules of the original
operators. They cannot be overridden. Any
5. There are some operators that cannot be overloaded. for e.g. sizeof, . four
, .* , : : , ?: rules
6. We cannot use friend functions to overload certain operators (=,( ),[ 1M each
],->).However member functions can be used to overload them.
7. Unary operators overloaded by means of a member function, take no
explicit arguments and return no explicit values, but those
overloaded by means of a friend function, take one reference
argument.
8. Binary operators overloaded through a member function take one
explicit argument and those which are overloaded through a friend
function take two explicit arguments.
9. When using binary operators overloaded through a member function,
the left hand operand must be an object of the relevant class.

Page 25 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
10. Binary arithmetic operators such as +,-,* and / must explicitly return
a value. They must not attempt to change their own arguments.

b) Explain structure with syntax and example. 4M


Ans. Structure:
The Structure is a user defined data supported by object oriented
programming.
It has almost similar properties that any other user defined data type
possess except all members are public by default. Explana
One can create a structure using following syntax: tion with
syntax
struct structure_name 2M
{
data_member1;
data_member2 ;
.
.
data_memberN;
};

Example:
Example
#include<iostream.h> 2M
#include<conio.h>
struct demo
{
int a;
};
void main()
{
demo d;
clrscr();
cout<<"\nEnter a value for demo's a";
cin>>d.a;
cout<<"\nThe Value is "<<d.a;
getch();
}

Page 26 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
c) Compare run-time and compile-time polymorphism. 4M
Ans. Sr. Run-time Polymorphism Compile-time Polymorphism
No
1. It simply means that It simply means that an object is
selection of appropriate bound to its function call at Any 4
function is done at run time. compile time. Points
2. Function to be called is Functions to be called are know of
unknown until appropriate well before. compari
selection is made. son 1M
each
3. This requires use of pointers This does not require use of
to object. pointers to objects.
4. Function call execution is Function calls are faster.
slower.
5. Also called as late binding. Also called as early binding.
6. E.g. virtual function. E.g. overloaded function call.
7. It also referred as Dynamic It also referred as Static Binding.
Binding.
d) Explain any four concept of OOP. 4M
Ans. Basic Concepts of Object Oriented Programming:
1. Objects
Objects are the basic run time entities in an object-oriented system.
They may represent a person, a place, a bank account, a table of data or Any 4
any item that the program has to handle. An object is the instance of the Concept
class. When a program is executed, the objects interact by sending s 1M
messages to one another. each

2. Classes
A class is the collection of related data and function under a single
name. A class is collection of object of similar type. The entire set of
data and code of an object can be made a user-defined data type with
the help of class. Once a class has been defined, we can create any
number of objects belonging to that class. Classes are user-defined that
types and behave like the built-in types of a programming language.

3. Data Abstraction and Encapsulation


The wrapping up of data and function into a single unit (called class) is
known as encapsulation. The data is not accessible to the outside world,
and only those functions which are wrapped in the class can access it.
This insulation of the data from direct access by the program is called

Page 27 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
data hiding or information hiding. Abstraction refers to the act of
representing essential features without including the background details
or explanation. Classes use the concept of abstraction; they encapsulate
all the essential properties of the object that are to be created.

4. Inheritance
Inheritance is the process by which objects of one class acquired the
properties of objects of another classes. In OOP, the concept of
inheritance provides the idea of reusability. This means that we can add
additional features to an existing class without modifying it. This is
possible by deriving a new class from the existing one. The new class
will have the combined feature of both the classes.

5. Polymorphism
Polymorphism means the ability to take more than on form. An
operation may exhibit different behavior is different instances. For
example, consider the operation of addition. For two numbers, the
operation will generate a sum. If the operands are strings, then the
operation would produce a third string by concatenation.

6. Dynamic Binding
Binding refers to the linking of a procedure call to the code to be
executed in response to the call.

7. Message Passing
An object-oriented program consists of a set of objects that
communicate with each other. Objects communicate with one another
by sending and receiving information.

e) Describe pointer arithmetic with example. 4M


Ans. 1. As a pointer holds the memory address of a variable some arithmetic
operations can be performed with pointers. C++ supports four
arithmetic operators that can be used with pointer such as increment++
2. Pointers are variables. They are not integers, but they can be
displayed as unsigned integers. The conversion specifier for a pointer is Descript
added and subtracted. ion 2M
For example:
Ptr++: causes the pointer position to be incremented, but not by 1.
Ptr --: the pointer position to be decremented, but not by 1.

Page 28 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
3. Following program segment shows the pointer arithmetic.
The integer value would occupy bytes 2000 and 2001
int value, * ptr; value=120;
ptr=&value;
ptr ++;
cout<<ptr;

Example:

#include<iostream.h>
#include<conio.h>
void main()
{ Example
int value; 2M
int*ptr;
ptr= &value;
cout<<”memory address before increment=”;
cout<<ptr<<endl; ptr++;
cout<<” memory address after increment=”;
cout<<ptr<<endl;
}
memory address before increment=0X24c8ff4
memory address after increment=0X24c8ff5

f) Write a program to calculate area of circle and area of rectangle 4M


using function overloading.
Each
Ans. #include<iostream.h> area
#include<conio.h> function
float area(float a) 1M
{ calling
return (3.14*a*a); function
} with
int area(int p,int q) valid
{ argumen
return(p*q); t 1M
}
void main() Main
{ function
clrscr(); 1M

Page 29 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
cout<<"Area of circle:"<<area(6);
cout<<"Area of Rectangle:"<<area(5,6);
getch();
}

6. Attempt any two of the following: 16


a) Write a program to declare a class 'staff' having data members as 8M
name and department. Accept this data for 10 staffs and display
names of staff that are in 'CO' department.
Ans. #include<iostream.h>
#include<conio.h> Defining
#include<string.h> class
class staff with
{ specifica
char name[10], dept[10]; tions 4M
public:
void accept() Displayi
{ ng
cout<<"Enter Name and Department:\t"; values
cin>>name>>dept; for
} conditio
void display() n 2M
{
if(strcmp(dept,"CO")==0|| strcmp(dept,"co")==0)
{ Creating
cout<<"\nStaff name::\t"<<name<<"\t"<<"Department is ::"<<dept; 10
} Objects
} 1M
};
void main() Calling
{ Functio
staff s[10]; ns 1M
int i;
clrscr();
for(i=0;i<=10;i++)
{
s[i].accept();
}
for(i=0;i<=10;i++)

Page 30 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
{
s[i].display();
}
getch();
}
b) Write a program to implement the concept of virtual base class for 8M
following figure. Accept and display information of one employee
with his name, code, basic pay, experience and gross salary with the
object of employee class.

Ans. #include<iostream.h>
#include<conio.h>
class Master
{
char name[10],code[3];
public:
void acceptM() Each
{ Class 1
cout<<"\nEnter name and code "; ½M
cin>>name>>code;
}
void displayM()
{
cout<<"\nThe name and code is"<<name<<code;
} Main
}; function
class Account : public virtual Master 2M

Page 31 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
{
float basic_pay;
public:
void virtual acceptA()
{
cout<<"\nEnter Basic Pay ";
cin>>basic_pay;
}
void virtual displayA()
{
cout<<"\nThe Basic Pay is"<<basic_pay;
}
};
class Admin : public virtual Master
{
float experience;
public:
void virtual acceptD()
{
cout<<"\nEnter Experience ";
cin>>experience;
}
void virtual displayD()
{
cout<<"\nThe Experience is"<<experience;
}
};
class Employee : public Admin, public Account
{
float gross_sal,da;
public:
void acceptE()
{
cout<<"\nEnter Gross Salary ";
cin>>gross_sal;
}
void displayE()
{
cout<<"\nThe Gross Salary is "<<gross_sal;
}

Page 32 / 33
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming Subject Code: 17432
};
void main()
{
Employee e;
clrscr();
e.acceptM();
e.acceptA();
e.acceptD();
e.acceptE();
e.displayM();
e.displayA();
e.displayD();
e.displayE();
getch();
}
c) Write a program to find length of a string using pointer to string. 8M

Ans. #include<iostream.h>
#include<conio.h> Pointer
void main() creation
{ and
char str1[10],*ptr; acceptin
int len=0; g string
cout<<"enter string:"; 2M
cin>>str1;
ptr=&str1[0]; Pointer
while(*ptr!='\0') to initial
{ position
len++; 1M
ptr++;
} Calculat
cout<<"\nThe Length of a string is"<<len; ion of
getch(); Length
} 4M
Display
length
1M

Page 33 / 33

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy