MCQs on Advanced Collections | Dart

Take your Dart programming to the next level by mastering advanced collection techniques such as Iterables, Iterators, Spread Operators, and Null-Aware Operators to write more efficient and clean code.


MCQs on Advanced Collections in Dart

1. Iterables and Iterators

  1. Which of the following is true about Dart Iterable?
    a) An Iterable can be modified after creation
    b) An Iterable is an interface, and it supports forEach() and map() methods
    c) Iterable elements can only be integers
    d) Iterable stores elements in a fixed-size array
  2. What does the iterator property of an Iterable return?
    a) The first element of the iterable
    b) A List of all elements
    c) A ListIterator object to traverse the iterable
    d) An error if the iterable is empty
  3. Which method in Dart Iterable is used to check if an element exists?
    a) contains()
    b) has()
    c) exists()
    d) find()
  4. What does the forEach() method in Dart do?
    a) Loops through the iterable, applying a function to each element
    b) Adds an element to the iterable
    c) Removes an element from the iterable
    d) Sorts the iterable
  5. How does an iterator in Dart work?
    a) It provides a method to iterate over a collection
    b) It randomly accesses elements in a collection
    c) It allows sorting of the iterable elements
    d) It restricts access to iterable elements
  6. What happens when you call the moveNext() method of an iterator?
    a) It moves the iterator to the next element and returns true if successful
    b) It resets the iterator to the first element
    c) It skips every other element
    d) It returns false if the iterator is empty
  7. Which of the following collections does not implement the Iterable interface in Dart?
    a) List
    b) Set
    c) Map
    d) Queue
  8. What does the map() function in Dart return for an Iterable?
    a) A new Iterable with each element transformed
    b) The original Iterable
    c) A sorted version of the Iterable
    d) The first element of the Iterable
  9. In Dart, how do you create an iterable from a List?
    a) Iterable(list)
    b) List.from()
    c) list.toIterable()
    d) Iterable(list)
  10. Which Dart method can be used to filter elements in an Iterable?
    a) filter()
    b) where()
    c) map()
    d) find()

2. Collection if and for

  1. What is the syntax for conditionally adding elements to a collection in Dart?
    a) if inside a list literal
    b) else inside a list literal
    c) switch inside a list literal
    d) try-catch inside a list literal
  2. Which of the following is a valid example of collection if in Dart?
    a) [if (condition) value]
    b) {if (condition) value}
    c) [value if (condition)]
    d) [value else (condition)]
  3. How can you add multiple elements to a list based on conditions in Dart?
    a) Use a loop inside a collection literal
    b) Use addAll() after the list is created
    c) Use if and for conditions inside a list
    d) Use the set data structure
  4. What does the for loop in collection literals do in Dart?
    a) Iterates over an existing collection and adds elements to the new collection
    b) Modifies the existing collection
    c) Loops without creating a new collection
    d) Filters elements from a collection
  5. Which of the following is the correct syntax for using for inside a collection literal?
    a) [for (var item in collection) item]
    b) [for item in collection item]
    c) [foreach (var item in collection) item]
    d) {for (var item in collection) item}
  6. How do you ensure that an element is only added if a condition is met inside a Dart collection?
    a) Use if with the condition inside the collection literal
    b) Use when with the condition inside the collection literal
    c) Use unless with the condition inside the collection literal
    d) Use forEach with the condition inside
  7. Which method is used to combine multiple conditions within a collection literal in Dart?
    a) &&
    b) ||
    c) if-else
    d) for-while
  8. How can you use the if condition inside a map literal?
    a) Map{if(condition) key: value}
    b) Map{key if condition: value}
    c) {key: value if condition}
    d) {key: value, if(condition) key: value}
  9. What happens if the if condition inside a collection literal evaluates to false?
    a) The element is not added to the collection
    b) An exception is thrown
    c) The element is added but with a null value
    d) The collection is empty
  10. What is the correct syntax to use for inside a Dart set literal?
    a) {for (var item in collection) item}
    b) {foreach (var item in collection) item}
    c) [for (var item in collection) item]
    d) {forEach (var item in collection) item}

3. Spread Operators (…, …?)

  1. What does the spread operator ... do in Dart?
    a) Expands elements from one collection into another collection
    b) Combines two collections into one
    c) Removes elements from a collection
    d) Filters elements from a collection
  2. What is the purpose of the ...? operator in Dart?
    a) It spreads elements from a collection and handles null values gracefully
    b) It filters out null values from a collection
    c) It combines multiple collections into one
    d) It performs deep copy operations on collections
  3. How do you add elements from a list to a set using spread operators in Dart?
    a) {...list}
    b) list.add({...set})
    c) set.add(...list)
    d) {set.addAll(...list)}
  4. Which of the following will result in a null exception when using the spread operator?
    a) {...null}
    b) {...list}
    c) {...set}
    d) {...map}
  5. How can you merge two lists using spread operators?
    a) [...list1, ...list2]
    b) [list1 + list2]
    c) [list1.concat(list2)]
    d) list1.addAll(list2)
  6. What does the spread operator do when placed inside a collection literal?
    a) It inserts the elements of one collection into another
    b) It filters duplicate elements
    c) It sorts elements
    d) It creates a new collection with null values
  7. How can you add a collection of items to a map using spread operators?
    a) {...map, key: value}
    b) {map.addAll(...list)}
    c) map.add(...list)
    d) {key: value, ...otherMap}
  8. Which operator is used for conditional spreading in Dart?
    a) ...?
    b) ...?
    c) ?...
    d) ...&&
  9. What does the ... operator require in Dart?
    a) An iterable collection
    b) A numeric value
    c) A map
    d) A fixed-length list
  10. How do you handle null values when using the spread operator in Dart?
    a) Use ...? to avoid exceptions for null collections
    b) Use ...? to spread null values
    c) Use ... to ignore null values
    d) Use null as a spread operator

Answers

QnoAnswer
1b) An Iterable is an interface, and it supports forEach() and map() methods
2c) A ListIterator object to traverse the iterable
3a) contains()
4a) Loops through the iterable, applying a function to each element
5a) It provides a method to iterate over a collection
6a) It moves the iterator to the next element and returns true if successful
7c) Map
8a) A new Iterable with each element transformed
9c) list.toIterable()
10b) where()
11a) if inside a list literal
12a) [if (condition) value]
13c) Use if and for conditions inside a list
14a) Iterates over an existing collection and adds elements to the new collection
15a) [for (var item in collection) item]
16a) Use if with the condition inside the collection literal
17a) &&
18d) {key: value, if(condition) key: value}
19a) The element is not added to the collection
20a) {for (var item in collection) item}
21a) Expands elements from one collection into another collection
22a) It spreads elements from a collection and handles null values gracefully
23a) {...list}
24a) {...null}
25a) [...list1, ...list2]
26a) It inserts the elements of one collection into another
27d) {key: value, ...otherMap}
28a) ...?
29a) An iterable collection
30a) Use ...? to avoid exceptions for null collections

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top