SRM OODP Unit 5 📘
Complete semester preparation notes for File Handling, STL Containers, and UML Revision with detailed explanations, top 10 crucial exam questions, viva preparation, and tips.
Question 1: What is File Handling in C++? Explain its main operations.
File handling in C++ is the mechanism used to store data permanently in a secondary storage device (like a hard drive) and retrieve it. It allows C++ programs to communicate with files using input and output streams.
Main File Operations
- Create File: Generates a new file in the system.
- Open File: Links the stream to the physical file.
- Read File: Fetches data from the file to the program.
- Write File: Pushes data from the program into the file.
- Close File: Safely disconnects the stream and saves changes.
PROGRAM ---> FILE ---> STORAGE
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream file; // Step 1: Create stream object
file.open("demo.txt"); // Step 2: Open file
file << "Hello SRM"; // Step 3: Write to file
file.close(); // Step 4: Close file
return 0;
}
Memory Trick 🧠
ofstream → Write (Output File Stream)
ifstream → Read (Input File Stream)
fstream → Both (Read & Write)
Question 2: What are Streams in C++? Explain their types.
A Stream is an abstraction that represents a device on which input and output operations are performed. A stream acts as a flow of data (sequence of bytes) between the executing program and I/O devices.
| Stream Object | Purpose |
|---|---|
| cin | Standard input stream (usually keyboard). |
| cout | Standard output stream (usually monitor/console). |
| ifstream | Input file stream class used to read data from files. |
| ofstream | Output file stream class used to write data to files. |
Main Stream Types
- Input Stream: Flows from a device (keyboard/file) into the main memory.
- Output Stream: Flows from main memory to a device (screen/file).
- File Stream: Specifically targets disk files to maintain persistent state.
Question 3: Explain the different File Opening Modes in C++.
File opening modes determine how a file will be processed once it is opened. They are defined inside the ios class.
| Mode Flag | Description |
|---|---|
| ios::in | Opens the file strictly for reading. |
| ios::out | Opens the file for writing. If the file exists, it overwrites the content. |
| ios::app | Opens the file in append mode. New data is written to the end of the file. |
| ios::binary | Opens the file in binary mode instead of the default text mode. |
Most Asked Exam Point 🎯
Difference between ios::out and ios::app:
ios::out will delete all existing data in the file before writing new data (truncation).
ios::app will preserve all existing data and safely add the new data at the very end of the file.
Question 4: Write a C++ program to Read and Write data from/to a file.
Files are interacted with using the <fstream> library by instantiating stream objects.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// WRITING TO A FILE
ofstream fout;
fout.open("data.txt");
fout << "Orygyns Educational Platform";
fout.close();
// READING FROM A FILE
ifstream fin;
string text;
fin.open("data.txt");
// Read string from file
fin >> text; // Reads word by word
cout << "Data read from file: " << text << endl;
fin.close();
return 0;
}
Advantages of File Handling
- Permanent data storage: Data isn't lost when the program ends.
- Data reuse: Same data can be accessed multiple times by different programs.
- Large data handling: Easy processing of massive datasets that won't fit entirely in RAM.
Question 5: What is the Standard Template Library (STL)? Explain its components.
The Standard Template Library (STL) is a powerful set of C++ template classes that provide general-purpose predefined classes and functions for implementing highly efficient data structures and algorithms (like arrays, lists, stacks, trees).
STL
|
|--- Containers
|--- Iterators
|--- Algorithms
Core Components of STL:
- Containers: Objects that store data elements. They manage the memory allocation for these elements. (e.g., Vector, List, Map).
- Algorithms: Procedures that perform operations on containers like searching, sorting, and reversing. (e.g.,
sort(),find()). - Iterators: Objects that act like pointers. They are used to step through or iterate over the elements of a container safely.
Question 6: Explain Vector in STL with an example.
A vector is a sequence container that represents a dynamic array. Its size can change automatically as elements are added or removed, making it superior to standard arrays.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v;
// Inserting elements
v.push_back(10);
v.push_back(20);
v.push_back(30);
// Accessing elements
cout << "First element: " << v[0] << endl; // Output: 10
cout << "Vector size: " << v.size() << endl; // Output: 3
return 0;
}
Advantages of Vectors
- Dynamic size resizing automatically.
- Easy insertion and deletion at the end using
push_backandpop_back. - Efficient memory usage and fast random access via indices.
Question 7: Explain List in STL with an example.
A list is a sequence container that allows non-contiguous memory allocation. It represents a doubly-linked list, which allows incredibly fast insertions and deletions anywhere within the sequence.
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> l;
// Inserting elements
l.push_back(10); // Adds to the end
l.push_back(20);
l.push_front(5); // Adds to the beginning
// Iterating through the list
cout << "List elements: ";
for(int x : l) {
cout << x << " ";
}
// Output: 5 10 20
return 0;
}
Easy Trick 🧠
Vector → Dynamic Array (Contiguous memory, fast access).
List → Linked List (Non-contiguous memory, fast insertion/deletion).
Question 8: Explain Map in STL with an example.
A map is an associative container that stores elements in Key-Value pairs. Keys must be strictly unique, and elements are automatically sorted by the key. It acts like a dictionary.
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int, string> m;
// Inserting data as Key-Value pairs
m[1] = "SRM";
m[2] = "Orygyns";
m[3] = "C++ Course";
// Accessing via Key
cout << "Value at key 1: " << m[1] << endl; // Output: SRM
return 0;
}
Main Features of Map
- Key-value storage mapping.
- Automatic sorting based on the key.
- Extremely fast searching for values based on their unique keys.
Question 9: What is the difference between Vector and List?
Knowing when to use a Vector versus a List is a critical concept in C++ STL programming.
| Vector (Dynamic Array) | List (Doubly Linked List) |
|---|---|
| Stores elements in contiguous memory blocks. | Stores elements in non-contiguous memory (nodes). |
Supports incredibly fast random access (e.g., v[3]). |
Does not support fast random access. Must be traversed sequentially. |
| Insertion/Deletion at the middle is slow (requires shifting elements). | Insertion/Deletion anywhere is extremely fast (just pointer manipulation). |
| Better cache locality and performance for reading data. | Better performance for inserting and removing large volumes of data randomly. |
Question 10: Provide a Comprehensive UML Revision.
UML (Unified Modeling Language) is crucial for visualizing the design of a system. Here is a summary of the diagrams required for SRM OODP.
| Diagram Type | Purpose | Key Elements |
|---|---|---|
| Use Case | Shows user interaction with the system. | Actors, Use Cases (Ovals), Boundaries. |
| Class Diagram | Shows the static structure of classes. | Classes, Attributes, Methods, Visibility (+,-,#). |
| Sequence Diagram | Shows message flow ordered by time. | Lifelines, Activation Boxes, Messages. |
| Activity Diagram | Shows workflow or process logic. | Action nodes, Control flows, Decision diamonds. |
| Component Diagram | Shows structural software components. | Components, Interfaces, Dependencies. |
| Deployment Diagram | Shows physical hardware structure. | Nodes (Servers, PCs), Artifacts, Connections. |
System Integration Mapping
A fundamental visualization of how the User, System logic, and File/Database integrate in a software architecture.
USER ---> SYSTEM ---> DATABASE
🔥 SRM Important Preparation Checklist
Most Expected Questions
- File handling operations with detailed examples.
- Complete program to Read and Write to a text file.
- Functions and exact differences between stream types.
- Explain the architecture of STL.
- Provide a working Vector insertion example.
- Provide a working List iteration example.
- Demonstrate Map key-value pair implementation.
- Explain 4 differences between vector and list.
Quick Memory Tricks 🧠
ofstream → Write Output
ifstream → Read Input
Vector → Fast Reading (Dynamic Array)
List → Fast Inserting (Linked List)
Map → Dictionary (Key Value Pair)