0% found this document useful (0 votes)
9 views21 pages

Unit 3

Oosd

Uploaded by

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

Unit 3

Oosd

Uploaded by

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

Previous year Questions :

2023-24

1. What do you mean by the optimization of design? (2 marks)

Optimization of Design refers to improving a system's design to achieve the best possible
performance, efficiency, and functionality while minimizing costs, errors, and resource usage. This
involves refining the design to meet the required specifications with the least complexity and
maximum usability.

Key Objectives:

 Minimize resource consumption (memory, CPU, etc.).

 Improve response time and efficiency.

 Enhance user experience and maintainability.

2. What do you mean by documentation? What are the various considerations in documentation
designing? Explain. (10 marks)

Documentation:
Documentation is the process of creating and maintaining written records, guides, and instructions
about a system, software, or process. It ensures that users, developers, and stakeholders can
understand, operate, and maintain the system effectively.

Types of Documentation:

1. User Documentation: Guides for end-users on how to use the software.

2. Technical Documentation: Details for developers or system administrators about the system’s
functionality, structure, and implementation.

3. Process Documentation: Details about the workflows, methodologies, and procedures


followed during development.

Considerations in Documentation Designing:

1. Audience Analysis: Understand who will use the documentation (e.g., developers, end-
users).

2. Clarity and Simplicity: Use simple language, avoiding technical jargon for non-technical users.

3. Structure and Layout: Use clear headings, subheadings, and sections for better navigation.

4. Accuracy and Completeness: Ensure all required information is included and up-to-date.

5. Consistency: Maintain a consistent style and format throughout the document.

6. Modularity: Break down content into manageable, independent sections.

7. Use of Visual Aids: Diagrams, flowcharts, and screenshots improve comprehension.

8. Accessibility: Ensure the documentation is accessible in various formats (PDF, online, etc.).
3. What is Data Abstraction? How is it different from Encapsulation? Explain with proper examples.
(10 marks)

Data Abstraction:
Data abstraction is the process of hiding the internal implementation details of a system or object
and exposing only the essential features to the user. It focuses on "what" the system does rather
than "how" it does it.

Example of data abstraction-


A TV remote is an abstraction; the user knows which buttons to press for specific actions but does
not need to understand the internal circuit or mechanism.

Encapsulation:
Encapsulation refers to wrapping data (variables) and methods (functions) into a single unit, usually a
class, and restricting direct access to some of the object’s components. It emphasizes controlling
access to the internal state of an object.

Key Differences between Data Abstraction and Encapsulation:

Feature Data Abstraction Encapsulation

Hides implementation details, shows


Focus Protects and controls access to data.
essential features.

Achieved using abstract classes or Achieved using access modifiers


Implementation
interfaces. (private, public).

To secure the internal state of the


Purpose To highlight what an object does.
object.

Use of private variables and public


Example in Code Abstract classes or interfaces in OOP.
getters/setters.

Code Example:

java

Copy code

// Data Abstraction using Abstract Class

abstract class Shape {

abstract void draw(); // Abstract method

class Circle extends Shape {

void draw() {

System.out.println("Drawing a Circle");
}

// Encapsulation Example

class Account {

private int balance; // Encapsulated field

public void setBalance(int amount) { // Setter to modify balance

if (amount > 0) balance = amount;

public int getBalance() { // Getter to access balance

return balance;

In the above examples:

 Data Abstraction: The user only knows about the draw() method and not the internal
implementation.

 Encapsulation: The balance field is protected, and access is controlled using getters and
setters.

4. How is it different from multiple inheritance and modeled using nested generalization? (2
marks)

Multiple Inheritance:

 Multiple inheritance is when a class inherits features from more than one parent class.

 Example: A class inheriting properties from two base classes like A and B.

Nested Generalization:

 Nested generalization models hierarchical relationships within a class structure, such as a


base class and multiple derived classes.

 Example: Generalizing a base class "Vehicle" into "Car" and "Bike," which further branch into
more specific types.
Difference:
Nested generalization uses inheritance to create a tree-like structure, ensuring clarity and avoiding
the complexity of multiple inheritance where conflicts may arise due to the diamond problem.

5. Define and differentiate Procedural and OOP with example. (2 marks)

Procedural Programming:

 Focuses on procedures (functions) to perform tasks.

 Example: C programming.

Object-Oriented Programming (OOP):

 Organizes code around objects, encapsulating data and behavior.

 Example: Java, Python.

Key Differences:

Aspect Procedural Programming Object-Oriented Programming

Focus Functions (procedures). Objects containing data and behavior.

Data Security Data is globally accessible. Data is protected via encapsulation.

Code Reusability Less reusable, relies on functions. More reusable through inheritance.

Example printf() in C. cout object in C++.

6. Prepare a DFD for computing the volume and surface area of a cone. Discuss some ways of
specifying operations. (10 marks)

Data Flow Diagram (DFD):

 Inputs: Height (h) and Radius (r).

 Outputs: Volume and Surface Area.

 Process: Calculation based on the formulas.

o Volume = 13πr2h\frac{1}{3} \pi r^2 h31πr2h

o Surface Area = πr(r+l)\pi r (r + l)πr(r+l), where l=r2+h2l = \sqrt{r^2 + h^2}l=r2+h2.

DFD Representation:

[User Input]

[Enter Height (h) and Radius (r)]

[Calculation Process]

→ [Volume]

→ [Surface Area]

Ways to Specify Operations:

1. Functional Specification: Clearly define the formulas or logic used for computation.

2. Pseudocode: Write a step-by-step algorithm.

3. Flowcharts: Visual representation of the calculation logic.

7. What do you understand by a static member function of a class? Discuss their characteristics.
Give an example where you can justify the use of static member functions. (10 marks)

Static Member Function:

 A static member function is associated with the class rather than any specific object.

 It can access only static data members of the class.

Characteristics:

1. Defined using the static keyword.

2. Can be called without creating an object of the class.

3. Cannot access non-static members of the class directly.

4. Shared among all objects of the class.

Example:

#include<iostream>

using namespace std;

class Counter {

static int count; // Static variable

public:

static void increment() { // Static member function

count++;

static int getCount() {

return count;

}
};

int Counter::count = 0;

int main() {

Counter::increment(); // Accessing static function without object

cout << "Count: " << Counter::getCount() << endl;

return 0;

Justification:
Static functions are ideal for maintaining shared properties or behaviors, like counting the number of
objects created or logging system-wide data.

8. Differentiate: (10 marks)

i. SA/SD (Structured Analysis and Structured Design) vs. OMT (Object Modeling Technique):

Aspect SA/SD OMT

Approach Process-oriented (focus on data flow). Object-oriented (focus on objects).

Tools DFD, flowcharts. Class diagrams, state diagrams.

Abstraction Functional decomposition. Encapsulation and inheritance.

Focus What the system does. What the system contains and behaves.

Use Case Suitable for small, procedural systems. Suitable for large, complex systems.

ii. SA/SD (Structured Analysis and Structured Design) vs. JSD (Jackson System Development):

Aspect SA/SD JSD

Origin Rooted in process modeling. Based on data stream transformations.

Focus Functional aspects. Sequential aspects of data processing.

Tools DFD, data dictionaries. System specifications, entity lifecycle.

Application General-purpose design methodology. Event-driven, real-time applications.

9. What are Constructors in Object-Oriented Programming? Explain their types with examples. (10
marks)

Constructors:
A constructor is a special member function in a class that is automatically called when an object of
the class is created. It initializes the object’s data members.

Characteristics:
1. Same name as the class.

2. No return type (not even void).

3. Automatically invoked.

Types of Constructors:

1. Default Constructor:

o A constructor with no parameters.

o Example:

Code

class Demo {

int x;

public:

Demo() { x = 0; } // Default Constructor

void show() { cout << "x = " << x; }

};

2. Parameterized Constructor:

o Accepts parameters to initialize object properties.

o Example:

Code

class Demo {

int x;

public:

Demo(int val) { x = val; } // Parameterized Constructor

void show() { cout << "x = " << x; }

};

3. Copy Constructor:

o Initializes an object by copying another object of the same class.

o Example:

Code

class Demo {

int x;

public:
Demo(int val) { x = val; }

Demo(const Demo &obj) { x = obj.x; } // Copy Constructor

void show() { cout << "x = " << x; }

};

10. What are the advantages of Object-Oriented Programming (OOP)? How does it differ from
Structured Programming? (10 marks)

Advantages of OOP:

1. Reusability: Use of classes and inheritance reduces redundancy.

2. Modularity: Divides software into manageable, independent objects.

3. Data Security: Encapsulation hides data from outside interference.

4. Scalability: Facilitates easy addition of new features.

5. Real-World Modeling: Models complex systems as objects with attributes and behaviors.

Difference between OOP and Structured Programming:

Aspect Structured Programming OOP

Focus Procedures and functions. Objects and their interactions.

Modularity Based on functions. Based on classes and objects.

Code Reusability Difficult to reuse code. High reusability through inheritance.

Data Security Data is globally accessible. Data is secure due to encapsulation.

Examples C, Pascal. C++, Java, Python.

11. Explain the concept of Inheritance in Object-Oriented Programming. What are its types?
Provide examples. (10 marks)

Inheritance:
Inheritance allows a class (child class) to acquire properties and behaviors of another class (parent
class). It promotes code reuse and creates a hierarchical relationship between classes.

Types of Inheritance:

1. Single Inheritance: One child class inherits from one parent class.

o Example:

Code

class Parent {

public: void display() { cout << "Parent Class"; }


};

class Child : public Parent {};

2. Multilevel Inheritance: A class inherits from another class, which in turn inherits from
another.

o Example:

Code

class Grandparent {

public: void show() { cout << "Grandparent Class"; }

};

class Parent : public Grandparent {};

class Child : public Parent {};

3. Hierarchical Inheritance: Multiple classes inherit from a single parent class.

o Example:

Code

class Parent {

public: void display() { cout << "Parent Class"; }

};

class Child1 : public Parent {};

class Child2 : public Parent {};

4. Multiple Inheritance: A class inherits from more than one parent class.

o Example:

Code

class Parent1 {

public: void display1() { cout << "Parent1 Class"; }

};

class Parent2 {

public: void display2() { cout << "Parent2 Class"; }

};

class Child : public Parent1, public Parent2 {};

5. Hybrid Inheritance: Combines two or more types of inheritance.


12. Define Polymorphism. What are its types? Explain with examples. (10 marks)

Polymorphism:
Polymorphism allows the same function or operator to behave differently based on the context. It
enhances flexibility and reusability in programs.

Types of Polymorphism:

1. Compile-Time Polymorphism (Static Binding):

o Achieved through function overloading and operator overloading.

o Example:

Code

class Demo {

public:

void show(int x) { cout << "Integer: " << x; }

void show(float y) { cout << "Float: " << y; }

};

2. Run-Time Polymorphism (Dynamic Binding):

o Achieved through function overriding.

o Example:

Code

class Parent {

public: virtual void display() { cout << "Parent Class"; }

};

class Child : public Parent {

public: void display() override { cout << "Child Class"; }

};

13. What is a Virtual Function? Explain its need and working with an example. (10 marks)

Virtual Function:
A virtual function is a member function in a base class that can be overridden in a derived class. It
ensures that the function called is determined by the object type (runtime polymorphism), not by
the pointer type.

Need for Virtual Function:

 It allows achieving runtime polymorphism.

 Ensures flexibility in cases where the object’s actual type is determined at runtime.
Working of Virtual Function:
When a base class declares a function as virtual, the compiler creates a vtable (virtual table), and
each derived class has its own entry. At runtime, the vtable is used to resolve which function to call.

Example:

cpp

Copy code

#include <iostream>

using namespace std;

class Base {

public:

virtual void show() { // Virtual function

cout << "Base class" << endl;

};

class Derived : public Base {

public:

void show() override { // Overriding the virtual function

cout << "Derived class" << endl;

};

int main() {

Base *ptr;

Derived obj;

ptr = &obj;

ptr->show(); // Calls the Derived class's show()

return 0;

Output:
Derived class
14. What is a Friend Function? Explain its purpose with an example. (10 marks)

Friend Function:
A friend function is a non-member function that can access the private and protected members of a
class. It is declared using the friend keyword inside the class definition.

Purpose:

1. Provides access to private data of a class to functions that are not its members.

2. Facilitates operations that require access to multiple classes' private members.

Example:

cpp

Copy code

#include <iostream>

using namespace std;

class Demo {

private:

int data;

public:

Demo(int val) : data(val) {}

friend void display(const Demo &obj); // Friend function declaration

};

void display(const Demo &obj) { // Friend function definition

cout << "Data: " << obj.data << endl;

int main() {

Demo obj(10);

display(obj); // Accessing private data through a friend function

return 0;

}
Output:
Data: 10

15. Write a short note on Exception Handling in C++. Explain try, catch, and throw with an example.
(10 marks)

Exception Handling in C++:


Exception handling is a mechanism to handle runtime errors gracefully without crashing the
program. It uses three main keywords:

 try: Contains the code block where an exception might occur.

 throw: Used to signal the occurrence of an exception.

 catch: Handles the exception thrown by the throw statement.

Syntax:

cpp

Copy code

try {

// Code that may throw an exception

throw exception;

} catch (type ex) {

// Code to handle the exception

Example:

cpp

Copy code

#include <iostream>

using namespace std;

void divide(int a, int b) {

if (b == 0)

throw "Division by zero error!"; // Throwing an exception

cout << "Result: " << a / b << endl;

}
int main() {

try {

divide(10, 2);

divide(10, 0); // This will cause an exception

} catch (const char* msg) { // Catching the exception

cout << "Exception caught: " << msg << endl;

return 0;

Output:
Result: 5
Exception caught: Division by zero error!

16. What are Design Patterns? Explain any two design patterns with examples. (10 marks)

Design Patterns:
Design patterns are reusable solutions to common software design problems. They are templates
that can be applied to specific problems in object-oriented design.

Categories of Design Patterns:

1. Creational Patterns: Deal with object creation mechanisms (e.g., Singleton, Factory).

2. Structural Patterns: Focus on object composition and relationships (e.g., Adapter,


Composite).

3. Behavioral Patterns: Deal with object interaction and responsibilities (e.g., Observer,
Strategy).

Example 1: Singleton Pattern (Creational)

 Ensures a class has only one instance and provides a global point of access to it.

Code

#include <iostream>

using namespace std;

class Singleton {

private:

static Singleton *instance; // Static instance

Singleton() {} // Private constructor


public:

static Singleton* getInstance() {

if (!instance)

instance = new Singleton();

return instance;

};

Singleton* Singleton::instance = nullptr;

int main() {

Singleton *s1 = Singleton::getInstance();

Singleton *s2 = Singleton::getInstance();

cout << (s1 == s2); // Output: 1 (both are the same instance)

return 0;

Example 2: Observer Pattern (Behavioral)

 Allows a subject to notify multiple observers about changes in its state.

code

#include <iostream>

#include <vector>

using namespace std;

class Observer {

public:

virtual void update(int temp) = 0;

};

class WeatherStation {

vector<Observer*> observers;

int temperature;

public:
void addObserver(Observer *obs) { observers.push_back(obs); }

void setTemperature(int temp) {

temperature = temp;

for (auto obs : observers)

obs->update(temp);

};

class Display : public Observer {

public:

void update(int temp) override {

cout << "Temperature updated: " << temp << "°C" << endl;

};

int main() {

WeatherStation station;

Display display1, display2;

station.addObserver(&display1);

station.addObserver(&display2);

station.setTemperature(25);

return 0;

17. Explain the relationship between classes with examples. (10 marks)

Class Relationships:
Relationships describe how classes interact or depend on each other in object-oriented design.

Types of Relationships:

1. Association: A "uses-a" relationship between two classes.

o Example: A Teacher teaches Students.

2. Aggregation: A "has-a" relationship where a class is made up of other classes but can exist
independently.
o Example: A Team has Players.

3. Composition: A stronger form of aggregation where the containing class cannot exist
without its components.

o Example: A Car is composed of an Engine.

4. Inheritance: A "is-a" relationship where one class inherits from another.

o Example: A Dog is an Animal.

Examples:

// Association Example

class Teacher {

public:

void teaches() { cout << "Teaches students" << endl; }

};

// Aggregation Example

class Player {};

class Team {

vector<Player*> players;

};

// Composition Example

class Engine {};

class Car {

Engine engine; // Engine is a part of Car

};

// Inheritance Example

class Animal {};

class Dog : public Animal {};

18. What are Interaction Diagrams? Explain Sequence Diagrams and Collaboration Diagrams. (10
marks)
Interaction Diagrams:
Interaction diagrams model the dynamic aspects of a system. They show how objects communicate
with each other.

Types of Interaction Diagrams:

1. Sequence Diagram: Focuses on the time order of messages exchanged between objects.

2. Collaboration Diagram: Focuses on the structural organization of objects and the


interactions among them.

Sequence Diagram Example:


A user logging into a system.

code

User --> LoginPage : Enter credentials

LoginPage --> Database : Validate credentials

Database --> LoginPage : Validation result

LoginPage --> User : Show result

Collaboration Diagram Example:


Same scenario as above, but focusing on relationships:

Code

+-------------------+

| User |

| ----------------- |

| - credentials |

| ----------------- |

| + enter() |

+-------------------+

+-------------------+

| LoginPage |

| ----------------- |

| - credentials |

| ----------------- |

| + validate() |

+-------------------+
|

+-------------------+

| Database |

| ----------------- |

| - records |

| ----------------- |

| + check() |

+-------------------+

19. What is the role of visibility in object design? Explain public, private, and protected with
examples. (10 marks)

Visibility in Object Design:


Visibility determines the accessibility of class members (attributes and methods) from other classes
or objects.

Access Specifiers:

1. Public: Members can be accessed from anywhere.

2. Private: Members are only accessible within the class.

3. Protected: Members are accessible within the class and its derived classes.

Example:

Code

#include <iostream>

using namespace std;

class Base {

private:

int privateVar; // Accessible only within the class

protected:

int protectedVar; // Accessible in derived classes

public:

int publicVar; // Accessible anywhere

};
class Derived : public Base {

public:

void show() {

// privateVar = 10; // Not accessible

protectedVar = 20; // Accessible

publicVar = 30; // Accessible

};

int main() {

Derived obj;

// obj.privateVar = 10; // Not accessible

// obj.protectedVar = 20; // Not accessible

obj.publicVar = 30; // Accessible

return 0;

20. Explain the concept of Object Design with reference to relationships and responsibility. (10
marks)

Object Design:
Object design focuses on how classes and objects interact to perform system functionality. It bridges
the gap between the analysis model and the implementation model.

Key Aspects of Object Design:

1. Relationships: Defines how classes interact (e.g., association, aggregation, inheritance).

2. Responsibilities: Assigning specific roles and tasks to objects based on the Single
Responsibility Principle (SRP).

Example:

 A Library system where:

o Book class stores book details.

o Member class handles member details.

o Transaction class manages borrowing and returning.


Code

class Book {

string title, author;

};

class Member {

string name, id;

};

class Transaction {

Member member;

Book book;

void borrowBook();

void returnBook();

};

You might also like