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 2

Values and types: variables, numbers, strings and operators interview questions

Interview questions on “Values and types: variables, numbers, strings and operators”.

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

    When does a Dart value need const rather than final?

    Answered in the lesson Variables: var, final, const and type inference

  2. Fresher

    How do % and remainder() treat a negative number in Dart?

    Answered in the lesson Numbers in Dart: int, double, BigInt and the web

  3. Fresher

    What do length, runes and characters each count in a Dart string?

    Answered in the lesson Dart strings, interpolation and Unicode text

  4. 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.

  1. 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

    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.

    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
  2. 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

    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
  3. 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

    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
  4. 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

    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
  5. 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

    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
  6. 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

    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
  7. 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

    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
  8. 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

    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

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.