Explore foundational Dart programming concepts crucial for Flutter development with this beginner-level quiz. Enhance your knowledge of syntax, data types, functions, collections, and control flow while building a solid base for creating efficient applications.
Which keyword is used to declare a variable with a fixed value in Dart, making it immutable?
Explanation: The 'const' keyword in Dart is used to declare compile-time constants, making the variable immutable. 'var' defines a variable with inferred type and is mutable, while 'late' is used for variables that will be initialized later. 'dynamic' allows variables to change type at runtime but does not make them immutable.
How can you include the value of an integer variable age in a string using Dart's string interpolation?
Explanation: Dart uses the dollar sign syntax for string interpolation, so 'Age is $age' will include the value of age in the string. '{age}' is not a Dart interpolation format, '+age+' is incorrect and would result in a string literal, and '@age' does not work for string interpolation in Dart.
What is the correct way to define the entry point of a Dart application?
Explanation: In Dart, the main entry point is defined using 'void main() { }'. 'def main() { }' is used in other languages like Python, and 'public static void main() { }' is used in Java but not valid in Dart. 'start() { }' is not a recognized entry point.
Which of the following statements correctly creates a growable list of integers in Dart?
Explanation: 'Listu003Cintu003E numbers = [1, 2, 3];' is the correct Dart syntax to create a growable list of integers. 'numbers = List[1,2,3];' is not valid Dart syntax. 'int List = (1,2,3);' does not create a list, and 'Arrayu003Cintu003E' is not a Dart type.
Which symbol is used in Dart to allow a variable to hold null values?
Explanation: In Dart, adding a '?' after a type (e.g., int?) allows the variable to hold null values, enabling null safety. The '#' symbol is not used for nullability, '!' is for null assertion, and 'u0026' has no relevance to null safety in Dart.
How do you correctly write a simple if statement in Dart to check if x is greater than 10?
Explanation: The correct Dart syntax for an if statement is 'if (x u003E 10) {}'. 'if x u003E 10 {}' misses the parentheses, 'if [x u003E 10] {}' incorrectly uses square brackets, and 'if (x u003E 10);' ends with a semicolon, which terminates the condition prematurely.
Which statement correctly declares a function named greet that returns nothing and takes no parameters?
Explanation: In Dart, functions returning nothing are declared with 'void greet() { }'. 'greet() { return void; }' is syntactically incorrect. 'function greet() { }' is not Dart syntax, and 'void greet[] { }' does not define a function properly.
How do you provide a default value of 5 for an optional named parameter in a Dart function?
Explanation: Dart uses curly braces for named parameters and assigns default values using 'int x = 5'. '[int x: 5]' and '{int x? = 5}' contain syntax errors. 'void foo(int x = 5)' makes x a positional parameter, not a named parameter.
Given a map fruits = {'apple': 3, 'banana': 5}, how do you access the value associated with 'banana'?
Explanation: In Dart, values in a map are accessed using square brackets and the key: 'fruits['banana']'. 'fruits.banana' incorrectly uses dot notation, '{banana}' is invalid, and '-u003E' is not Dart syntax for accessing map elements.
Which Dart keyword allows the compiler to automatically determine a variable’s type based on assigned value?
Explanation: 'var' in Dart enables type inference, allowing the compiler to determine type from the initial value. 'const' and 'final' both declare constants but focus on immutability, not type inference. 'auto' is not a Dart keyword for type inference.