Bird
Raised Fist0
Fluttermobile~20 mins

Variables and type inference in Flutter - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Variables and Type Inference Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
1:30remaining
Understanding Dart variable type inference
What is the type of variable x after this code runs?
var x = 42;
Flutter
var x = 42;
Adynamic
Bdouble
Cint
DString
Attempts:
2 left
💡 Hint
Dart infers the type from the assigned value.
❓ ui_behavior
intermediate
1:30remaining
Effect of variable type on Flutter widget display
Given this Flutter code snippet, what will be displayed on the screen?
var message = 123;
return Text(message.toString());
Flutter
var message = 123;
return Text(message.toString());
AThe number 123 as text
BAn error because Text expects a String variable
CNothing is displayed
DThe word 'message' is displayed
Attempts:
2 left
💡 Hint
Check how the integer is converted to text.
❓ lifecycle
advanced
2:00remaining
Variable type and widget rebuild behavior
In a Flutter StatefulWidget, if you declare a variable with var count = 0; inside the State class and update it inside setState(), what happens to the variable type during rebuilds?
Flutter
class _MyWidgetState extends State<MyWidget> {
  var count = 0;

  void increment() {
    setState(() {
      count++;
    });
  }
}
AThe variable type changes to dynamic after first rebuild
BThe variable type remains int across rebuilds
CThe variable is reset to 0 on every rebuild
DThe variable becomes null after rebuild
Attempts:
2 left
💡 Hint
Consider where the variable is declared and how Flutter rebuilds widgets.
🔧 Debug
advanced
2:00remaining
Identifying type inference error in Dart
What error will this Dart code produce?
var name = null;
name = "Flutter";
Flutter
var name = null;
name = "Flutter";
ANo error, name becomes a String
BSyntax error due to missing type declaration
CRuntime error when assigning String to null
DA compile-time error because type is inferred as Null
Attempts:
2 left
💡 Hint
Think about what type Dart infers when initialized with null.
🧠 Conceptual
expert
2:00remaining
Why use explicit types instead of var in Flutter?
Which reason best explains why you might prefer explicit types over var in Flutter app variables?
AExplicit types improve code readability and catch type errors early
BExplicit types make the app run faster at runtime
CUsing <code>var</code> disables null safety features
DExplicit types reduce the app size significantly
Attempts:
2 left
💡 Hint
Think about how explicit types help developers understand code.

Practice

(1/5)
1. What does the var keyword do in Dart when declaring a variable?
easy
A. It lets Dart guess the variable type from the assigned value.
B. It declares a variable without assigning any value.
C. It forces the variable to be of type String.
D. It creates a constant variable that cannot change.

Solution

  1. Step 1: Understand var usage

    The var keyword tells Dart to infer the type based on the value assigned.
  2. Step 2: Check other options

    Options B, C, and D describe different behaviors not related to var.
  3. Final Answer:

    It lets Dart guess the variable type from the assigned value. -> Option A
  4. Quick Check:

    Type inference = It lets Dart guess the variable type from the assigned value. [OK]
Hint: Remember: var means Dart guesses the type from the value [OK]
Common Mistakes:
  • Thinking var declares a variable without a value
  • Confusing var with const or final
  • Assuming var always means String type
2. Which of the following is the correct way to declare an integer variable with explicit type in Dart?
easy
A. integer count = 10;
B. int count = 10;
C. var int count = 10;
D. count int = 10;

Solution

  1. Step 1: Recall Dart syntax for typed variables

    In Dart, the type comes first, then the variable name, then the value.
  2. Step 2: Check each option

    int count = 10; follows correct syntax: int count = 10;. Others are invalid syntax.
  3. Final Answer:

    int count = 10; -> Option B
  4. Quick Check:

    Explicit type declaration = int count = 10; [OK]
Hint: Type then name then value: int count = 10; [OK]
Common Mistakes:
  • Putting var and type together
  • Using 'integer' instead of 'int'
  • Swapping variable name and type
3. What will be the output of this Dart code?
var name = 'Alice';
var age = 30;
print('$name is $age years old');
medium
A. Alice is 30 years old
B. name is age years old
C. 30 is Alice years old
D. Error: Cannot print variables like this

Solution

  1. Step 1: Understand string interpolation

    The \$ syntax inserts variable values inside strings in Dart.
  2. Step 2: Substitute variables in the print statement

    Variables name and age are replaced by 'Alice' and 30 respectively.
  3. Final Answer:

    Alice is 30 years old -> Option A
  4. Quick Check:

    String interpolation = Alice is 30 years old [OK]
Hint: Use \$variable to insert values in strings [OK]
Common Mistakes:
  • Printing variable names instead of values
  • Forgetting to escape $ in strings
  • Assuming print shows variable types
4. Identify the error in this Dart code:
var number;
number = 5;
number = 'five';
medium
A. Error: Variable must be declared with final
B. Error: Cannot assign a String to a variable inferred as int
C. No error, Dart allows changing types with var
D. Error: Missing semicolon after first line

Solution

  1. Step 1: Understand type inference with var and no initial value

    When declared without initial value, var number; defaults to dynamic type.
  2. Step 2: Check assignments

    Assigning 5 (int) then 'five' (String) is allowed only if type is dynamic. But Dart infers type on first assignment if declared with var and initialized immediately. Here, no initial value means dynamic, so no error.
  3. Final Answer:

    No error, Dart allows changing types with var -> Option C
  4. Quick Check:

    Uninitialized var = dynamic type = No error, Dart allows changing types with var [OK]
Hint: Uninitialized var is dynamic, so type can change [OK]
Common Mistakes:
  • Assuming var always fixes type on declaration
  • Thinking missing semicolon causes error here
  • Confusing final with var
5. Given this Dart code, what is the type of result?
var a = 10;
var b = 3.5;
var result = a + b;
hard
A. int
B. String
C. num
D. double

Solution

  1. Step 1: Identify types of variables a and b

    a is int (10), b is double (3.5).
  2. Step 2: Understand type inference of arithmetic operations

    Adding int and double results in a double in Dart.
  3. Final Answer:

    double -> Option D
  4. Quick Check:

    int + double = double [OK]
Hint: int plus double results in double type [OK]
Common Mistakes:
  • Assuming result is int because a is int
  • Choosing num instead of double
  • Thinking result is String due to concatenation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard