Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close
    Lesson16 / 20

    Module 4 · Reliable programs: copy, handle errors and check results · Lesson 16 of 20

    Check behaviour: catch a boundary bug before it reaches a learner

    Test the exact number where a rule changes

    The planner gives you a break once you finish at least three tasks, including the third. Code can run without errors and still get that rule wrong. A boundary is the point where the result changes. Here the boundary is three, so two, three, and four are useful numbers to check.

    A test chooses an input, states the expected answer from the rule, and compares that answer with what the program actually returns. For the input three, we expect true because the rule includes the third task.

    Read the code and make your prediction first. Choose Try it, then Run, to check what the example prints.

    C#Runs
    using System;
    
    bool expected = true;
    bool actual = EarnsBreak(3);
    Console.WriteLine(actual == expected);
    
    static bool EarnsBreak(int completed) => completed >= 3;

    The print line compares the actual result with the expected result. It reports whether the check passed.

    Change the call to EarnsBreak(2) and change expected to false. What does Console.WriteLine(actual == expected) print?

    A False because two tasks do not earn a break
    B True because the actual and expected answers are both false
    C 2 because completed is 2
    D Nothing because false cannot be compared
    Show answer

    Correct answer: B

    EarnsBreak returns false below the boundary. The test expects false, so the comparison is true and the check passes.

    Microsoft Learn: what makes a test readable

    The code builds, but exactly 25 minutes fails a 25-minute goal. What check would catch that mistake next time? Turn the boundary rule into repeatable checks.

    Predict a bug

    The requirement is: 25 minutes or more meets a 25-minute goal. Before running, predict this program. Then decide whether the observed result satisfies the requirement.

    C#Runs
    using System;
    
    Console.WriteLine(IsGoalMet(25, 25));
    
    static bool IsGoalMet(int minutes, int goal)
    {
        return minutes > goal;
    }

    The program prints False. Which explanation identifies the defect?

    A The program should compare with >= because exactly 25 also meets the goal
    B Console.WriteLine cannot print a bool
    C Every comparison needs == true
    D The method needs a loop
    Show answer

    Correct answer: A

    The requirement includes equality. The current > comparison excludes the exact boundary.

    Option B: WriteLine can print a bool.

    Option C: A bool expression is already a usable result.

    Option D: One comparison is sufficient; a loop would not fix it.

    Hint: Read “or more” in the requirement.

    Choose checks from the requirement

    Text
    Requirement: minutes >= goal
    
    24 minutes ──> below ──> ?
    25 minutes ──> equal ──> ?
    26 minutes ──> above ──> ?

    A check needs an input, an expected result and a comparison with the actual result. “The program ran” is not the same as “the result meets the requirement.” The three neighbouring values make the boundary visible.

    Run repeatable checks

    The small Check method below prints a pass or throws on failure. It uses no extra packages. This is a first step toward automated tests; a formal test framework gives you richer discovery and reporting later.

    C#Runs
    using System;
    
    Check("below the goal", IsGoalMet(24, 25) == false);
    Check("exactly the goal", IsGoalMet(25, 25) == true);
    Check("above the goal", IsGoalMet(26, 25) == true);
    Check("zero goal", IsGoalMet(0, 0) == true);
    
    static bool IsGoalMet(int minutes, int goal)
    {
        return minutes >= goal;
    }
    
    static void Check(string name, bool passed)
    {
        if (!passed)
        {
            throw new InvalidOperationException("FAIL: " + name);
        }
        Console.WriteLine("PASS: " + name);
    }

    Change only >= to > inside IsGoalMet. Predict which check catches the change, run it, and then restore >=.

    Which set is most useful for checking this boundary?

    A 25, 25, 25
    B 1, 2, 3 only
    C 24, 25, 26 with expected results
    D Only one very large number
    Show answer

    Correct answer: C

    Below, equal and above exercise both sides and the exact transition.

    Option A: Repeating one value does not cover both sides.

    Option B: All three inputs are below 25 and miss the equality bug.

    Option D: A large number cannot tell you whether equality is handled.

    Your turn: test a different rule

    Write CanStartSession(int minutes) for the rule “a session must be at least 5 minutes and at most 90 minutes.” Before coding, write the expected results for 4, 5, 90 and 91. Implement the method and add four Check calls in your own project.

    Stop here and run your attempt. Each check should name the input and compare the returned bool with the rule.

    Hint

    Both comparisons must hold: at least 5, and at most 90.

    Compare your result

    Compare each result with the lower and upper bounds. If a check disagrees with your prediction, trace both comparisons separately.

    A check fails after you change a method. What is the best first step?

    A Delete the check so the program is green
    B Compare the input, requirement, expected value and actual result
    C Catch every exception and print success
    D Add random changes until the error disappears
    Show answer

    Correct answer: B

    A failure tells you a promised behaviour may have changed. First establish whether the expectation or implementation is wrong.

    Option A: Deleting the evidence does not fix the behaviour.

    Option C: Swallowing the failure hides a defect.

    Option D: Unexplained changes make the cause harder to find.

    Explain it back

    Explain why a passing compile is weaker evidence than these four checks. Predict CanStartSession(-1) and name the comparison that determines the result. Then decide whether the input screen should explain the rule for negative durations.

    Reference: Microsoft — testing in .NET.

    Quick reference

    • Read the requirement before choosing test inputs.
    • Check below, equal to and above a boundary.
    • Compare actual and expected behaviour.
    • A failing check is information to investigate.
    • Keep validation policy explicit.

    Practice

    Practice test: C# Beginner Practice: Test a Study Goal Boundary5 questions · 1 mark each, no negative marking
    Take the test
    Sign in to mark lessons done and keep your place in the course.Sign in

    TechInterviewNotes Editorial Team · Updated 24 Nov 2026 ·