How unit 5 is examined
This unit covers C++ strings, exception handling, multithreading with STL collections, and the library/ATM case study; exception handling, multithreading with collections, and the library diagram carry the marks.
Strings
Low weight
Definition. A string is a sequence of characters; in C it is a char array ended by the null character '\0', and in C++ the std::string class (header <string>) manages such a sequence with member functions.
Key points.
- A C-style string such as
char s[] = "RGPV";occupies one extra byte for '\0', so it needs 5 bytes. std::stringgrows automatically, so the programmer does not manage size or memory.length()orsize()returns the number of characters, andappend()or+joins two strings.substr(pos, len)extracts a part,find(str)returns the first index orstring::npos, andcompare()returns 0 if equal, negative or positive otherwise.- C functions from
<cstring>arestrlen,strcpy,strcatandstrcmp.
Example.
string s = "Object"; s.append(" Oriented");
cout << s.length(); // 15
cout << s.substr(0, 6); // Object
cout << s.find("Ori"); // 7
cout << s.compare("Object"); // > 0
Asked: [7 marks] (Nov 2022) What is String? Explain functions of strings with suitable examples.
Exceptional handling
High weight
Definition. An exception is an abnormal runtime condition, such as division by zero or a failed allocation, that disrupts normal program flow; exception handling is the C++ mechanism of try, throw and catch that detects and handles it gracefully.
Diagram.
Key points.
- The
tryblock encloses the code that may cause an error and is monitored for exceptions. - The
throwstatement raises an exception by giving a value (int, string or object) and transfers control out of the try block. - The
catchblock, placed immediately after try, receives the thrown value; the block whose parameter type matches is executed. - If no exception occurs, all catch blocks are skipped; if none matches,
terminate()is called and the program aborts. catch(...)is a catch-all handler and should be written last.- Several catch blocks may follow one try block, one per exception type.
- Standard exceptions in
<exception>includebad_alloc,out_of_range,invalid_argumentandbad_cast, all withwhat(). - Compared with error codes, exceptions separate error handling from normal logic and cannot be silently ignored.
Example. Array index out of bounds (index outside 0..n-1) and null pointer dereference are runtime errors; C++ arrays do not check, so vector::at() throws out_of_range, while a null pointer is tested and thrown by the programmer.
Program (Jun 2023).
int main() {
int x, y, z; cin >> x >> y >> z;
try {
if (x - y == 0) throw "Division by zero";
cout << "R = " << (float)z / (x - y);
}
catch (const char* m) { cout << "Error: " << m; }
}
// input 5 5 9 -> Error: Division by zero ; input 2 5 9 -> R = -3
Answer frame. Open with the definition of exception; draw the try-throw-catch flow; explain try, throw, catch in order, then matching, catch(...), standard exceptions and advantage over error codes; give the divide-by-zero program; close with one line on graceful termination. For the Jun 2023 program, define exception in two lines, then give the code and the two outputs.
Pitfall: Writing throw outside a try block, or a catch of the wrong type, aborts the program instead of handling the error.
Asked: [7 marks] (Nov 2019) Explain the concept of exception handling in C++ with an example. Asked: [7 marks] (May 2019) How exception are handled in C++ programming? Explain. Asked: [7 marks] (Dec 2020) What is meant by exceptions? How an exception is handled? Asked: [7 marks] (Jun 2020) What are exceptions and how are they handled? What are Array Index Out of Bounds and Null Pointer exceptions? Uses of try, catch and throw with example. Asked: [7 marks] (Dec 2023) What do you mean by exception? How is exception handled in C++? Explain with an example. Asked: [7 marks] (Jun 2023) What is exception? Write a program to read x, y, z and evaluate R = z / (x - y), throwing an exception on division by zero. Asked: [7 marks] (Nov 2022, Dec 2025) Explain exception handling and multithreading. Asked: [14 marks] (Nov 2018) Explain Exception Handling and multi-threading.
Multi-threading and Data collections
High weight
Definition. Multithreading is the concurrent execution of two or more threads, the smallest units of execution, within a single process; a collection is an object that stores and manages a group of elements, and a collection class is the class providing it, such as the STL vector, list, set and map.
Thread lifecycle. New, Runnable, Running, Waiting or Blocked, Terminated.
Key points.
- A thread shares the code, data and heap of its process but has its own stack and registers, so it is lighter than a process.
- Multithreading gives better CPU utilization because while one thread waits for input or output another runs.
- It keeps applications responsive, since a GUI thread stays alive while a worker thread does a long job.
- Threads share resources and memory, so communication is cheaper than between processes, and throughput rises on multicore machines.
- In OOP it is used in GUI applications, web servers handling many clients, games, real-time simulation and concurrent data processing.
- Shared data needs synchronization, using
std::mutex, to avoid race conditions. - Types of collections are sequence (vector, list, deque), associative (set, map), unordered (unordered_set, unordered_map) and adaptors (stack, queue, priority_queue).
- A collection class hides storage and offers insert, erase, search and iterate operations for any type through templates.
Example.
void hello(int n) { cout << "worker " << n << "\n"; }
int main() {
thread t(hello, 1); // start
t.join(); // wait for it
cout << "done"; // worker 1, then done
}
Comparison (Dec 2024).
| Collection | Access | Insert/Delete | Use case |
|---|---|---|---|
| vector | O(1) index | O(1) at end, O(n) middle | Default dynamic array |
| list | O(n) | O(1) at known position | Frequent insertion, deletion |
| map | O(log n) | O(log n) | Sorted key-value data |
| unordered_map | O(1) average | O(1) average | Fast lookup by key |
| set | O(log n) | O(log n) | Unique sorted elements |
Answer frame. Open with the definition of multithreading; draw the lifecycle; develop points 1-6, then the collection types and the collection class; for Dec 2024 give the table then a use case per container; close with one line that multithreading gives speed and collections give organised data.
Asked: [7 marks] (Dec 2023, Dec 2024) What is Multithreading? Discuss its importance and application in OOP. Describe multi-threading and its advantages. Asked: [7 marks] (Jun 2020, Jun 2023) Explain multithreading in OOP, its benefits. Discuss the collection and its type. What is a collection class? Asked: [7 marks] (Dec 2024) Discuss various data collections available in C++. How do they differ in performance and use cases? Asked: [7 marks] (Dec 2025) Discuss data collections with a suitable case study (ATM/Library). Asked: [14 marks] (Nov 2018) Short notes (any two): Data collections, Destruction, Overloading. Asked: [14 marks] (Dec 2020) Short notes (any two): Multi-threading, Association, Access modifier. Asked: [14 marks] (Nov 2022) Short notes (any two): Virtual function, File Handling, Data collection.
Case study: ATM, Library management system
Medium weight
Definition. A case study applies object-oriented methodology to a real system: identify objects and classes from the problem, give them attributes and operations, and join them by associations.
Diagram.
Key points.
- Methodology steps are analysis (find nouns as classes: Book, Member, Librarian, Loan), design (attributes and methods), then implementation.
- Book has ISBN, title, author and status, with methods
issue()andreturnBook(). - Member has id, name and borrowed books, with
borrow()andreturn(). - Librarian adds books, registers members and issues them; Loan links Member to Book with issue date, due date and fine.
- Associations: a Member borrows 0..* Books through Loans; a Library aggregates many Books and Members.
- Issue works as: search catalog, check the member, check availability, create a Loan and mark the book unavailable; return computes the fine.
- Strings hold titles, names and ISBN; exceptions handle book unavailable, invalid member ID and overdue limit; a
map<string, Book>or vector holds the catalog and users. - An ATM follows the same pattern: Account, Card, Transaction and Bank classes, with an exception for insufficient balance.
Answer frame. Open with the entities; draw the class diagram with attributes, methods and multiplicities; explain issue and return workflow; for the architecture question add the UI, business logic and storage layers and the use of strings, exceptions and collections; close with one line on reuse and maintainability.
Asked: [7 marks] (Jun 2023, Dec 2024) Explain library management system using object-oriented diagram; architecture and functionalities, and use of strings, exception handling and collections. Asked: [7 marks] (Dec 2020) Explain library management system using object oriented diagram. Asked: [7 marks] (Jun 2020) Explain the object oriented methodology of designing a library management system; the object and class diagrams. Asked: [14 marks] (May 2019) Explain briefly: Containers, Destruction, ATM.
Last-minute revision
- Exception = abnormal runtime condition; handled with try, throw, catch.
- Unmatched exception calls
terminate();catch(...)catches all. - Standard exceptions: bad_alloc, out_of_range, invalid_argument, bad_cast.
- Thread lifecycle: New, Runnable, Running, Blocked, Terminated.
- Threads share heap and data, but each has its own stack.
- Use
thread t(f); t.join();andstd::mutexfor shared data. - vector access O(1); list insertion O(1); map O(log n); unordered_map O(1) average.
- C string ends with '\0'; std::string has length, append, substr, find, compare.
- Destructor releases resources when an object is destroyed.
- Library classes: Book, Member, Librarian, Loan; ATM: Account, Card, Transaction.
Memory hooks
- TTC: Try, Throw, Catch.
- Thread = light worker inside a process; process = the whole factory.
- VLMU: vector, list, map, unordered_map.
- Library: Books are lent to Members by Librarian through a Loan.
Coverage checklist
- Strings: Nov 2022 string functions.
- Exceptional handling: May 2019, Nov 2019, Dec 2020, Jun 2020, Jun 2023, Dec 2023, Nov 2022 and Dec 2025, Nov 2018.
- Introduction of Multi-threading and Data collections: Nov 2018, Dec 2020, Nov 2022, Jun 2020, Jun 2023, Dec 2023, Dec 2024, Dec 2025.
- Case study like: ATM, Library management system: Dec 2020, Jun 2020, Jun 2023, Dec 2024, May 2019.