Dart interview questions

25 questions from the Dart course, each with the correct answer and an explanation. Try to answer before you open the answer.

Getting Started with Dart

1. Which compilation mode does Flutter use during development to support hot reload?

  1. JIT compilation on the Dart VM
  2. AOT compilation to native code
  3. Compilation to JavaScript only
  4. Interpretation without compilation
Show answer

Answer: JIT compilation on the Dart VM

The JIT on the Dart VM allows code to be updated in a running app.

Lesson: What Dart Is and Where It Runs

2. Which file declares a Dart package's dependencies and SDK constraint?

  1. package.json
  2. build.gradle
  3. Cargo.toml
  4. pubspec.yaml
Show answer

Answer: pubspec.yaml

pubspec.yaml is the manifest for Dart and Flutter packages.

Lesson: The Dart SDK, Projects and pubspec.yaml

3. What is the difference between final and const in Dart?

  1. There is none
  2. const can be reassigned
  3. final is set once at run time; const must be a compile-time constant and is deeply immutable
  4. final only works with numbers
Show answer

Answer: final is set once at run time; const must be a compile-time constant and is deeply immutable

const values are known at compile time and canonicalised; final variables are assigned once.

Lesson: Variables, final, const and Basic Types

Null Safety, Control Flow and Collections

4. In Dart 3, what does the type String (without ?) guarantee?

  1. It may be null
  2. It is always empty
  3. It can never be null
  4. It is dynamic
Show answer

Answer: It can never be null

With sound null safety, non-nullable types cannot hold null.

Lesson: Sound Null Safety

5. What does `[ 'Home', if (isAdmin) 'Admin' ]` produce when isAdmin is false?

  1. [Home, null]
  2. [Home]
  3. [Home, Admin]
  4. A compile error
Show answer

Answer: [Home]

Collection if omits the element entirely when the condition is false.

Lesson: Control Flow and Collection Literals

6. What does Iterable.where return?

  1. A lazy Iterable whose elements are computed when iterated
  2. A new List immediately
  3. A Set
  4. A Future
Show answer

Answer: A lazy Iterable whose elements are computed when iterated

where and map are lazy; convert with toList() for a concrete collection.

Lesson: Lists, Sets, Maps and Iterables

Functions and Error Handling

7. How do you make a named parameter mandatory in Dart?

  1. Mark it with the required keyword
  2. Put it in square brackets
  3. Give it a default value
  4. Make it final
Show answer

Answer: Mark it with the required keyword

Named parameters are optional unless marked required.

Lesson: Functions and Parameters

8. What is a tear-off in Dart?

  1. A way to delete a function
  2. A type of exception
  3. A collection literal
  4. Using a function or method name as a function value without wrapping it in a lambda
Show answer

Answer: Using a function or method name as a function value without wrapping it in a lambda

Tear-offs like list.forEach(print) pass existing functions directly.

Lesson: Closures, Function Types and Tear-offs

9. Which keyword rethrows the current exception while preserving its stack trace?

  1. throw
  2. finally
  3. rethrow
  4. on
Show answer

Answer: rethrow

rethrow keeps the original stack trace, unlike throw e.

Lesson: Exceptions and Errors

Classes and Object-Oriented Dart

10. What can a factory constructor do that a generative constructor cannot?

  1. Take parameters
  2. Initialise final fields
  3. Return an existing cached instance or a subtype instead of always creating a new instance of the class
  4. Be called with new
Show answer

Answer: Return an existing cached instance or a subtype instead of always creating a new instance of the class

Factory constructors decide what to return, including cached objects or subclasses.

Lesson: Classes, Constructors and Initialiser Lists

11. In Dart, how can a class promise to provide another class's members without inheriting its code?

  1. extends
  2. implements
  3. with
  4. super
Show answer

Answer: implements

Every class defines an implicit interface that other classes can implement.

Lesson: Inheritance, Interfaces, Abstract Classes and Mixins

12. What does marking a class sealed enable?

  1. Exhaustiveness checking in switch, because all subtypes are known within the library
  2. Faster method calls
  3. Multiple inheritance
  4. Automatic JSON serialisation
Show answer

Answer: Exhaustiveness checking in switch, because all subtypes are known within the library

Sealed classes have a known, closed set of subtypes.

Lesson: Enhanced Enums and Dart 3 Class Modifiers

Records, Patterns, Generics and Extensions

13. How do records compare for equality in Dart?

  1. Structurally: same shape and equal field values means equal
  2. By identity only
  3. They cannot be compared
  4. Only by their first field
Show answer

Answer: Structurally: same shape and equal field values means equal

Records have structural equality and matching hash codes.

Lesson: Records and Destructuring

14. What does the list pattern `[final first, ..., final last]` match?

  1. Only lists with exactly three elements
  2. Empty lists
  3. Maps
  4. Lists with at least two elements, binding the first and last
Show answer

Answer: Lists with at least two elements, binding the first and last

The rest element ... matches any number of elements between first and last.

Lesson: Patterns and Switch Expressions

15. What is the main benefit of an extension type such as OrderId(String value)?

  1. It adds run-time validation automatically
  2. It makes the value mutable
  3. It gives a distinct static type with no run-time overhead, preventing mix-ups with plain strings
  4. It replaces generics
Show answer

Answer: It gives a distinct static type with no run-time overhead, preventing mix-ups with plain strings

Extension types are compile-time wrappers that erase to the underlying type at run time.

Lesson: Generics, Extension Methods and Extension Types

Asynchronous Dart

16. How do you run two independent futures concurrently and wait for both?

  1. await each one in sequence
  2. Use forEach with async
  3. Start both and await Future.wait (or the record .wait)
  4. Wrap them in a Stream
Show answer

Answer: Start both and await Future.wait (or the record .wait)

Starting both first and waiting together lets them run at the same time.

Lesson: Futures and async/await

17. What does an async* function return?

  1. A Future
  2. A Stream, emitting values with yield
  3. A List
  4. An Isolate
Show answer

Answer: A Stream, emitting values with yield

async* generators produce streams; sync* generators produce iterables.

Lesson: Streams

18. How do Dart isolates communicate?

  1. By passing messages, since each isolate has its own memory
  2. By sharing objects in memory with locks
  3. Through global variables
  4. They cannot communicate
Show answer

Answer: By passing messages, since each isolate has its own memory

Isolates have separate heaps and exchange data via messages.

Lesson: Isolates and the Event Loop in Depth

Packages, Code Quality and Testing

19. What does the version constraint ^1.2.0 allow in pubspec.yaml?

  1. Any version from 1.2.0 up to but not including 2.0.0
  2. Exactly 1.2.0
  3. Any version at all
  4. Only 1.2.x patch releases
Show answer

Answer: Any version from 1.2.0 up to but not including 2.0.0

Caret syntax allows compatible versions within the same major version.

Lesson: Libraries, Imports and pub.dev Packages

20. Which command applies automated fixes suggested by the Dart analyser?

  1. dart format
  2. dart pub upgrade
  3. dart compile
  4. dart fix --apply
Show answer

Answer: dart fix --apply

dart fix applies analyser-provided fixes, including API migrations.

Lesson: Static Analysis, Lints and Formatting

21. Which matcher checks that a function throws a specific error type in package:test?

  1. equals
  2. isNull
  3. throwsA(isA<ArgumentError>())
  4. emitsDone
Show answer

Answer: throwsA(isA<ArgumentError>())

throwsA with `isA<T>`() asserts the thrown object's type.

Lesson: Testing with package:test and Mocks

Compiling, Servers, Flutter Context and Revision

22. Which dart compile target produces a self-contained native executable?

  1. js
  2. kernel
  3. exe
  4. wasm
Show answer

Answer: exe

dart compile exe bundles AOT code with a small runtime into one executable.

Lesson: Compilation and Deployment Targets

23. Which package provides composable HTTP handlers and middleware for Dart servers?

  1. package:args
  2. package:shelf
  3. package:test
  4. package:lints
Show answer

Answer: package:shelf

shelf is the foundational package for HTTP servers in Dart.

Lesson: Dart on the Server and the Command Line

24. Why are const constructors valuable in Flutter widget trees?

  1. Identical const widgets are canonicalised, so Flutter can reuse them and skip unnecessary rebuild work
  2. They make widgets mutable
  3. They enable hot reload
  4. They are required for every widget
Show answer

Answer: Identical const widgets are canonicalised, so Flutter can reuse them and skip unnecessary rebuild work

const instances are created once and shared, reducing rebuild cost.

Lesson: Dart Patterns You Will Use in Flutter

25. Which feature lets Dart check at compile time that a switch handles every subtype of a class?

  1. Mixins
  2. Extension methods
  3. Factory constructors
  4. Sealed classes with exhaustiveness checking
Show answer

Answer: Sealed classes with exhaustiveness checking

Switching over a sealed type is checked for exhaustiveness.

Lesson: Revision and Interview Questions

← Back to the course