Dart Language Essentials for Flutter Developers Quiz

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.

  1. Variable Declaration

    Which keyword is used to declare a variable with a fixed value in Dart, making it immutable?

    1. var
    2. dynamic
    3. late
    4. const

    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.

  2. String Interpolation

    How can you include the value of an integer variable age in a string using Dart's string interpolation?

    1. 'Age is $age'
    2. 'Age is {age}'
    3. 'Age is +age+'
    4. 'Age is @age'

    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.

  3. Main Function Structure

    What is the correct way to define the entry point of a Dart application?

    1. void main() { }
    2. start() { }
    3. def main() { }
    4. public static void main() { }

    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.

  4. List Creation

    Which of the following statements correctly creates a growable list of integers in Dart?

    1. int List = (1,2,3);
    2. Arrayu003Cintu003E numbers = {1,2,3};
    3. numbers = List[1,2,3];
    4. Listu003Cintu003E numbers = [1, 2, 3];

    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.

  5. Null Safety

    Which symbol is used in Dart to allow a variable to hold null values?

    1. #
    2. !
    3. u0026
    4. ?

    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.

  6. Conditional Statement Syntax

    How do you correctly write a simple if statement in Dart to check if x is greater than 10?

    1. if (x u003E 10);
    2. if [x u003E 10] {}
    3. if x u003E 10 {}
    4. if (x u003E 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.

  7. Function Declaration

    Which statement correctly declares a function named greet that returns nothing and takes no parameters?

    1. function greet() { }
    2. void greet() { }
    3. void greet[] { }
    4. greet() { return void; }

    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.

  8. Default Parameter Values

    How do you provide a default value of 5 for an optional named parameter in a Dart function?

    1. void foo([int x: 5])
    2. void foo(int x = 5)
    3. void foo({int x? = 5})
    4. void foo({int x = 5})

    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.

  9. Map Access

    Given a map fruits = {'apple': 3, 'banana': 5}, how do you access the value associated with 'banana'?

    1. fruits.banana
    2. fruits['banana']
    3. fruits-u003Ebanana
    4. fruits{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.

  10. Type Inference

    Which Dart keyword allows the compiler to automatically determine a variable’s type based on assigned value?

    1. auto
    2. final
    3. const
    4. var

    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.