A variable is a name that holds a value. It can store data like numbers, text, or objects that your app uses.
Click to reveal answer
beginner
How does type inference work in Flutter?
Type inference means Flutter can guess the type of a variable from the value you assign, so you don't always need to write the type explicitly.
Click to reveal answer
beginner
What keyword do you use in Flutter for a variable whose type is inferred?
You use the keyword var to declare a variable with type inference.
Click to reveal answer
intermediate
Can you change the type of a variable declared with var later?
No, once a variable is assigned a type by inference, you cannot assign a value of a different type to it later.
Click to reveal answer
intermediate
What is the difference between var and dynamic in Flutter?
var infers the type and keeps it fixed, while dynamic allows the variable to hold any type and can change at runtime.
Click to reveal answer
Which keyword lets Flutter guess the variable type automatically?
Avar
Bint
Cfinal
Dconst
✗ Incorrect
The var keyword tells Flutter to infer the type from the assigned value.
What happens if you assign a string to a var variable initially holding an int?
AThe app crashes immediately
BIt works fine
CThe variable changes type to string
DFlutter throws a type error
✗ Incorrect
Flutter fixes the type at the first assignment, so assigning a different type later causes a type error.
Which keyword allows a variable to hold any type and change it later?
Avar
Bfinal
Cdynamic
Dconst
✗ Incorrect
dynamic allows the variable to hold any type and change it at runtime.
What is the type of a variable declared as var name = 'Alice';?
AString
Bint
Cdynamic
DObject
✗ Incorrect
Flutter infers the type String because the value is text.
Which keyword declares a variable that cannot be changed after it is set?
Avar
Bfinal
Cdynamic
Dconst
✗ Incorrect
final declares a variable that can only be set once and then cannot change.
Explain how Flutter uses type inference with the var keyword.
Think about how Flutter decides the variable's type without you writing it.
You got /3 concepts.
Describe the difference between var and dynamic variables in Flutter.
Consider how flexible each variable type is after declaration.
You got /3 concepts.
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
Step 1: Understand var usage
The var keyword tells Dart to infer the type based on the value assigned.
Step 2: Check other options
Options B, C, and D describe different behaviors not related to var.
Final Answer:
It lets Dart guess the variable type from the assigned value. -> Option A
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
Step 1: Recall Dart syntax for typed variables
In Dart, the type comes first, then the variable name, then the value.
Step 2: Check each option
int count = 10; follows correct syntax: int count = 10;. Others are invalid syntax.
Final Answer:
int count = 10; -> Option B
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
Step 1: Understand string interpolation
The \$ syntax inserts variable values inside strings in Dart.
Step 2: Substitute variables in the print statement
Variables name and age are replaced by 'Alice' and 30 respectively.
Final Answer:
Alice is 30 years old -> Option A
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
Step 1: Understand type inference with var and no initial value
When declared without initial value, var number; defaults to dynamic type.
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.
Final Answer:
No error, Dart allows changing types with var -> Option C
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
Step 1: Identify types of variables a and b
a is int (10), b is double (3.5).
Step 2: Understand type inference of arithmetic operations
Adding int and double results in a double in Dart.
Final Answer:
double -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.