Dart (3.13) Module 2
Values and types: variables, numbers, strings and operators interview questions
Interview questions on “Values and types: variables, numbers, strings and operators”.
Warm-up questions
Short questions with short answers, each answered in full.
-
Fresher
When does a Dart value need const rather than final?
Answered in the lesson Variables: var, final, const and type inference
-
Fresher
How do % and remainder() treat a negative number in Dart?
Answered in the lesson Numbers in Dart: int, double, BigInt and the web
-
Fresher
What do length, runes and characters each count in a Dart string?
Answered in the lesson Dart strings, interpolation and Unicode text
-
Fresher
How do == and identical() differ in Dart?
Answered in the lesson Operators, cascades and equality in Dart
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 small server declares a top-level final settings = loadSettings();, which reads a file. One morning the file appeared a few seconds after the server started. The first request failed, every later one worked, and the log shows loadSettings running twice. Explain all three observations, and say how you would load the settings instead.
- What it tests
- Knowing that top-level and static variables are initialised at their first read, what a throwing initializer leaves behind, and why settings are better loaded and checked at startup.
- First step
- Start from when the initializer runs: not when the server starts, but at the first read of settings, inside the first request. Then ask what a throw leaves in a variable that was never written.
- Follow-ups
-
- Why did loadSettings run a second time?
- How would you make a missing settings file stop the server at startup?
- What changes if loadSettings itself reads settings while it runs?
From the lesson Variables: var, final, const and type inference
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 4 minutes
-
Mid to senior
A service decodes request bodies with jsonDecode and hands the result around as dynamic. A typo, body['items'].lenght, compiled without a warning and failed only in production, with a NoSuchMethodError. How would you type decoded JSON, and which analyzer settings report such code before it runs?
- What it tests
- Knowing what dynamic turns off, how Object? and patterns move the checks to the boundary, and which analyzer modes and lints flag dynamic code.
- First step
- Start from the declared return type of jsonDecode, dynamic, and what it means for every member access on the result. Then decide where the program turns untyped data into typed values, and what that step must check.
- Follow-ups
-
- Why did the typo compile at all?
- What does strict-casts report, and what does avoid_dynamic_calls add?
- Where would a misspelt key such as body['itmes'] still slip through, and how do you catch it?
From the lesson Variables: var, final, const and type inference
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 4 minutes
-
Fresher to mid
A bill-splitting screen divides 1,000 rupees among 3 friends. It computes 1000 / 3, shows 333.33 for each person, and the three shares add up to 999.99, one paisa short of the bill. A 5 % service charge is coming next. How do you compute every share so the shares always add up to the bill?
- What it tests
- Keeping money as whole numbers of its smallest unit, splitting with integer division and the leftover, and choosing one place and one rule for rounding.
- First step
- Move the whole computation to an int of paise: 1,000 rupees is 100000 paise. 100000 ~/ 3 is 33333 and 100000 % 3 is 1, so two shares are 33333 paise and one is 33334. Then decide who gets the extra paisa, and write the rule down.
- Follow-ups
-
- Where does the service charge get rounded, and by which rule?
- The amount is to be split in proportion to what each person ordered. What changes?
- How do you test that the shares always add up?
From the lesson Numbers in Dart: int, double, BigInt and the web
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 4 minutes
-
Fresher to mid
A sign-in screen texts a 6-digit code. The code is made with Random(DateTime.now().millisecondsSinceEpoch).nextInt(999999), stored as an int, and compared with int.parse of what the person types. Name every problem you see, and say what you would ship instead.
- What it tests
- Knowing why a time-seeded Random is guessable, the exclusive bound of nextInt, leading zeros lost in an int, and what else keeps a short code safe: attempts, expiry and single use.
- First step
- Start with the generator: a seed taken from the clock can be guessed to within a few seconds, and every code then follows from it, so the code must come from Random.secure(). Then read the rest of the line for bugs that have nothing to do with secrecy.
- Follow-ups
-
- Which codes can this line never produce, and which can it not show correctly?
- A million possible codes sounds like plenty. What stops someone from trying them all?
- How do you test code that uses Random.secure()?
From the lesson Numbers in Dart: int, double, BigInt and the web
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 4 minutes
-
Fresher to mid
A push service allows notification titles of 64 UTF-8 bytes and cuts longer ones at byte 64. Your Dart backend first shortens each title with substring(0, 64). English titles arrive intact, but many Hindi and emoji titles arrive cut short, some ending in �. Why, and how should the backend shorten titles?
- What it tests
- Knowing that length and substring count UTF-16 code units while the limit counts UTF-8 bytes, that a cut must fall between grapheme clusters, and combining both in one shortening function with tests.
- First step
- Measure one broken Hindi title three ways: title.length, utf8.encode(title).length and title.characters.length. If its length is under 64 while its UTF-8 size is far above, substring(0, 64) never cut it, and the service's own cut at byte 64 landed inside a character.
- Follow-ups
-
- Why did substring(0, 64) leave the Hindi title too long?
- Where does the ellipsis go, and what does it cost?
- What if one grapheme cluster is larger than the whole budget?
- How would you test the function?
From the lesson Dart strings, interpolation and Unicode text
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 5 minutes
-
Mid to senior
Your app shows a contact list sorted with names.sort(). Users report that arjun comes after Zoya, that Élodie comes after every English name, and that Hindi and Tamil names always come last. Why does this happen, and how would you give each reader an order that makes sense to them?
- What it tests
- Knowing that compareTo is code-unit order, what collation is and why it depends on the reader's language, and where a Dart app can get a locale-aware comparison that stays deterministic.
- First step
- Explain the symptoms from the code units: sort() calls compareTo, so capitals (0x41 to 0x5A) come before small letters (0x61 to 0x7A), É (0xC9) after both, and each script as one block in the order of the Unicode blocks. Then say what users expect instead: collation for their language, which no code-unit order can give.
- Follow-ups
-
- Is lower-casing both names before comparing enough?
- Which order should names in different scripts take?
- Where should the sorting happen when the list is paged from a server?
- How do you keep the order deterministic and testable?
From the lesson Dart strings, interpolation and Unicode text
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 5 minutes
-
Fresher to mid
A price cache is a Map<List<int>, int> keyed by [storeId, itemId]. Every request builds its key list again, every lookup misses although the same price was stored a moment earlier, and the map keeps growing. What is wrong, and how would you key the cache instead?
- What it tests
- Knowing that a map finds a key with hashCode and ==, that List keeps the identity-based versions of both, and choosing a key type with value equality, such as a record.
- First step
- Ask how a map recognises a key: it compares hash codes first, then ==. Both come from the key's class, and List inherits them from Object, so two lists with the same ids are two different keys. Check it by printing [7, 42] == [7, 42] and the hash codes of two such lists.
- Follow-ups
-
- Why does the map keep growing instead of replacing the old entry?
- Why does a record key such as (storeId, itemId) work?
- Would const [7, 42] as the key fix it?
- What goes wrong if a key list is changed after it was stored?
From the lesson Operators, cascades and equality in Dart
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 4 minutes
-
Mid to senior
A settings loader returns List<Object>, because a list may hold numbers or text. One caller knows its list holds only ports and writes final ports = values as List<int>;. It throws a TypeError although every element is an int. Why, and what should the caller write instead?
- What it tests
- Knowing that is and as check the runtime type of the object itself, that Dart keeps type arguments at run time, and the trade-offs of cast<int>(), List<int>.from and a collection for that checks each element.
- First step
- Print values.runtimeType. The list object is a List<Object>, whatever it holds, and as checks that one object's type, not the types of its elements. A List<Object> may take a String at any time, so it can never be a List<int>; the fix has to look at each element instead.
- Follow-ups
-
- Why does values is List<int> give false while every element is an int?
- Would the cast work if the loader built the list as <int>[8080, 8443]?
- How do cast<int>(), List<int>.from and a collection for differ when one element is a String?
- When is whereType<int>() the wrong fix?
From the lesson Operators, cascades and equality in Dart
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 4 minutes