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
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Low weight</span>
Definition. <mark>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.</mark>
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
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">High weight</span>
Definition. <mark>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.</mark>
Diagram. <figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u5-01" viewBox="0 0 467 252" width="467" height="252" role="img" aria-label="try block (T), throw (Th), matching catch (C), normal continuation (N), program continues (E)"><style>#dsfig-u5-01 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u5-01 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u5-01 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u5-01 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u5-01 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u5-01 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u5-01 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u5-01 .t{fill:#16181D;font-weight:500}#dsfig-u5-01 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u5-01 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u5-01 .dot{fill:#16181D}#dsfig-u5-01 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u5-01 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u5-01 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u5-01 .ah{fill:#454C5A}#dsfig-u5-01 .ah.hi{fill:#2340B8}#dsfig-u5-01 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u5-01 .wl .t{font-size:12px;font-weight:700}#dsfig-u5-01 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u5-01 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u5-01 .e{stroke:#B1B7C3}html.dark #dsfig-u5-01 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u5-01 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u5-01 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u5-01 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u5-01 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u5-01 .t{fill:#E6E8ED}html.dark #dsfig-u5-01 .t.inv{fill:#0F1115}html.dark #dsfig-u5-01 .kd{stroke:#E6E8ED}html.dark #dsfig-u5-01 .dot{fill:#E6E8ED}html.dark #dsfig-u5-01 .ann{fill:#8FA3FF}html.dark #dsfig-u5-01 .lbl{fill:#858D9C}html.dark #dsfig-u5-01 .ptr{fill:#8FA3FF}html.dark #dsfig-u5-01 .ah{fill:#B1B7C3}html.dark #dsfig-u5-01 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u5-01 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u5-01 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u5-01 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah11" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah" d="M0,1 L9,5 L0,9 z"/></marker><marker id="ahh11" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah hi" d="M0,1 L9,5 L0,9 z"/></marker></defs><path class="e" d="M59,126 L148,126" marker-end="url(#ah11)"/><path class="e" d="M184.8,115.5 L280.5,51.6" marker-end="url(#ah11)"/><path class="e" d="M58,132 L278.1,205.4" marker-end="url(#ah11)"/><path class="e" d="M313.8,50.5 L409.5,114.4" marker-end="url(#ah11)"/><path class="e" d="M313.8,201.5 L409.5,137.6" marker-end="url(#ah11)"/><g class="wl"><rect x="81" y="117" width="47.1" height="18" rx="9"/><text class="t" x="104.5" y="126" dy=".35em" text-anchor="middle">error</text></g><g class="wl"><rect x="210" y="74" width="47.1" height="18" rx="9"/><text class="t" x="233.5" y="83" dy=".35em" text-anchor="middle">match</text></g><g class="wl"><rect x="138.3" y="160" width="61.5" height="18" rx="9"/><text class="t" x="169" y="169" dy=".35em" text-anchor="middle">noerror</text></g><circle class="n" cx="40" cy="126" r="18"/><text class="t" x="40" y="126" dy=".35em" text-anchor="middle">T</text><circle class="n" cx="169" cy="126" r="18"/><text class="t" x="169" y="126" dy=".35em" text-anchor="middle">Th</text><circle class="n" cx="298" cy="40" r="18"/><text class="t" x="298" y="40" dy=".35em" text-anchor="middle">C</text><circle class="n" cx="298" cy="212" r="18"/><text class="t" x="298" y="212" dy=".35em" text-anchor="middle">N</text><circle class="n" cx="427" cy="126" r="18"/><text class="t" x="427" y="126" dy=".35em" text-anchor="middle">E</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">try block (T), throw (Th), matching catch (C), normal continuation (N), program continues (E)</figcaption></figure>
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
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">High weight</span>
Definition. <mark>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.</mark>
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
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Medium weight</span>
Definition. <mark>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.</mark>
Diagram. <figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u5-02" viewBox="0 0 338 252" width="338" height="252" role="img" aria-label="Library classes: Librarian(L), Member(M), Book(B), Loan(Ln); Lb = Library holding all"><style>#dsfig-u5-02 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u5-02 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u5-02 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u5-02 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u5-02 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u5-02 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u5-02 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u5-02 .t{fill:#16181D;font-weight:500}#dsfig-u5-02 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u5-02 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u5-02 .dot{fill:#16181D}#dsfig-u5-02 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u5-02 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u5-02 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u5-02 .ah{fill:#454C5A}#dsfig-u5-02 .ah.hi{fill:#2340B8}#dsfig-u5-02 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u5-02 .wl .t{font-size:12px;font-weight:700}#dsfig-u5-02 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u5-02 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u5-02 .e{stroke:#B1B7C3}html.dark #dsfig-u5-02 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u5-02 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u5-02 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u5-02 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u5-02 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u5-02 .t{fill:#E6E8ED}html.dark #dsfig-u5-02 .t.inv{fill:#0F1115}html.dark #dsfig-u5-02 .kd{stroke:#E6E8ED}html.dark #dsfig-u5-02 .dot{fill:#E6E8ED}html.dark #dsfig-u5-02 .ann{fill:#8FA3FF}html.dark #dsfig-u5-02 .lbl{fill:#858D9C}html.dark #dsfig-u5-02 .ptr{fill:#8FA3FF}html.dark #dsfig-u5-02 .ah{fill:#B1B7C3}html.dark #dsfig-u5-02 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u5-02 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u5-02 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u5-02 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah12" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah" d="M0,1 L9,5 L0,9 z"/></marker><marker id="ahh12" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah hi" d="M0,1 L9,5 L0,9 z"/></marker></defs><path class="e" d="M59,40 L279,40"/><path class="e" d="M51.4,55.2 L157.6,196.8"/><path class="e" d="M298,59 L298,191" marker-end="url(#ah12)"/><path class="e" d="M279,212 L190,212" marker-end="url(#ah12)"/><path class="e" d="M40,193 L40,59"/><g class="wl"><rect x="138.3" y="31" width="61.5" height="18" rx="9"/><text class="t" x="169" y="40" dy=".35em" text-anchor="middle">manages</text></g><g class="wl"><rect x="70.2" y="117" width="68.7" height="18" rx="9"/><text class="t" x="104.5" y="126" dy=".35em" text-anchor="middle">catalogs</text></g><g class="wl"><rect x="267.3" y="117" width="61.5" height="18" rx="9"/><text class="t" x="298" y="126" dy=".35em" text-anchor="middle">borrows</text></g><g class="wl"><rect x="220.3" y="203" width="26.4" height="18" rx="9"/><text class="t" x="233.5" y="212" dy=".35em" text-anchor="middle">of</text></g><circle class="n" cx="40" cy="40" r="18"/><text class="t" x="40" y="40" dy=".35em" text-anchor="middle">L</text><circle class="n" cx="298" cy="40" r="18"/><text class="t" x="298" y="40" dy=".35em" text-anchor="middle">M</text><circle class="n" cx="169" cy="212" r="18"/><text class="t" x="169" y="212" dy=".35em" text-anchor="middle">B</text><circle class="n" cx="298" cy="212" r="18"/><text class="t" x="298" y="212" dy=".35em" text-anchor="middle">Ln</text><circle class="n" cx="40" cy="212" r="18"/><text class="t" x="40" y="212" dy=".35em" text-anchor="middle">Lb</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">Library classes: Librarian(L), Member(M), Book(B), Loan(Ln); Lb = Library holding all</figcaption></figure>
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.