Dart (3.13) Module 4
Sound null safety interview questions
Interview questions on sound null safety: 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
What does the ! operator do, and why is it called a code smell?
Answered in the lesson Sound null safety: nullable types and operators
-
Fresher
When should you prefer a nullable type over a late variable?
Answered in the lesson Type promotion, late and definite assignment
Questions with model answers
What each question tests, the first step of a good answer and the follow-ups to expect.
-
Mid to senior
A screen crashes with "Null check operator used on a null value" for some users but not others. The code reads user profile fields with ! throughout, such as user.phone! and user.address!.city!. Walk through how you would find the crash and remove the ! operators without changing behaviour for users whose fields are set.
- What it tests
- Knowing that ! throws only when a field is actually null, how to reproduce with a sparse profile, and the safe replacements: ??, promotion, ?., if-case and null-aware elements.
- First step
- Reproduce with a user whose optional fields are null, then read the stack trace to the exact ! that threw. The ones that never fire are on fields that happen to be always set for the users you tested.
- Follow-ups
-
- Why does it crash for only some users?
- How do you decide between ?? , promotion and ?. for each field?
- How would you stop new ! operators from creeping back in?
From the lesson Sound null safety: nullable types and operators
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 teammate used a language whose nullability was only a warning and asks what "sound" buys that is worth the extra ? and ! in the code. Explain what soundness guarantees, one thing it lets the compiler do, and one class of bug it removes, with a concrete example.
- What it tests
- Knowing that soundness means a non-nullable type is never null at run time, that it lets the compiler elide null checks, and that it turns a class of null-dereference crashes into compile errors.
- First step
- Contrast a guarantee with a warning: a warning can be ignored and the null still arrives, while a sound type system proves the null cannot be there. Then name what each side can and cannot rely on.
- Follow-ups
-
- What can the compiler optimise because the guarantee is sound?
- Where does ! poke a hole in the guarantee, and whose responsibility is it then?
- How does data from outside, such as JSON, re-enter the nullable world?
From the lesson Sound null safety: nullable types and operators
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
A class has a public field String? status, and a method checks if (status != null) then calls status.trim(). The compiler reports that status cannot be promoted. Explain why a local variable would promote but this field does not, and give the fixes you would choose between.
- What it tests
- Knowing that fields can change or be overridden by a getter so the checked value may differ from the used value, and the fixes: a local copy, a private final field (promotable since 3.2), or an if-case.
- First step
- Ask what could change status between the check and the call. A local cannot change underneath you; a public field could be reassigned, or a subclass could make it a getter that returns a new value each read.
- Follow-ups
-
- Why does copying the field into a local fix it?
- When does making the field private and final help, and since which version?
- Does marking the field late change anything about promotion?
From the lesson Type promotion, late and definite assignment
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
A Session class declares late User user; and late String token; so the rest of the code can use them without null checks. Logged-out sessions never set them, and the app crashes with LateInitializationError when a guest opens a profile screen. Explain the trade-off the team made with late, and how you would redesign Session.
- What it tests
- Knowing that late trades a compile-time guarantee for a run-time one, that a legitimately-absent value should be nullable, and how to split always-present from maybe-absent fields.
- First step
- For each late field ask whether a logged-out session can exist without it. If yes, late is the wrong tool: the absence is legitimate and belongs in the type as nullable.
- Follow-ups
-
- What exactly did late promise that logged-out sessions broke?
- Which fields stay late or become constructor-initialised, and which become nullable?
- How do readers change when a field becomes nullable?
From the lesson Type promotion, late and definite assignment
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes