Comprog Midterm Reviewer
Comprog Midterm Reviewer
In Dart, what keyword allows you to declare a variable capable of holding different data
types?
2. How do you declare an empty list in Dart?
3. Describe the symbol used in Dart to access elements within a Map.
4. What result does the expression print(5 ~/ 2) produce in Dart?
5. In Dart, how would you declare a constant integer variable with the value 10?
6. If you create a list with [1, 2, 3], add an element 4 to it, and check its length, what
would the length be?
7. Explain the purpose of using the ? symbol when declaring a variable in Dart.
8. What happens if you try to change a final variable after it has been assigned a value?
9. What kind of error occurs if you declare var x = 5 and later try to assign a string to x?
10. Why might you choose to use a Map instead of a List in Dart?
11. How would you iterate over all key-value pairs in a Dart Map?
12. Describe how you would add multiple elements to a Dart List in one operation.
13. How can you check if a specific key exists within a Map in Dart?
14. What is the best way to create a nested list with 3 rows and 4 columns in Dart?
15. If you have a nested list, how would you access the second element of the third sub-list?
16. Identify the error in the following code: Map<String, int> scores = {'John':
85, 'Mary': '90', 'Bob': 78};
17. In nested loops, what happens with the outer loop when the inner loop completes all its
iterations?
18. Given a nested loop where the outer loop iterates twice and the inner loop iterates three
times, list the output pattern if each loop's variable is printed.
19. In a nested Map structure, what is the effect of using an incorrect key type?
20. If you assign a list to a new variable, then add an element to the new variable, what will
happen to the original list?
21. What loop structure would be most effective for iterating through each key-value pair in a
Map?
22. When choosing between nested Lists and nested Maps in Dart, what factors should you
consider?
23. What’s a recommended practice for handling null values within Maps?
24. Which control structure would be the most suitable for validating user input that requires
different conditions?
25. If you needed to implement a basic caching system, what data structure would you
choose?
26. Describe how you could implement a queue using a Dart List.
27. To perform matrix multiplication in Dart, what kind of loop structure would you use?
28. How can you convert the values of a Map to a List in Dart?
29. What would be the result of running this code in Dart?
33. How would you remove all elements from a List that meet a specific condition?
34. In Dart, what is the correct approach to combine the contents of two Maps?
35. Given a list [1, 2, 3], if you want to create a new list where each element is doubled,
how would you do that?
36. What is a safe way to access values in a nested Map structure in Dart?
37. If you have a list of numbers, and you want to print only the even numbers, how would
you accomplish this?
38. Which loop structure would be most efficient for transforming each element in a List?
39. In Dart, what’s the recommended method for handling exceptions?
2. Collections
Lists
- Declaration
List<int> numbers = [1, 2, 3];
var names = ['John', 'Jane'];
- Common Operations
- `add()`: Add single element
- `addAll()`: Add multiple elements
- `removeAt()`: Remove at index
- `remove()`: Remove specific element
- `length`: Get size
- `contains()`: Check existence
Maps
- Declaration
Map<String, int> scores = {
'John': 90,
'Jane': 95
};
- Common Operations
- Access: `map['key']`
- Check key: `containsKey()`
- Add/Update: `map['key'] = value`
- Remove: `remove('key')`
- For-in Loop
for (var item in list) {
print(item);
}
- forEach
list.forEach((item) => print(item));
Conditionals
if (condition) {
// code
} else if (otherCondition) {
// code
} else {
// code
}
5. Null Safety
- Null Safety Operators
- `?`: Nullable type
- `!`: Null assertion
- `??`: Null coalescing
- `?.`: Safe navigation
6. Error Handling
try {
// Code that might throw error
} catch (e) {
// Handle error
} finally {
// Always executed
}
2. Collection Efficiency
- Lists for ordered sequences
- Maps for key-value pairs
- Choose based on access patterns
3. Best Practices
- Use null safety features
- Handle errors properly
- Follow modular programming principles
- Use appropriate data structures
1. List Transformations
// Map transformation
var numbers = [1, 2, 3];
var doubled = numbers.map((x) => x * 2).toList();
// Filter
var evens = numbers.where((x) => x.isEven).toList();
2. Map Operations
// Merging maps
var map1 = {'a': 1};
var map2 = {'b': 2};
var merged = Map.from(map1)..addAll(map2);
// Safe access
var value = map['key']?['nested'];
3. Loop Patterns
// Nested loops
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
print('$i$j');
}
}