Dart Basics & Null Safety Questions with Answers

15 practice questions on Dart Basics & Null Safety (Flutter & Dart) with answers and step-by-step solutions. Try 10 samples below, then practise all 15 free in the AptiNest app.

Sample questions

Question 1Easy

In Dart with null safety, what does the declaration int? count; create?

  1. A nullable int variable whose initial value is null
  2. An int that must be assigned before the next statement
  3. A non-nullable int that defaults to 0 until assigned
  4. A compile error, because nullable variables need a value

Answer: A. A nullable int variable whose initial value is null

Explanation

The? suffix makes the type nullable. A nullable variable without an initializer starts as null.

Step-by-step solution

  1. The? suffix allows the int to be null.
  2. Variables with? can start as null without initial value.
  3. Count is declared but not assigned, so it's null by default.
  4. This matches option A exactly.
  5. Final answer: A nullable int variable whose initial value is null.

Why the other options are wrong

  • B. The? makes the type nullable, so no assignment is required before the next statement.
  • C. A plain int does not silently default to 0; the question mark makes this variable nullable instead.
  • D. Declaring a nullable variable without a value is legal in Dart; it simply starts as null, so no error occurs.

Tip: Use? for variables that might not have a value yet.

Question 2Easy

What is the main difference between final and const in Dart?

  1. They are identical keywords and can always be swapped freely
  2. final needs a compile-time constant; const may be set at runtime
  3. const is fixed at compile time; final is set once at runtime
  4. const variables can be reassigned, final variables cannot

Answer: C. const is fixed at compile time; final is set once at runtime

Explanation

Both prevent reassignment. A const value must be known at compile time, while a final can hold a value computed at runtime, such as DateTime.now().

Step-by-step solution

  1. Final variables are set once at runtime, const values are fixed at compile time.
  2. Const requires a compile-time constant, final can use runtime calculations.
  3. Const is evaluated at compile time, final is assigned during program execution.
  4. Final allows runtime computation, const must be known when code is compiled.
  5. The correct answer is: const is fixed at compile time; final is set once at runtime.

Why the other options are wrong

  • A. final and const are not identical or freely swappable; const must be a compile-time constant value.
  • B. This reverses the rule: const needs a compile-time constant, while final can be set from a runtime value.
  • D. Neither final nor const can be reassigned after it is set, so this reverses the real rule.

Tip: Remember: const is compile-time constant, final is runtime value that cannot be changed.

Question 3Easy

What is the output?

void main() {
  String? name;
  print(name ?? 'Guest');
}
  1. An empty line
  2. Guest
  3. null
  4. Compile error: name is not initialised

Answer: B. Guest

Explanation

?? returns the right operand when the left one is null. name is a nullable local that defaults to null.

Step-by-step solution

  1. Name is a nullable String with no value, so it is null.
  2. The ?? operator returns the left side unless it is null.
  3. Since name is null, the right side 'Guest' is printed.

Why the other options are wrong

  • A. An empty line would need name to be an empty string, which it is not.
  • C. The ?? operator replaces null, so null is never printed here.
  • D. Dart does not raise an error for an uninitialised nullable local; it is null.

Tip: X ?? y gives x unless x is null, in which case it gives y.

Question 4Easy

What happens with this code?

void main() {
  var x = 5;
  x = 'hi';
}
  1. It runs, because var means the same as dynamic
  2. Compile error, because var inferred x as int
  3. Runtime TypeError when the second line executes
  4. It runs, and x silently becomes a String

Answer: B. Compile error, because var inferred x as int

Explanation

var infers the static type from the initializer (int here). Assigning a String to an int variable is rejected at compile time.

Step-by-step solution

  1. The variable x is initialized with the integer 5.
  2. The var keyword infers the static type of x as int.
  3. The second line attempts to assign the string 'hi' to x.
  4. Assigning a String to an int variable is a type mismatch.
  5. The compiler rejects this invalid assignment.
  6. Compile error, because var inferred x as int.

Why the other options are wrong

  • A. var does not mean dynamic; it infers a fixed type from the first value, so the reassignment is rejected.
  • C. The error is caught at compile time, before the program runs, not as a runtime TypeError.
  • D. Dart does not silently change a variable's type; the assignment is rejected, so the program does not run.

Tip: Remember that var is not dynamic; it locks in the type of the first value assigned.

Question 5Medium

In Dart, what does "sound" null safety guarantee?

  1. A non-nullable type can never hold null at runtime
  2. Null checks happen at runtime only and never at compile time
  3. Nullable variables cannot be passed to any function parameter
  4. Null values are converted to defaults like 0 or an empty string

Answer: A. A non-nullable type can never hold null at runtime

Explanation

Soundness means the static types are trustworthy: if the type is non-nullable, the compiler guarantees the value is never null, so it can also optimise away checks.

Step-by-step solution

  1. Sound null safety means the type system guarantees the rule.
  2. A non-nullable type such as int can never hold null at runtime.
  3. So option A is the guarantee.

Why the other options are wrong

  • B. Sound null safety checks mostly happen at compile time, not only at runtime.
  • C. Nullable values can be passed to parameters whose type accepts null.
  • D. Dart does not turn null into 0 or an empty string.

Tip: A non-nullable type means the compiler ensures it never holds null.

Question 6Medium

What is the output?

void main() {
  var list = [1, 2, 3];
  print(list.map((e) => e * 2));
}
  1. (2, 4, 6)
  2. Instance of 'List<int>'
  3. [2, 4, 6]
  4. [1, 2, 3]

Answer: A. (2, 4, 6)

Explanation

map returns a lazy Iterable, not a List. Iterable's toString uses parentheses; you would need .toList() to print [2, 4, 6].

Step-by-step solution

  1. list.map returns a lazy Iterable, not a List.
  2. Each value is e × 2, giving 2, 4 and 6.
  3. Print shows this Iterable as (2, 4, 6).

Why the other options are wrong

  • B. Instance of text appears for objects without a readable toString; this Iterable prints its values.
  • C. A List would print with square brackets, but map does not return a List.
  • D. Map does not change the original list, which stays [1, 2, 3].

Tip: Map is lazy; call .toList() to get a List, which prints with square brackets.

Question 7Medium

What is the output?

void main() {
  var sb = StringBuffer()..write('a')..write('b');
  print(sb.toString());
}
  1. b
  2. a
  3. ab
  4. Compile error: write returns void

Answer: C. ab

Explanation

The cascade operator .. calls each method on the same receiver and the whole expression evaluates to that receiver, the StringBuffer, so both writes apply.

Step-by-step solution

  1. StringBuffer() creates one object.
  2. The cascade .. calls write('a') and then write('b') on that same object.
  3. The buffer now holds ab.
  4. toString() gives ab, which is option C.

Why the other options are wrong

  • A. B is only the second write, not the full text.
  • B. A is only the first write, not the full text.
  • D. Write returns void, but a cascade keeps the target object, so this compiles.

Tip: The cascade operator .. runs each call on the same object and returns that object.

Question 8Medium

What does the late modifier on late String token; mean?

  1. token becomes a constant once the first value is assigned to it
  2. Non-nullable, assigned later; reading it first throws
  3. token is nullable and silently returns null until it is assigned
  4. token is computed on a background isolate when first needed

Answer: B. Non-nullable, assigned later; reading it first throws

Explanation

late defers initialization of a non-nullable variable. Reading it before assignment throws a LateInitializationError at runtime.

Step-by-step solution

  1. Late tells Dart the variable will be assigned later.
  2. The variable is non-nullable and cannot hold null.
  3. Reading it before assignment throws LateInitializationError.
  4. This ensures no accidental use of unassigned variables.
  5. It helps catch errors early in development.
  6. Final answer: Non-nullable, assigned later; reading it first throws.

Why the other options are wrong

  • A. late does not make a variable constant; it only delays assignment, and the variable can still change.
  • C. A late String is not nullable; it never returns null, and reading it early throws instead.
  • D. late does not run code on a background isolate; it only defers the assignment until later.

Tip: Use late for non-nullable variables that are assigned later to avoid runtime errors.

Question 9Hard

What happens with this code?

class User {
  String? name;
}

void greet(User u) {
  if (u.name != null) {
    print(u.name.length);
  }
}
  1. Compile error, because a class field can never be nullable
  2. It compiles but throws at runtime when name is reassigned
  3. Compile error: the public field is not promoted
  4. It compiles, because the null check promotes u.name to String

Answer: C. Compile error: the public field is not promoted

Explanation

Type promotion only applies to locals and some private final fields. A public non-final field could be overridden by a getter or changed between the check and the use, so you must copy it into a local or use !.

Step-by-step solution

  1. Promotion works for locals, not for public non-final fields.
  2. u.name could be overridden or changed between check and use.
  3. So u.name.length is a compile error.

Why the other options are wrong

  • A. Fields can be nullable.
  • B. It does not compile at all.
  • D. Fields are not promoted automatically.

Tip: Copy to a local first.

Question 10Hard

What is the output?

void main() {
  print('${-7 ~/ 2} ${-7 % 2}');
}
  1. -3 -1
  2. -3 1
  3. -4 -1
  4. -4 1

Answer: B. -3 1

Explanation

~/ is truncating integer division, so -3.5 becomes -3. Dart's % operator is a Euclidean modulo whose result is never negative for a positive divisor, so it gives 1 (remainder() would give -1).

Step-by-step solution

  1. ~/ is integer division that truncates toward zero: -7 ~/ 2 is -3.5 truncated to -3.
  2. Dart's % gives a result from 0 up to the divisor minus 1 for a positive divisor: -7 % 2 is 1.
  3. So the output is -3 1, which is option B.

Why the other options are wrong

  • A. Dart's % does not return -1 here; the result is 1.
  • C. ~/ truncates toward zero, so the result is -3, not -4.
  • D. Both parts are wrong: the division gives -3 and the remainder gives 1.

Tip: In Dart, ~/ rounds toward zero and % gives a result from 0 up to the divisor minus 1.

Practise all 15 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Flutter & Dart topics · All topics