UML Diagrams: Class, Use Case, Sequence and Activity Notation with Worked Examples

By the NSCT Exam Editorial Team · Updated 2026-11-24 · Facts checked against official sources on 2026-11-24

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

  1. Name the diagram and what it models: structure (class), goals (use case), messages over time (sequence), workflow (activity) or object life cycle (state machine).
  2. Decode the symbols one by one: line style (solid or dashed), arrowhead (filled, open or hollow triangle) and end marks (diamonds, multiplicities).
  3. Read multiplicities at the far end of an association.
  4. For traces, expand the fragments. Unroll loops, evaluate guards, and follow only the branch whose guard is true.
  5. 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
The six UML class relationships and their notation: association (solid line), aggregation (hollow diamond), composition (filled diamond), generalisation (hollow triangle), realisation (dashed line, hollow triangle) and dependency (dashed line, open arrow).
How each class-diagram relationship is drawn, with the examples used on this page.

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:

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

Common mistakes

Practice set

  1. Programme 1 —— 2 Batch, and Batch 1 —— 25 Student. How many students does one programme have?
  2. School 1 —— 1..4 Block, and Block 1 —— 20..60 Room. What is the maximum number of rooms?
  3. Which symbol marks a protected attribute?
  4. 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?
  5. A fork starts two branches of two ordered actions each. How many single-processor orders are possible?
  6. Which diagram shows which .war file runs on which server?
  7. "Print Receipt" happens only if the user asks. Which relationship links it to "Withdraw Cash", and which way does the arrow point?
  8. Which relationship does class Admin(User) create?
Show answers
  1. 50. 2 × 25.
  2. 240. 4 × 60.
  3. #
  4. 10. 2 outside + 4 × 2 inside.
  5. 6. Choose 2 of the 4 slots for branch 1: C(4, 2).
  6. Deployment diagram.
  7. «extend», with the arrow pointing from Print Receipt to Withdraw Cash.
  8. 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

Interactive quiz loading. The first 10 questions are shown below.

  1. In a UML class diagram, what visibility does the '#' symbol before an attribute indicate?

    1. Private
    2. Protected
    3. Package
    4. Public
    Show answer

    Answer: B. Protected

    UML visibility markers are + public, − private, # protected and ~ package. A protected feature is visible to the class and its subclasses.

  2. A class diagram shows a dashed line with a hollow triangular arrowhead from class Wallet to «interface» Payable. What does it mean?

    1. Wallet inherits from the class Payable
    2. Wallet realises (implements) Payable
    3. Wallet depends on Payable temporarily
    4. Wallet is composed of Payable objects
    Show answer

    Answer: B. Wallet realises (implements) Payable

    A dashed line with a hollow triangle is interface realisation: Wallet provides the operations Payable specifies. Generalisation uses a solid line with a hollow triangle, and dependency uses a dashed line with an open arrowhead.

  3. In a UML 2.5 sequence diagram, what does a solid line with an open (stick) arrowhead represent?

    1. A synchronous call
    2. A reply message
    3. An asynchronous message
    4. A creation message
    Show answer

    Answer: C. An asynchronous message

    UML draws synchronous messages with a filled arrowhead and asynchronous messages with an open arrowhead, both on solid lines. Replies use a dashed line, and a creation message is a dashed line with an open arrowhead pointing at the new lifeline's head.

  4. In an activity diagram, how does a flow final node (a circle with an X) differ from an activity final node (a bull's-eye)?

    1. A flow final node ends the whole activity; an activity final node ends one flow
    2. Both end the whole activity; they differ only in appearance
    3. A flow final node starts a new parallel flow
    4. A flow final node ends only the flow reaching it; an activity final node ends the whole activity
    Show answer

    Answer: D. A flow final node ends only the flow reaching it; an activity final node ends the whole activity

    When a token reaches an activity final node, the entire activity stops, including other concurrent flows. A flow final node simply consumes the token that reaches it, and other flows carry on.

  5. In a class diagram, an attribute is shown underlined: totalStudents : Integer. What does the underline mean?

    1. It is abstract
    2. It is derived from other attributes
    3. It is static (class-scope), shared by all instances
    4. It is private
    Show answer

    Answer: C. It is static (class-scope), shared by all instances

    UML underlines class-scope (static) features. An abstract class or operation is shown in italics (or with {abstract}), a derived attribute has a leading '/', and private visibility is '−'.

  6. Which UML diagram shows physical nodes such as servers and the artifacts (for example, .jar files) deployed on them?

    1. Component diagram
    2. Package diagram
    3. Activity diagram
    4. Deployment diagram
    Show answer

    Answer: D. Deployment diagram

    A deployment diagram models the run-time hardware or execution environments (nodes) and the artifacts deployed to them. A component diagram shows logical components and their interfaces, without the hardware.

  7. A class diagram shows Hospital 1 —— 2..6 Ward and Ward 1 —— 10..30 Bed. What is the maximum number of beds one hospital can have?

    1. 36
    2. 60
    3. 30
    4. 180
    Show answer

    Answer: D. 180

    Multiplicities are read at the far end: a hospital has 2 to 6 wards, and each ward has 10 to 30 beds. The maximum is 6 × 30 = 180 (and the minimum is 2 × 10 = 20).

  8. How should the relationship between Invoice and InvoiceLine in this code be drawn in a UML class diagram?

    class InvoiceLine:
        def __init__(self, item, qty):
            self.item, self.qty = item, qty
    
    class Invoice:
        def __init__(self, rows):
            # lines are created here and never shared
            self.lines = [InvoiceLine(i, q) for i, q in rows]
    1. Aggregation: hollow diamond at the Invoice end
    2. Composition: filled diamond at the Invoice end
    3. Generalisation: hollow triangle pointing to Invoice
    4. Dependency: dashed arrow from Invoice to InvoiceLine
    Show answer

    Answer: B. Composition: filled diamond at the Invoice end

    Invoice creates its lines itself and never shares them, so the lines exist only as parts of that invoice: composition, drawn with a filled diamond at the whole (Invoice) end. Aggregation would suit parts created elsewhere that can outlive the whole.

  9. Which UML relationship best represents how Report uses Printer in this code?

    class Printer:
        def send(self, text):
            print(text)
    
    class Report:
        def __init__(self, title):
            self.title = title
        def print_to(self, printer):     # printer is only a parameter
            printer.send(self.title)
    1. Association (solid line)
    2. Composition (filled diamond at Report)
    3. Dependency (dashed arrow from Report to Printer)
    4. Realisation (dashed line with hollow triangle)
    Show answer

    Answer: C. Dependency (dashed arrow from Report to Printer)

    Report keeps no reference to a Printer; it only receives one as a method parameter and uses it for the duration of the call. That temporary 'uses' link is a dependency, drawn as a dashed line with an open arrowhead towards Printer.

  10. 'Checkout' always performs 'Validate Payment'. 'Apply Coupon' runs during checkout only when the customer has a coupon. Which pair of use case relationships is drawn correctly?

    1. «include» arrow from Validate Payment to Checkout; «extend» arrow from Checkout to Apply Coupon
    2. «extend» arrow from Checkout to Validate Payment; «include» arrow from Apply Coupon to Checkout
    3. «include» arrow from Checkout to Validate Payment; «extend» arrow from Apply Coupon to Checkout
    4. Generalisation from Validate Payment to Checkout; generalisation from Apply Coupon to Checkout
    Show answer

    Answer: C. «include» arrow from Checkout to Validate Payment; «extend» arrow from Apply Coupon to Checkout

    An «include» arrow points from the base use case to the use case it always includes. An «extend» arrow points from the optional, conditional use case to the base use case it extends. So Checkout includes Validate Payment, and Apply Coupon extends Checkout.

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.