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.
Warm-up questions
Short questions with short answers, each answered in full.
-
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
-
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
-
Fresher
When should a function use named parameters instead of positional ones?
Answered in the lesson Functions: positional, named and default parameters
-
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.
-
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
-
- Why did small-list tests pass while production failed?
- How does removeWhere avoid the problem?
- When would you iterate over a copy instead?
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.
Passes can’t be bought right now, so the model answers can’t be opened at the moment. If you have a pass, enter your licence key.
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
-
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
-
- Why does JavaScript's var give a different result from let?
- When does a closure in Dart still capture a shared, changing variable?
- How would you capture a snapshot of a value that does keep changing?
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
-
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
-
- Why did adding the enum value not cause a compile error?
- When is a default case still the right choice?
- How do you get both a total switch and a safe fallback at run time?
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
-
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
-
- Which case order produces all Cs?
- How would a test on boundary marks have caught this?
- Does the compiler warn about an unreachable case here?
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
-
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
-
- What would a single save over a shared supertype look like?
- When are two distinct names the better answer?
- How do generics help when the bodies really are identical?
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
-
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
-
- Should the new named parameters be required or have defaults?
- Why is making them nullable not the same as making them optional?
- Where does a default value belong, and must it be a constant?
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
-
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
-
- When does a tear-off fail to compile where a lambda works?
- How do optional parameters of the torn-off function affect the two forms?
- Is there any run-time cost difference worth caring about?
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