Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

Dart (3.13) Module 3

Control flow and functions interview questions

Interview questions on control flow and functions: short warm-ups, then what each harder question tests, the first step of a good answer and its follow-ups.

12 questions: 4 warm-ups and 8 with model answers · Levels: Fresher to senior

Warm-up questions

Short questions with short answers, each answered in full.

  1. Fresher

    Which Dart loop always runs its body at least once, and when would you use it?

    Answered in the lesson if, else and loops: for, while and do-while

  2. Fresher

    In Dart 3, do you still need break at the end of a switch case?

    Answered in the lesson Dart switch statements and expressions

  3. Fresher

    When should a function use named parameters instead of positional ones?

    Answered in the lesson Functions: positional, named and default parameters

  4. Fresher

    What does a Dart closure capture, the value of a variable or the variable itself?

    Answered in the lesson Closures, function types and tear-offs

Questions with model answers

What each question tests, the first step of a good answer and the follow-ups to expect.

  1. Fresher to mid

    A cleanup job walks a list of pending tasks and removes each finished one inside a for-in loop. In tests on small lists it works; in production it sometimes throws "Concurrent modification during iteration" and leaves tasks behind. Explain why it passed tests yet fails in production, and how you would remove the finished tasks safely.

    What it tests
    Knowing that changing a list inside its own for-in throws, why the timing looks intermittent, and the safe alternatives: removeWhere, iterating a copy, or building a new list.
    First step
    Reproduce it with a list where the first finished task is not the last element, then read what for-in does when the list it is reading changes length under it.
    Follow-ups

    From the lesson if, else and loops: for, while and do-while

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.

    Opening the model answers with your pass…

    Model answers open with any MySmartCoPilot pass.

    • Pro pass · 1 week₹79$2.99
    • 30 days₹179$5.99
    • 1 year₹1,699$45

    One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

    It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  2. Mid to senior

    A developer from JavaScript builds a list of button handlers in a for loop, each meant to remember its own index. Their old JavaScript printed the final count from every handler; the Dart version prints a different index from each. Explain the difference, and where capture can still bite in Dart.

    What it tests
    Knowing that Dart binds a fresh loop variable each turn while JavaScript's var shared one, what let and const changed in JavaScript, and the Dart cases where a single shared variable is still captured.
    First step
    Build the list of closures in both languages and call them. In Dart each turn's loop variable is a new binding; name what the closures capture and why the captured values differ.
    Follow-ups

    From the lesson if, else and loops: for, while and do-while

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  3. Mid to senior

    An order pipeline maps an OrderState enum to a screen with a switch that ends in a default returning an error page. A new state, refundPending, is added for a feature, the code compiles, and refunds silently show the error page in production. Explain why the compiler did not help, and how you would structure the switch so it would.

    What it tests
    Knowing that a default or wildcard case makes a switch exhaustive and silences the enum check, and that naming every case turns a new enum value into a compile error.
    First step
    Ask what the default case did for exhaustiveness: it covered every value, including ones that do not exist yet, so adding an enum value could not make the switch incomplete. Then remove it and see what the compiler says.
    Follow-ups

    From the lesson Dart switch statements and expressions

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  4. Fresher to mid

    A grading function written as a switch expression with relational patterns gives every student a C, even those who scored 95. The bands are all present and the code compiles. What is the likely cause, and how do you find and fix it?

    What it tests
    Knowing that a switch expression tries cases top to bottom and takes the first match, so a broad band placed above a narrower one shadows it.
    First step
    Write out the order of the cases as the code has them, then trace a mark of 95 from the top: the first pattern it satisfies decides the grade, whatever comes after.
    Follow-ups

    From the lesson Dart switch statements and expressions

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  5. Mid to senior

    A teammate coming from Java asks why they cannot write two versions of save, one taking a String path and one taking a File, and keeps hitting "already declared in this scope". Explain why Dart has no overloading, and walk through the options you would offer for this specific case.

    What it tests
    Knowing that a Dart name is one declaration, and the replacements: a shared supertype, optional or named parameters, generics, distinct names, or an extension, chosen by why two versions were wanted.
    First step
    Separate the two reasons someone wants overloads: same behaviour over related types, or different behaviour per type. Dart handles each differently, so decide which save is.
    Follow-ups

    From the lesson Functions: positional, named and default parameters

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  6. Fresher to mid

    A review flags the call cropImage(photo, true, false) as unreadable. The function is cropImage(Image img, bool keepAspect, bool fromCenter). Explain the problem, rewrite the signature, and say what nullable versus optional has to do with the fix.

    What it tests
    Knowing the boolean-trap readability problem, how required named parameters fix it, and that making a parameter optional or nullable is a separate decision from naming it.
    First step
    Read the call without looking at the declaration and ask what true and false mean. Then convert the two booleans to named parameters and decide for each whether it should be required or have a default.
    Follow-ups

    From the lesson Functions: positional, named and default parameters

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  7. Mid to senior

    A developer builds a list of callbacks in a loop and, inside each, reads a counter they declared once above the loop and increment as they go. Every callback ends up reporting the same final count, which is not what they wanted. Explain what each callback captured, and how to give each its own value.

    What it tests
    Knowing that closures capture variables not values, that one variable declared outside the loop is shared by all the callbacks, and that a fresh local per turn (or a captured parameter) fixes it.
    First step
    Count the variables. There is one counter declared above the loop, so every callback captured that single variable and reads its latest value. Then ask how to introduce a separate variable per callback.
    Follow-ups

    From the lesson Closures, function types and tear-offs

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  8. Fresher to mid

    In a review you see both items.map((s) => int.parse(s)) and items.map(int.parse). A colleague says they are identical and the choice is pure style. Are they always interchangeable? Explain when a tear-off and a wrapping lambda differ, with examples.

    What it tests
    Knowing that a tear-off is the function itself while a lambda is a new function, how optional or named parameters and type inference can make them behave differently, and when a lambda is actually needed.
    First step
    Write both forms and ask what each passes: the existing function, or a new closure that calls it. Then look for a case where the wrapping closure changes which arguments reach the inner function.
    Follow-ups

    From the lesson Closures, function types and tear-offs

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes

All Dart (3.13) interview questions · The Dart (3.13) track

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.