1/39
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
In Dart, what does this mean?
print("a + b = ${sum}");It takes the expression corresponding to the sum variable and concatenates it with the rest of the String.
Using String Interpolation, print a sentence using both of the following variables:
int quantity = 3;
String item = "apple";int quantity = 3;
String item = "apple";
print("I have $quantity $item");When doing String Interpolation, how do we know whether to include curly braces or not?
In String Interpolation, we only use curly braces when we have an expression, such as:
int ans = a + b;
print("hello ${ans}");
// OR
print("hello ${a+b}");Otherwise, we don’t need curly braces:
String name = "Sofia";
print("My name is $name");Is the following expression valid?
print("Your name has {name.length} characters");Yes, this is perfectly legal.
How does Dart handle single and double quotes?
Single and double quotes can be mixed interchangeably. So you can you use either one wherever, as long as it’s done consistently, meaning if you open with a single quote, you must finish with a single quote.
Example:
String message = "She said Hello";
String message = 'She said Hello';
String message = "She said 'Hello'";Is this allowed in Dart?
print("Hello" + name);No, it is not. You must use String interpolation.
print("Hello $name");How does var work? What will the following variable be interpreted as?
var name = "Sofia";This variable will be interpreted as a String.
var allows Dart to infer the type of the variable based on the value provided.
However, once you assign a var variable to a specific type, you can no longer reassign it to a value of a different type.
Meaning:
var name = "Sofia";
name = 26; // NOT ALLOWED, once you assign var to a certain type of value, you can only reassign it to values of the same type (String)How does dynamic work? What will the following variable be interpreted as?
dynamic x = 10;dynamic also allows Dart to infer the type based on the value provided. The difference is that it allows you to reassign variables to values of a different type. It gives you much more freedom and does not compare/check operations on the value in the same way as var.
Meaning:
dynamic x = 10;
x = "Hello"; // THIS IS ALLOWEDWhat is Object?
Object is a type in Dart.
It is also the superclass of all Dart classes except for null.
Is this allowed? Why?
Object value = "Hello";
value = 10;This is allowed because a String is an object, and an Integer is also an object. Dart recognizes both as subtypes of the Object class, so it allows this.
What is the difference between dynamic and Object?
Object value = "Hello";
dynamic value = "Hello";In the first one, Dart only recognizes value as an Object. So it only allows you to reassign it to other values of type “Object” or conduct operations on it using other variables of type “Object”.
In the second one, Dart recognizes value as a String. All dynamic does is it tells Dart to not enforce static type checking. This means you could reassign it to a variable of any type and conduct operations on it with numbers (like division).
In each of the following examples, identify the following:
What type Dart thinks the variable is
Whether it can hold a different type later
Whether it has static safety
// Example 1:
String x = "Hi";
// Example 2:
var x = "Hi";
// Example 3:
Object x = "Hi";
// Example 4:
dynamic x = "Hi";// Example 1:
String x = "Hi";
// Type: String
// Cannot hold a different type later
// Static safety: True
// Example 2:
var x = "Hi";
// Type: infers String
// Cannot hold a different type later
// Static safety: True
// Example 3:
Object x = "Hi";
// Type: Object
// Can hold a different type later, but only compatible objects
// Static Safety: True
// Example 4:
dynamic x = "Hi";
// Type: dynamic
// Can hold a different type later, any type
// Static Safety: False/ReducedWhat’s the difference between compile time and runtime?
Compile Time = When the program is being prepared/checked to run.
Runtime = When the program actually executes.
Create a list called names, and add two names to it.
List<String> names = ["Sofia", "Anas"];Are you allowed to reassign a list to another list?
List<String> names = ["Sofia", "Anas"];
List<String> fastFood = ["Burgers", "Fries"];
names = fastFood;Yes, this is allowed and valid.
In Dart, are you allowed to add items to a pre-existing list? How can we add a third name “Lachesis” to the following list?
names = ["Sofia", "Anas"];List<String> names = ["Sofia", "Anas"];
names.add("Lachesis");If a list is final, are you still allowed to modify it by adding new elements or by reassigning it to another list?
For example, is this allowed?
final List<String> names = ["Sofia", "Anas"];
names.add("Lachesis");
List<String> fastFood = ["Burger", "Fries"];
names = fastFood;Yes, this is allowed. Final only prevents you from reassigning a list to another list.
What is the actual definition of const?
const, by definition, means a variable whose value must be known at compile time (before execution).
const int number = 5; // completely fine
const int result = 3+5; // also allowed, because the calculation itself can be determined from constant values. Nothing that happens during execution time is going to determine this value.What modifier does allow you to receive values at runtime?
final allows you to receive something at runtime.
For example:
final String answer = getSomethingAtRuntime();Are you allowed to modify a const list?
const List<String> names = ["a", "b", "c", "d"];
names.add("c");No, you are not allowed. Doing this is completely invalid:
const List<String> names = ["a", "b", "c", "d"];
names.add("c"); // NOT ALLOWED WITH CONST LISTSIs type inference allowed with const and final?
For example, is this allowed?
final name = "Sofia";
const name = "Sofia";Yes actually!! Both const and final allow type inference, which can save you time. You do not have to include the type unless you want to.
final String name = "Sofia";Make a list of the following fruits in Dart:
Apple
Banana
Cherry
List<String> fruits = ["Apple", "Banana", "Cherry"];How can we access elements in the following fruit list? For example, access element 1.
List<String> fruits = ["Apple", "Banana", "Cherry"];List<String> fruits = ["Apple", "Banana", "Cherry"];
fruits[1];How can we find the length of the following fruit list?
List<String> fruits = ["Apple", "Banana", "Cherry"];List<String> fruits = ["Apple", "Banana", "Cherry"];
fruits.length;Create a var list of the fruits: “Apple”, “Banana”, “Cherry”.
What happens when we create a list using var?
var fruits = ["Apple", "Banana", "Cherry"];When we create a list using var, Dart infers the type of the list. It sees [“Apple”, “Banana”….] so it thinks List<String>
How can we create a list of dynamic values? This list should contain a String, an int, a bool value, and a double.
List<dynamic> values = ["Sofia", 42, true, 3.14];What does the following statement mean?
List<List<String>>It means a List containing lists of Strings.
It looks like:
[
[ “apple”, “banana”],
[ “cat”, “dog”],
[“red”, “blue”]
]
Describe what the following lines are doing:
for(var f in fruits) {
print("fruit $f");
}It repeatedly assigns each element from the fruit list to f.
so f = “Apple”,
then f = “Banana”,
then f = “Cherry”
and it prints out each value subsequently after assigning it.
What does the following example do?
for(int i = 0; i < fruits.length; i++) {
print(i.toString() + ":" + fruits[i]);
}This converts each i to a String and concatenates it to each element in the fruits list.
For example:
0: Apple
1: Banana
..etc
How can we create an empty list of Strings using var?
var names = <String>[];
// var lets Dart infer that names is a List<String>
// <String>[] creates an empty list of StringsWhat does the syntax for each of these represent?
<int>[]
<String>[]
<Widget>[]<int>[] // Empty list of int
<String>[]
<Widget>[] // Empty list of WidgetWhat type do you expect Dart to infer here?
var numbers = [1,2,3];Dart infers List<int>
What does List<dynamic> mean?
It means a list of dynamic values, various values that include anything and everything all in the same list, including ints, doubles, Strings, bools, etc.
How would you write this function in Dart?
int getSum(int a, int b) {
int answer = a + b;
return answer;
}Wait!! This is a trick question!! This is already a function in Dart!! It looks identical to Java!
int getSum(int a, int b) {
int answer = a + b;
return answer;
}What does the => operator represent in Dart?
The arrow represents a return expression with curly braces:
{
return expression;
}How can we shorten the following function using shorthand arrow notation?
int doubleNumber(int number) {
return number * 2;
}int doubleNumber(int number) => number * 2;Why is arrow => notation limited in what it can do?
We say it’s limited in what it can do because it can only return a single expression. Doing something like this would be completely invalid, because we need to return multiple statements.
int add(int a, int b) => {
int answer = a + b;
print(answer);
return answer; // COMPLETELY WRONG
}Is a function an object?
Yes, a function is an object. This means that a function can be assigned to a variable or passed as arguments to another function.
Let’s say we have the following function:
void sayHello() {
print("hello");
}Show how you would call the function vs. how would call the function itself as a value.
sayHello(); // This calls the function.
sayHello; // This calls the function itself as a value.
// So you can do:
var action = sayHello; // this assigns the function to a variableIf sayHello() is a function, what does it mean when we assign it to a variable?
var action = sayHello;If sayHello() has the function behavior to print(“Hello”); then it means that the variable, action, will have the same functionality.