SRM OODP Unit 4 Study Guide 📖
Master all 7 crucial questions covering Generic Programming, Templates, Exception Handling & Advanced UML Diagrams.
Question 1: What is a Function Template? Explain with an example.
A Function Template allows you to write a single generic function that can work with different data types. Instead of writing multiple overloaded functions for int, float, or double, you create one template. The compiler automatically generates the correct function based on the arguments passed.
#include <iostream>
using namespace std;
// Defining a function template
template <typename T>
T add(T a, T b) {
return a + b;
}
int main() {
// Calling template with int
cout << "Int addition: " << add<int>(5, 3) << endl;
// Calling template with double
cout << "Double addition: " << add<double>(4.5, 2.3) << endl;
return 0;
}
Question 2: What is a Class Template? Explain with an example.
A Class Template allows you to create a single generic class that can handle any data type. The data type is passed as a parameter when an object of the class is instantiated. This is extremely useful for building data structures like Stacks, Queues, and Vectors.
#include <iostream>
using namespace std;
// Defining a class template
template <class T>
class Box {
private:
T data;
public:
void setData(T item) {
data = item;
}
T getData() {
return data;
}
};
int main() {
// Creating an object of Box that stores an integer
Box<int> intBox;
intBox.setData(100);
cout << "Integer Box contains: " << intBox.getData() << endl;
// Creating an object of Box that stores a string
Box<string> strBox;
strBox.setData("Orygyns");
cout << "String Box contains: " << strBox.getData() << endl;
return 0;
}
Question 3: What is Exception Handling? Explain with an example.
Exception Handling is a mechanism to handle runtime errors (anomalies) so that the normal flow of the program is maintained and the program doesn't crash abruptly. It relies on three keywords: try, throw, and catch.
- try: Represents a block of code that may throw an exception.
- throw: Used to throw an exception when an error is detected.
- catch: Represents a block of code that handles the thrown exception.
#include <iostream>
using namespace std;
int main() {
int numerator = 10;
int denominator = 0;
int result;
try {
if (denominator == 0) {
throw "Division by zero error!";
}
result = numerator / denominator;
cout << "Result: " << result << endl;
}
catch (const char* msg) {
cout << "Exception caught: " << msg << endl;
}
return 0;
}
Question 4: What are the Types of Exceptions in C++?
Exceptions in C++ are broadly classified into two categories based on their origin, and they can also be categorized by implementation.
| Type | Description |
|---|---|
| Synchronous Exceptions | Exceptions that occur directly as a result of executing a program instruction. Examples include division by zero, out-of-bounds array access, or memory allocation failure. They are predictable. |
| Asynchronous Exceptions | Exceptions caused by events beyond the control of the program, such as hardware interrupts, keyboard interrupts (Ctrl+C), or disk failures. Standard C++ exception handling is not designed to handle these. |
| Built-in Exceptions | Exceptions derived from the standard <exception> library, such as std::bad_alloc or std::out_of_range. |
| User-Defined Exceptions | Custom exception classes created by the programmer (usually by inheriting from std::exception) to handle specific application logic errors. |
Question 5: What is a UML Package Diagram? Draw an example.
A Package Diagram organizes elements of a system into related groups to minimize dependencies between them. A package is rendered as a tabbed folder. It is used to simplify complex class diagrams by grouping classes into higher-level units.
+-------------------------+
| UI_Package |
+-------------------------+
| + WebInterface |
| + MobileInterface |
+-------------------------+
|
| <<import>>
v
+-------------------------+
| BusinessLogic_Package |
+-------------------------+
| + Authentication |
| + TransactionProcessing |
+-------------------------+
|
| <<access>>
v
+-------------------------+
| Database_Package |
+-------------------------+
| + MySQLConnector |
| + DataModels |
+-------------------------+
Question 6: What is a UML Component Diagram? Draw an example.
A Component Diagram models the physical aspects of an object-oriented system. It shows the organization and dependencies among a set of components (like executables, libraries, files, or documents). Components communicate through interfaces.
[ <<component>> ] [ <<component>> ]
[ OrderUI.exe ] --- ( ) ------- [ Billing.dll ]
^
| I_ProcessPayment
|
[ <<component>> ]
[ PaymentAPI ]
Question 7: What is a UML Deployment Diagram? Draw an example.
A Deployment Diagram models the physical deployment of artifacts on nodes. Nodes are hardware devices or software execution environments (like a Web Server or a Database Server). It shows how software is distributed across hardware.
<<device>> <<device>>
+------------------+ +------------------+
| Client PC | | Web Server |
| | | |
| [ <<artifact>> ] | --- TCP/IP ---> | [ <<artifact>> ] |
| [ Browser ] | | [ Apache Web ] |
+------------------+ +------------------+
|
| JDBC/SQL
v
<<device>>
+------------------+
| Database Server |
| |
| [ <<artifact>> ] |
| [ MySQL DB ] |
+------------------+
🔥 SRM Important Preparation
Most Expected Questions Checklist
- Function template with example
- Class template with example
- Exception handling with example
- Types of exceptions
- Draw UML Package Diagram
- Draw UML Component Diagram
- Draw UML Deployment Diagram
Memory Tricks 🧠
Function Template → Generic Function
Class Template → Generic Class
Exception Handling → Try Throw Catch
Component → Software (Executables, DLLs, Files)
Deployment → Hardware (Servers, PCs, Networks)