Dart Online Quiz

Welcome to our Dart Online Quiz, designed to challenge and enhance your knowledge of Dart, a client-optimized language for fast apps on any platform. Developed by Google, Dart is often used for building web, server, and mobile applications, especially popular in the Flutter framework for cross-platform development. This quiz contains questions ranging from basic syntax and data types to advanced concepts such as async programming and mixins. Whether you are a beginner or an advanced developer, this quiz will help you gauge and expand your Dart programming skills.

1. What does the 'final' keyword do in Dart?

a) It makes a variable constant after it is set for the first time
b) It declares a variable that must be initialized immediately
c) It indicates that a class cannot be extended
d) It prevents a variable from changing type

2. How do you create a list in Dart?

List<String> names = ['Alice', 'Bob', 'Charlie'];
a) var names = List['Alice', 'Bob', 'Charlie'];
b) List<String> names = ['Alice', 'Bob', 'Charlie'];
c) List names = new List('Alice', 'Bob', 'Charlie');
d) String[] names = {'Alice', 'Bob', 'Charlie'};

3. What is the output of the following Dart code?

void main() {
    int a = 10;
    double b = a as double;
    print(b);
}
a) 10
b) 10.0
c) Error
d) None

4. Which of these is a valid way to declare a function in Dart that returns no value?

a) function myFunction() {}
b) void myFunction() {}
c) func myFunction() -> void {}
d) None of the above

5. What is the purpose of the 'await' keyword in Dart?

Future<String> fetchUsername() {
    return Future.delayed(Duration(seconds: 1), () => 'Future User');
}
void main() async {
    print(await fetchUsername());
}
a) To pause function execution until a Future completes
b) To execute a Future
c) To declare an asynchronous function
d) To prevent a function from returning

6. How do you define a map in Dart?

a) var map = map{'key1': 'value1', 'key2': 'value2'};
b) Map map = {'key1': 'value1', 'key2': 'value2'};
c) Map<String, String> map = new Map('key1': 'value1', 'key2': 'value2');
d) Map<String, String> map = {'key1': 'value1', 'key2': 'value2'};

7. What does the following Dart code output?

void main() {
    var numbers = [1, 2, 3];
    numbers.add(4);
    print(numbers.length);
}
a) 3
b) 4
c) Error
d) None of the above

8. How do you declare a constructor in a Dart class?

class Person {
    String name;
    Person(this.name);
}
a) class Person { String name; Person(String this.name); }
b) class Person { String name; Person(name) { this.name = name; } }
c) class Person { String name; Person(String name) { this.name = name; } }
d) class Person { String name; Person(this.name); }

9. What is Dart's null safety feature?

a) It ensures that variables are always non-null
b) It requires developers to initialize all variables
c) It prevents null reference errors by requiring developers to declare whether a variable can be null
d) It automatically assigns a default value to null variables

10. What is the main purpose of the pubspec.yaml file in a Dart project?

a) To define the project's metadata and dependencies
b) To store the application's source code
c) To configure project settings and preferences
d) To list the project's contributors

11. How do you declare a list in Dart?

List<int> numbers = [1, 2, 3];
a) List<int> numbers = [1, 2, 3];
b) int[] numbers = {1, 2, 3};
c) array<int> numbers = [1, 2, 3];
d) List numbers = new List(1, 2, 3);

12. What is the output of the following Dart code?

void main() {
    var x = 10;
    var result = x > 5 ? 'Yes' : 'No';
    print(result);
}
a) Yes
b) No
c) true
d) false

13. Which function is used to parse a string into an integer in Dart?

a) parseInt()
b) int.parse()
c) String.toInt()
d) parse.Int()

14. What does the following Dart code snippet do?

Future<String> fetchData() {
    return Future.delayed(Duration(seconds: 5), () => 'Data Loaded');
}
void main() async {
    print(await fetchData());
}
a) Prints "Data Loaded" after a 5-second delay
b) Immediately prints "Data Loaded"
c) Causes a runtime error
d) None of the above

15. How do you create a constant map in Dart?

a) const map = {'id': 1, 'name': 'Alice'};
b) Map map = const {'id': 1, 'name': 'Alice'};
c) final map = {'id': 1, 'name': 'Alice'};
d) Map map = new Map('id': 1, 'name': 'Alice');

16. What will the following Dart code output?

void main() {
    for (var i = 0; i < 3; i++) {
        print(i);
    }
}
a) 0 1 2
b) 0 1 2 3
c) 1 2 3
d) None of the above

17. Which of the following is a valid way to define a function in Dart that does not return a value?

a) function myFunction() {}
b) void myFunction() {}
c) None myFunction() {}
d) Nil myFunction() {}

18. What is Dart's type inference feature?

a) Automatically converting types
b) The ability to infer types based on values
c) A method to check the type of variables
d) A feature that allows dynamic type assignment

119. What does the async keyword do in Dart?

a) It makes a function return a new thread
b) It allows a function to run synchronously
c) It defines a function that executes asynchronously
d) It pauses the execution of a function

20. What is the default value of a boolean in Dart if it is declared but not initialized?

a) true
b) false
c) null
d) 0

21. What will the following Dart code output?

void main() {
    int? a;
    print(a?.isEven);
}
a) true
b) false
c) null
d) Error

22. How do you check if a Dart list contains a specific element?

a) list.contains(element)
b) list.has(element)
c) list.find(element)
d) list.exists(element)

23. What is the correct way to declare a named constructor in Dart?

a) ClassName.constructorName() {}
b) ClassName : constructorName() {}
c) ClassName - constructorName() {}
d) ClassName { constructorName() }

24. How do you convert an integer to a double in Dart?

a) Using the toDouble() method
b) Using the double() constructor
c) Using the asDouble() function
d) Using the parseDouble() method

25. How do you create a constant variable in Dart?

const double pi = 3.14159;
a) const double pi = 3.14159;
b) final double pi = 3.14159;
c) var pi = const double(3.14159);
d) static double pi = 3.14159;

Comments