You are on page 1of 18

SOLUTION TO PAPER

SECTION A

1. Attempt all questions in brief. 2 x 7 = 14


a. Define Dynamic Binding.

Ans: The process of linking procedure call to a specific sequence of code at


run-time. It means that the code to be executed for a specific procedure call is
not known until run-time. Dynamic binding is also known as late
binding or run-time binding

b. Differentiate synchronous and asynchronous messages.

Ans: Synchronous messages, which are associated with an operation, have a send
and a receive message. A message is sent from the source lifeline to the target
lifeline. The source lifeline is blocked from other operations until it receives a
response from the target lifeline.
Asynchronous messages, which are associated with operations, typically have
only a send message, but can also have a reply message. In contrast to a
synchronous message, the source lifeline is not blocked from receiving or sending
other messages. You can also move the send and receive points individually to
delay the time between the send and receive events.

c. What will be the output of the program? Justify


#include<iostream>
using namespace std;
main() {
int const a = 5;
a++;
cout<<a;
}

Ans: Compile error. Because constant variable cannot modified.

d. Differentiate Call by reference and return by reference.

Ans:
call by value call by reference
In call by value, a copy of actual arguments is In call by reference, the location (address) of
passed to formal arguments of the called actual arguments is passed to formal
function and any change made to the formal arguments of the called function. This means
arguments in the called function have no effect by accessing the addresses of actual arguments
on the values of actual arguments in the calling we can alter them within from the called
function. function.
In call by value, actual arguments will remain In call by reference, alteration to actual
safe, they cannot be modified accidentally. arguments is possible within from called
function; therefore the code must handle
arguments carefully else you get unexpected
results.

e. What is the significance of access specifiers in a class?

Ans: Access modifiers are keywords in object oriented languages that set the accessibility of
classes, methods, and other members. There are 3 types of specifiers public, private and protected.

f. What is the use of typedef?


Ans: typedef gives an alternative name for predefine data type. Eg: typedef float Dec;
So now, instead of using float we can define float variables using Dec.

g. List the four kinds of relationship in UML.


Ans:
• Aggregation
• Association
• Inheritance
• Realization
• Composition

SECTION B

2. Attempt any three of the following: 7 x 3 = 21


a. What do you mean by an object? Discuss the various characteristics of object with
examples?

Ans:
Object is an instance of class. Class is an entity in which data and functions are organized.
Each object has a class which defines its data and behavior. When program is executed the
objects interact by sending messages to one another.
Object characterstics are:
-Encapsulation – Encapsulation is capturing data and keeping it safely and securely from
outside interfaces.
-Inheritance- This is the process by which a class can be derived from a base class with all
features of base class and some of its own. This increases code reusability.
-Polymorphism- This is the ability to exist in various forms. For example an operator can be
overloaded so as to add two integer numbers and two floats.
-Abstraction- The ability to represent data at a very conceptual level without any details.
- Emphasis on data rather than procedure
- Programs are divided into entities known as objects
- Data Structures are designed such that they characterize objects
- Functions that operate on data of an object are tied together in data structures
- Data is hidden and cannot be accessed by external functions
- Objects communicate with each other through functions
- New data and functions can be easily added whenever necessary
- Follows bottom up design in program design

b. Explain UML, why we use UML and draw architecture of UML.


Ans: UML diagram is a diagram based on the UML (Unified Modeling Language) with
the purpose of visually representing a system along with its main actors, roles, actions,
artifacts or classes, in order to better understand, alter, maintain, or document information
about the system.

The use of UML will help:

• The communication of the desired structure and behaviour of a system


between analysts, architects, developers, stakeholders and users.
• The visualisation and control of system architecture.
• Promote a deeper understanding of the system, exposing opportunities for
simplification and re-use.
• Manage risk.
Any real-world system is used by different users. The users can be developers, testers,
business people, analysts, and many more. Hence, before designing a system, the
architecture is made with different perspectives in mind. The most important part is to
visualize the system from the perspective of different viewers. The better we understand the
better we can build the system.

UML plays an important role in defining different perspectives of a system. These


perspectives are −

• Design

• Implementation

• Process

• Deployment

The center is the Use Case view which connects all these four. A Use Caserepresents the
functionality of the system. Hence, other perspectives are connected with use case.

Design of a system consists of classes, interfaces, and collaboration. UML provides class
diagram, object diagram to support this.

Implementation defines the components assembled together to make a complete physical


system. UML component diagram is used to support the implementation perspective.

Process defines the flow of the system. Hence, the same elements as used in Design are also
used to support this perspective.

Deployment represents the physical nodes of the system that forms the hardware. UML
deployment diagram is used to support this perspective.

c. What do you understand by Modeling? Explain the principles of modeling.


Ans
Modeling is a simplification of reality.
- Blueprint of the actual system.
- Specify the structural and behavior of the system.
- Templates for designing the system.
- Helps document the system.

1. The choice of what models to create has a profound influence on how a


problem is attacked and how a solution is shaped.
In other words, choose your models well. The right models will brilliantly
illuminate the most wicked development problems, offering insight that you
simply could not gain otherwise; the wrong models will mislead you, causing
you to focus on irrelevant issues.

2. Every model may be expressed at different levels of precision.


If you are building a high rise, sometimes you need a 30,000-foot viewfor
instance, to help your investors visualize its look and feel. Other times, you
need to get down to the level of the studsfor instance, when there's a tricky
pipe run or an unusual structural element.
3. The best models are connected to reality.
A physical model of a building that doesn't respond in the same way as do real
materials has only limited value; a mathematical model of an aircraft that
assumes only ideal conditions and perfect manufacturing can mask some
potentially fatal characteristics of the real aircraft. It's best to have models that
have a clear connection to reality, and where that connection is weak, to know
exactly how those models are divorced from the real world. All models
simplify reality; the trick is to be sure that your simplifications don't mask any
important details.
4. No single model or view is sufficient. Every nontrivial system is best
approached through a small set of nearly independent models with
multiple viewpoints
If you are constructing a building, there is no single set of blueprints that
reveal all its details. At the very least, you'll need floor plans, elevations,
electrical plans, heating plans, and plumbing plans. And within any kind of
model, you need multiple views to capture the breadth of the system, such as
blueprints of different floors.

d. Discuss array in c++ .Write a program of bubble sort in c++.

Ans: C++ provides a data structure, the array, which stores a fixed-size sequential
collection of elements of the same type. An array is used to store a collection of data,
but it is often more useful to think of an array as a collection of variables of the same
type.

To declare an array in C++, the programmer specifies the type of the elements and the
number of elements required by an array as follows –

Type arrayName [ arraysize];


#include <stdio.h>

void swap(int *xp, int *yp)


{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

void bubbleSort(int arr[], int n)


{
int i, j;
for (i = 0; i < n-1; i++)

for (j = 0; j < n-i-1; j++)


if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}

void printArray(int arr[], int size)


{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("n");
}
int main()
{
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}

e. Explain Type Casting in C++ and determine its types.

Ans: Converting an expression of a given type into another type is known as type-casting.

1. dynamic_cast

dynamic_cast can be used only with pointers and references to objects. Its purpose is to ensure
that the result of the type conversion is a valid complete object of the requested class.

therefore, dynamic_cast is always successful when we cast a class to one of its base classes:
class CBase { };
class CDerived: public CBase { };
CBase b; CBase* pb;
CDerived d; CDerived* pd;

pb = dynamic_cast<CBase*>(&d); // ok: derived-to-base


pd = dynamic_cast<CDerived*>(&b); // wrong: base-to-derived

2. static_cast
static_cast can perform conversions between pointers to related classes, not only from the
derived class to its base, but also from a base class to its derived. This ensures that at least the
classes are compatible if the proper object is converted, but no safety check is performed
during runtime to check if the object being converted is in fact a full object of the destination
type. Therefore, it is up to the programmer to ensure that the conversion is safe. On the other
side, the overhead of the type-safety checks of dynamic_cast is avoided.

class CBase {};


class CDerived: public CBase {};
CBase * a = new CBase;
CDerived * b = static_cast<CDerived*>(a)

3. reinterpret_cast
reinterpret_cast converts any pointer type to any other pointer type, even of unrelated classes.
The operation result is a simple binary copy of the value from one pointer to the other. All
pointer conversions are allowed: neither the content pointed nor the pointer type itself is
checked.
class A {};
class B {};
A * a = new A;
B * b = reinterpret_cast<B*>(a)

4 .const_cast
This type of casting manipulates the constness of an object, either to be set or to be removed.
For example, in order to pass a const argument to a function that expects a non-constant
paramete
#include <iostream>
using namespace std;

void print (char * str)


{
cout << str << endl;
}

int main () {
const char * c = "sample text";
print ( const_cast<char *> (c) );
return 0;
}

SECTION C
3. Attempt any one part of the following: 7x1=7
(a) Discuss JSD and its approach comparing with object modeling techniques.

Ans: JSD, Jackson Structure Design is a methodology to specify and design systems in which
time factor is significant and system may be described using sequence of events. Developed
by Michael A. Jackson , this design method considers the fact that the design of the system is
an extension of the programme design. The purpose of this design method is to create a
maintainable software. The method addresses all stages of the software development life
cycle. It has three phases:
Modeling phase - A JSD model starts with real world consideration. This phase is a part of
analysis process. The aspects of the real world relevant to the system being developed are
modeled in this phase.
Specification phase - This phase focuses on the specification. In this phase JSD determines
what is to be done? The previous phase i.e. the modeling phase provides the basic for the
system specifications to achieve the required functionality.
implementation phase - In this phase , JSD determines how to achieve required functionality.
Operational specifications are executed so that it expresses desired system behavior in terms
of some abstract machine.

Steps for JSD software development


Originally presented by Jackson in 1983 the method consisted of six steps which are
following.
• Entity /action step
• Entity structure step
• Initial model step
• function step
• system timing step
• system implementing step

(b) Show the difference between reference and pointers. Demonstrate pointer constant and
pointer to constant using example.

Ans:
4. Attempt any one part of the following: 7x1=7
(a) What do you mean by operator overloading? What are the pitfalls of operator
overloading? Explain in brief.

Ans In C++, we can make operators to work for user defined classes. For example, we
can overload an operator ‘+’ in a class like String so that we can concatenate two
strings by just using +.
Other example classes where arithmetic operators may be overloaded are Complex
Number, Fractional Number, Big Integer, etc.
#include<iostream>
using namespace std;

class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i =0) {real = r; imag = i;}
void print() { cout << real << " + i" << imag << endl; }
friend Complex operator + (Complex const &, Complex const &);
};
Complex operator + (Complex const &c1, Complex const &c2)
{
return Complex(c1.real + c2.real, c1.imag + c2.imag);
}

int main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2; // An example call to "operator+"
c3.print();
return 0;
}

1) For operator overloading to work, at leas one of the operands must be a user defined
class object.
2) Assignment Operator: Compiler automatically creates a default assignment
operator with every class. The default assignment operator does assign all members of
right side to the left side and works fine most of the cases (this behavior is same as
copy constructor).
3) Conversion Operator: We can also write conversion operators that can be used to
convert one type to another type.
PITFALLS
• Arity (number of operands) cannot be changed
• Associativity of an operator cannot be changed
• Precedence of an operator cannot be changed
• No new operators can be created
• No overloading operators for built-in types
• Cannot change how two integers are added
• Produces a syntax error

(b) What are the three models in OMT? How object oriented analysis and design attached with
OMT. Explain with example?
Ans The object-modeling technique (OMT) is an object modeling approach
for software modeling and designing. It was developed around 1991 by Rumbaugh as
a method to develop object-oriented systems and to support object-oriented
programming. OMT describes object model or static structure of the system.
OMT was developed as an approach to software development. The purposes of
modeling according to Rumbaugh are:

• testing physical entities before building them (simulation),


• communication with customers,
• visualization (alternative presentation of information), and
• reduction of complexity.
OMT has proposed three main types of models:

• Object model: The object model represents the static and most stable phenomena in
the modeled domain. Main concepts are classes and associations with attributes
and operations. Aggregation and generalization (with multiple inheritance) are
predefined relationships.
• Dynamic model: The dynamic model represents a state/transition view on the
model. Main concepts are states, transitions between states, and events to trigger
transitions. Actions can be modeled as occurring within states. Generalization and
aggregation (concurrency) are predefined relationships.
• Functional model: The functional model handles the process perspective of the
model, corresponding roughly to data flow diagrams. Main concepts are process,
data store, data flow, and actors.

The systems designed using OMT are closer to the real world as the real world
functioning of the system is directly mapped into the system designed using OMT.
Because of this, it becomes easier to produce and understand designs.

• The objects in the system are immune to requirement changes because of data hiding
and encapsulation features of object orientation.
• OMT designs encourage more reusability. The classes once defined can easily be
used by other applications. This is achieved by defining classes and putting them into a
library of classes where all the classes are maintained for future use. Whenever a new
class is needed the programmer first looks into the library of classes and if it is
available, it can be used as it is or with some modification. This reduces the
development cost & time and increases quality.
• As the programmer has to spend less time and effort so he can utilize saved time in
concentrating on other aspects of the system.
5. Attempt any one part of the following: 7x1=7
(a) Describe the concept of inheritance and its types with example in c++.

Ans: The capability of a class to derive properties and characteristics from another class is
called Inheritance. Inheritance is one of the most important feature of Object Oriented
Programming.
Sub Class: The class that inherits properties from another class is called Sub class or Derived
Class.
Super Class:The class whose properties are inherited by sub class is called Base Class or
Super class.

Syntax:
class subclass_name : access_mode base_class_name
{
//body of subclass
};
#include <bits/stdc++.h>
using namespace std;

class Parent
{
public:
int id_p;
};
class Child : public Parent
{
public:
int id_c;
};

int main()
{
Child obj1;

obj1.id_c = 7;
obj1.id_p = 91;
cout << "Child id is " << obj1.id_c << endl;
cout << "Parent id is " << obj1.id_p << endl;
return 0;
}
In C++, we have 5 different types of Inheritance. Namely,

1. Single Inheritance
2. Multiple Inheritance
3. Hierarchical Inheritance
4. Multilevel Inheritance
5. Hybrid Inheritance (also known as Virtual Inheritance)

Single Inheritance:
In this type of inheritance one derived class inherits from only one base class. It is the most
simplest form of Inheritance.

Multiple Inheritance
In this type of inheritance a single derived class may inherit from two or more than two base
classes.

Hierarchical Inheritance
In this type of inheritance, multiple derived classes inherits from a single base class.

Multilevel Inheritance
In this type of inheritance the derived class inherits from a class, which in turn inherits from
some other class. The Super class for one, is sub class for the other.

Hybrid Inheritance
Hybrid Inheritance is combination of Hierarchical and Mutilevel Inheritance.
(b) Illustrate Polymorphism in c++ with suitable example.

Ans The word polymorphism means having many forms. In simple words, we can define
polymorphism as the ability of a message to be displayed in more than one form.
Real life example of polymorphism, a person at a same time can have different characteristic.
Like a man at a same time is a father, a husband, a employee. So a same person posses have
different behavior in different situations. This is called polymorphism.
Polymorphism is considered as one of the important features of Object Oriented
Programming.
In C++ polymorphism is mainly divided into two types:
▪ Compile time Polymorphism
▪ Runtime Polymorphism
Example: #include <iostream>
using namespace std;

class Shape {
protected:
int width, height;

public:
Shape( int a = 0, int b = 0){
width = a;
height = b;
}
int area() {
cout << "Parent class area :" <<endl;
return 0;
}
};
class Rectangle: public Shape {
public:
Rectangle( int a = 0, int b = 0):Shape(a, b) { }

int area () {
cout << "Rectangle class area :" <<endl;
return (width * height);
}
};
class Triangle: public Shape {
public:
Triangle( int a = 0, int b = 0):Shape(a, b) { }

int area () {
cout << "Triangle class area :" <<endl;
return (width * height / 2);
}
};
int main() {
Shape *shape;
Rectangle rec(10,7);
Triangle tri(10,5);
shape = &rec;
shape->area();
shape = &tri;
shape->area();

return 0;
}

6. Attempt any one part of the following: 7x1=7


(a) Design a program of swapping in c++ using friend function and describe friend function in
detail.
Ans:
A friend function can be given special grant to access private and protected members. A friend function
can be:
a) A method of another class
b) A global function
#include<iostream>

using namespace std;

class Sample
{
private:
int x,y;
public:
void setdata(int a,int b)
{
x=a;
y=b;
}
void showdata()
{
cout<<"x="<<x<<"\ny="<<y;
}
friend void swap(Sample &s);
};

void swap(Sample &s)


{
int temp;
temp=s.x;
s.x=s.y;
s.y=temp;
}

int main()
{
Sample s;
int x,y;

cout<<"Enter x = ";
cin>>x;
cout<<"Enter y = ";
cin>>y;

s.setdata(x,y);

cout<<"\nBefore Swapping\n";
s.showdata();
cout<<"\nAfter Swapping\n";
swap(s);
s.showdata();

return 0;
}

(b)Explain the concept of Virtual function and Pure Virtual function.

Ans: A virtual function a member function which is declared within base class and is re-
defined (Overriden) by derived class. When you refer to a derived class object using a pointer
or a reference to the base class, you can call a virtual function for that object and execute the
derived class’s version of the function.
▪ Virtual functions ensure that the correct function is called for an object, regardless of the
type of reference (or pointer) used for function call.
▪ They are mainly used to achieve Runtime polymorphism
▪ Functions are declared with a virtual keyword in base class.
▪ The resolving of function call is done at Run-time.

#include<iostream>
using namespace std;

class base
{
public:
virtual void print ()
{ cout<< "print base class" <<endl; }

void show ()
{ cout<< "show base class" <<endl; }
};

class derived:public base


{
public:
void print ()
{ cout<< "print derived class" <<endl; }

void show ()
{ cout<< "show derived class" <<endl; }
};

int main()
{
base *bptr;
derived d;
bptr = &d;
bptr->print();
bptr->show();
}
Pure Virtual Function:

A pure virtual function is implemented by classes which are derived from a Abstract class.
Following is a simple example to demonstrate the same.
#include<iostream>
using namespace std;

class Base
{
int x;
public:
virtual void fun() = 0;
int getX() { return x; }
};

// This class inherits from Base and implements fun()


class Derived: public Base
{
int y;
public:
void fun() { cout << "fun() called"; }
};

int main(void)
{
Derived d;
d.fun();
return 0;
}
7. Attempt any one part of the following: 7x1=7
(a) Draw UML Timing Diagram for ATM machine.

(b) Draw UML Class Diagram for Library Management System.

You might also like