UML Diagrams: Class, Use Case, Sequence and Activity Notation with Worked Examples
UML (Unified Modeling Language) diagram questions test three skills: reading the notation (what a filled diamond or a dashed arrow means), choosing the right diagram for a question, and tracing a diagram (counting objects, messages or paths). UML 2.5 defines 14 diagram types, but class, use case, sequence, activity and state machine diagrams cover most exam questions. Software Engineering carries 10% of the NSCT according to HEC's Areas of Competencies, with a 2026 accuracy of 50.1%, per the HEC NSCT report and APP.
The short overview (the diagram families, and aggregation versus composition) is on Software Design, and use cases as requirements are covered in Software Requirements Engineering. This page goes deeper into notation and tracing. Every example uses one system, a university course-registration portal, and every count was checked by a small Python simulator.
The method, step by step
- Name the diagram and what it models: structure (class), goals (use case), messages over time (sequence), workflow (activity) or object life cycle (state machine).
- Decode the symbols one by one: line style (solid or dashed), arrowhead (filled, open or hollow triangle) and end marks (diamonds, multiplicities).
- Read multiplicities at the far end of an association.
- For traces, expand the fragments. Unroll loops, evaluate guards, and follow only the branch whose guard is true.
- Count only what is asked: calls or all messages, paths or actions.
Which diagram answers which question?
| Question | Diagram |
|---|---|
| What classes exist and how are they related? | Class |
| What can each user (actor) achieve with the system? | Use case |
| In what order do objects exchange messages in one scenario? | Sequence |
| Which objects are linked, with messages numbered on the links? | Communication |
| What is the workflow, including decisions and parallel steps? | Activity |
| How does one object change state in response to events? | State machine |
| Which software artifacts run on which hardware nodes? | Deployment |
UML 2.5's 14 types split into 7 structural diagrams (class, object, component, composite structure, deployment, package, profile) and 7 behavioural diagrams (use case, activity, state machine, plus the interaction diagrams: sequence, communication, timing, interaction overview).
Class diagrams
Reading a class box:
| Line in the box | Meaning |
|---|---|
| − rollNo : String | private attribute of type String |
| + register(c : Course) : Boolean | public operation, one parameter, returns Boolean |
| # validate() : Boolean | protected operation |
| ~ advisorId : Integer | package-visible attribute |
| totalStudents : Integer | static (class-scope) attribute, shown underlined |
| /age : Integer | derived attribute (computed from other data) |
An abstract class or operation is written in italics or marked {abstract}.
Relationship notation:
| Relationship | Line | End mark | Example |
|---|---|---|---|
| Association | solid | optional open arrowhead for navigability | Student — Course |
| Aggregation | solid | hollow diamond at the whole | Department ◇— Lecturer |
| Composition | solid | filled diamond at the whole | Transcript ◆— Grade |
| Generalisation | solid | hollow triangle at the parent | GraduateStudent —▷ Student |
| Realisation | dashed | hollow triangle at the interface | FeeAccount ‐ ‐▷ «interface» Payable |
| Dependency | dashed | open arrowhead at the supplier | Report ‐ ‐> Printer |
Composition rule: a part belongs to at most one composite at a time, so the multiplicity at the composite end must be 1 or 0..1.
Worked example: multiplicity. Course 1 —— 1..3 Section, and Section 1 —— 15..40 Student. One course has 1 to 3 sections, and each section has 15 to 40 students. A course therefore enrols at least 1 × 15 = 15 and at most 3 × 40 = 120 students.
Worked example: code to diagram.
| Code pattern | Relationship |
|---|---|
| A class creates its parts in its constructor and never shares them | Composition |
| A class stores references to objects created elsewhere that outlive it | Aggregation |
| A method only receives another class as a parameter | Dependency |
class GraduateStudent(Student) |
Generalisation |
| A class implements every operation of an interface | Realisation |

Use case diagrams
Actors (stick figures) sit outside the subject box (the system boundary), and solid lines link actors to use cases (ovals).
| Relationship | Arrow | Direction | Use when |
|---|---|---|---|
| «include» | dashed, open arrowhead | from the base use case to the included one | the base always needs that behaviour |
| «extend» | dashed, open arrowhead | from the extending use case to the base | the behaviour is optional or conditional |
| Generalisation | solid, hollow triangle | from the specialised element to the general one | a graduate student is a kind of student |
Worked example. "Register for Course" always checks the login, so Register for Course «include» Authenticate. A late fee applies only after the deadline, so Apply Late Fee «extend» Register for Course (the extension condition is "after deadline"). GraduateStudent inherits all of Student's use cases through actor generalisation.
Sequence diagrams
| Element | Notation |
|---|---|
| Lifeline | box with a dashed vertical line |
| Synchronous call | solid line, filled arrowhead |
| Asynchronous message | solid line, open arrowhead |
| Reply | dashed line back to the caller |
| Create message | dashed line, open arrowhead, pointing at the new lifeline's head |
| Destruction | X at the bottom of the lifeline |
| Combined fragments | boxes labelled alt, opt, loop, par, break and others, with [guards] |
Worked example: tracing a registration.
Student -> UI : register(c)
UI -> Ctrl : register(s, c)
loop [for each prerequisite]
Ctrl -> Transcript : passed(p)
Transcript --> Ctrl : bool
Ctrl -> Course : hasSeat()
Course --> Ctrl : bool
alt [seat available]
Ctrl --> Enrolment : «create»
else
Ctrl -> Waitlist : add(s)
Ctrl --> UI : status
UI --> Student : message
| Case | Messages | Synchronous calls |
|---|---|---|
| 2 prerequisites, seat available | 2 + 4 + 2 + 1 + 2 = 11 | 5 |
| 0 prerequisites, no seat | 2 + 0 + 2 + 1 + 2 = 7 | 4 |
Only one operand of an alt fragment runs, the one whose guard is true. An opt fragment is an alt with a single operand.
Activity diagrams
| Node | Symbol | Meaning |
|---|---|---|
| Initial | filled circle | where the flow starts |
| Activity final | bull's-eye | ends the whole activity |
| Flow final | circle with X | ends only that flow |
| Decision / merge | diamond | branch on [guards] / bring branches back together |
| Fork / join | thick bar | split into parallel flows / wait for all of them |
| Partition | swimlane | who performs each action |
Worked example: counting paths. After "Check eligibility", the flow either rejects the application, or goes to "Pay fee". Payment either succeeds or fails once, followed by a retry, and then the student is enrolled. The paths are (1) reject, (2) pay and succeed, then enrol, and (3) pay, fail, retry, then enrol: 3 paths.
Worked example: fork and join. A fork starts branch 1 (Send email, then Send SMS) and branch 2 (Update roster). Each branch keeps its own order, but they can interleave. Update roster can run before, between or after the two messages, giving 3 possible orders on one processor. The join waits for both branches.
State machine diagrams
Transitions are labelled trigger [guard] / effect. An enrolment starts as Requested:
- approve [seats > 0] / seats−− → Enrolled
- approve [seats = 0] → Waitlisted
- Waitlisted: seatFreed → Enrolled
- Enrolled: drop → Dropped
Trace. With seats = 0, the events "approve, drop" leave it Waitlisted, because Waitlisted has no drop transition, so that event is ignored. The events "approve, seatFreed, drop" end in Dropped.
Speed tricks
- Dashed line = weaker link (dependency, realisation, include, extend, reply). Solid line = structural link or call.
- The hollow triangle always points to the more general element: a parent class or an interface.
- The diamond sits at the whole, never at the part.
- «include» points away from the base; «extend» points towards the base.
- For traces, write the expanded message list. Do not count in your head.
Common mistakes
- Putting the composition diamond at the part's end.
- Reversing «include» and «extend» arrows.
- Using a sequence diagram when the question asks for an object's life cycle (that needs a state machine).
- Counting both operands of an alt fragment.
- Treating any arrow back to the caller as a reply. A reply must be dashed.
- Confusing the flow final node with the activity final node.
Practice set
- Programme 1 —— 2 Batch, and Batch 1 —— 25 Student. How many students does one programme have?
- School 1 —— 1..4 Block, and Block 1 —— 20..60 Room. What is the maximum number of rooms?
- Which symbol marks a protected attribute?
- A loop runs 4 times. Inside it are one call and one reply, and outside it are one call and one reply. How many messages are there?
- A fork starts two branches of two ordered actions each. How many single-processor orders are possible?
- Which diagram shows which .war file runs on which server?
- "Print Receipt" happens only if the user asks. Which relationship links it to "Withdraw Cash", and which way does the arrow point?
- Which relationship does
class Admin(User)create?
Show answers
- 50. 2 × 25.
- 240. 4 × 60.
- #
- 10. 2 outside + 4 × 2 inside.
- 6. Choose 2 of the 4 slots for branch 1: C(4, 2).
- Deployment diagram.
- «extend», with the arrow pointing from Print Receipt to Withdraw Cash.
- Generalisation, with a hollow triangle pointing at User.
Practise
The quiz below has 20 UML MCQs on notation, diagram choice, code-to-diagram mapping and traces, filterable by tier and difficulty. The traces were checked with a Python simulator, and the code snippets were run.
Practise: 20 questions
FAQs
How many UML diagrams are there?
UML 2.5 defines 14: seven structural (class, object, component, composite structure, deployment, package, profile) and seven behavioural (use case, activity, state machine, sequence, communication, timing, interaction overview). Exams focus on class, use case, sequence, activity and state machine diagrams.
What is the difference between include and extend?
«include» means the base use case always runs the included behaviour, and the arrow points from the base to the included use case. «extend» adds optional or conditional behaviour, and its arrow points from the extending use case to the base.
When should I use a sequence diagram instead of an activity diagram?
Use a sequence diagram when the question is about which objects send which messages, in what order, in one scenario. Use an activity diagram for a workflow of actions with decisions and parallel branches, especially across roles.
How do I tell aggregation from composition in code?
Ask who creates the parts and whether they outlive the whole. Parts created inside the whole and never shared mean composition. Parts passed in from outside that survive the whole mean aggregation.
Next step
Revise design basics in Software Design, then compare process models in SDLC Models Compared. Return to the Software Engineering MCQ hub, or time yourself in the NSCT mock test.