MCQs on Collections Basics | Dart

Dive into Dart collections by mastering the basics of Lists, Sets, and Maps. Learn essential operations, properties, and practical use cases to enhance your programming skills in Dart.


MCQs on Collections Basics in Dart

1. Lists and Their Operations

  1. How do you create a fixed-length list in Dart?
    a) List.filled()
    b) List.fixed()
    c) List.generate()
    d) List.new()
  2. Which of the following methods is used to add a single element to a list?
    a) add()
    b) insert()
    c) addAll()
    d) append()
  3. What does the removeAt(index) method do in a list?
    a) Removes an element at a specific index
    b) Removes all elements equal to the given value
    c) Replaces an element at the specified index
    d) Removes the last element
  4. How do you create an empty growable list in Dart?
    a) List()
    b) []
    c) List.empty(growable: true)
    d) new List()
  5. What does the insert(index, element) method do?
    a) Inserts an element at the specified index
    b) Replaces the element at the specified index
    c) Removes the element at the specified index
    d) Clears the entire list
  6. Which method checks if a list is empty?
    a) isEmpty()
    b) length == 0
    c) list == null
    d) Both a and b
  7. How can you concatenate two lists in Dart?
    a) list1.addAll(list2)
    b) list1 + list2
    c) list1.concat(list2)
    d) list1.union(list2)
  8. What does the indexOf(element) method return if the element is not found in a list?
    a) -1
    b) 0
    c) null
    d) An error
  9. How do you reverse the elements of a list in Dart?
    a) list.reversed
    b) list.reverse()
    c) reverse(list)
    d) list[::-1]
  10. Which method retrieves the last element of a list in Dart?
    a) last
    b) getLast()
    c) tail()
    d) pop()

2. Sets and Their Properties

  1. What is the key property of a Set in Dart?
    a) Elements must be unique
    b) Elements are sorted
    c) Elements must be integers
    d) Sets are immutable
  2. How do you create a Set in Dart?
    a) Set()
    b) {}
    c) new Set()
    d) Both a and c
  3. What method checks if a set contains an element?
    a) contains()
    b) has()
    c) exists()
    d) in()
  4. Which operation removes all elements of one set that are present in another?
    a) difference()
    b) intersection()
    c) removeAll()
    d) subtract()
  5. What does the union() method do in a set?
    a) Combines all elements from two sets
    b) Finds common elements between two sets
    c) Removes duplicate elements
    d) Sorts the elements in the set
  6. Which method is used to check if one set is a subset of another?
    a) containsAll()
    b) isSubsetOf()
    c) subset()
    d) isSuperset()
  7. How do you add an element to a set?
    a) add()
    b) append()
    c) insert()
    d) put()
  8. What happens if you try to add a duplicate element to a set in Dart?
    a) It is ignored
    b) An error occurs
    c) The duplicate is added
    d) The set is cleared
  9. Which method removes all elements from a set?
    a) clear()
    b) removeAll()
    c) delete()
    d) reset()
  10. What is the default type of a Dart set?
    a) Set<dynamic>
    b) Set<Object>
    c) Set<int>
    d) Set<String>

3. Maps and Key-Value Pairs

  1. How do you create an empty map in Dart?
    a) {}
    b) Map()
    c) new Map()
    d) Both b and c
  2. What method retrieves a value for a specific key in a map?
    a) map[key]
    b) get(key)
    c) value(key)
    d) find(key)
  3. How do you add a new key-value pair to a map?
    a) map[key] = value
    b) add(key, value)
    c) put(key, value)
    d) insert(key, value)
  4. Which method checks if a map contains a specific key?
    a) containsKey()
    b) keyExists()
    c) hasKey()
    d) inKeys()
  5. What happens when you try to access a key that doesn’t exist in a map?
    a) Returns null
    b) Throws an error
    c) Adds the key with a default value
    d) Removes all existing keys
  6. How do you remove a key-value pair from a map?
    a) remove(key)
    b) delete(key)
    c) unset(key)
    d) clear(key)
  7. What does the keys property of a map return?
    a) All keys in the map
    b) All values in the map
    c) Key-value pairs as a list
    d) The first key in the map
  8. What method combines two maps into one?
    a) addAll()
    b) merge()
    c) combine()
    d) concat()
  9. How do you iterate over all key-value pairs in a map?
    a) forEach()
    b) map()
    c) each()
    d) keys.forEach()
  10. What does the values property of a map return?
    a) All values in the map
    b) All keys in the map
    c) Key-value pairs as a list
    d) The first value in the map

Answers

QnoAnswer
1a) List.filled()
2a) add()
3a) Removes an element at a specific index
4c) List.empty(growable: true)
5a) Inserts an element at the specified index
6d) Both a and b
7a) list1.addAll(list2)
8a) -1
9a) list.reversed
10a) last
11a) Elements must be unique
12d) Both a and c
13a) contains()
14a) difference()
15a) Combines all elements from two sets
16a) containsAll()
17a) add()
18a) It is ignored
19a) clear()
20a) Set<dynamic>
21d) Both b and c
22a) map[key]
23a) map[key] = value
24a) containsKey()
25a) Returns null
26a) remove(key)
27a) All keys in the map
28a) addAll()
29a) forEach()
30a) All values in the map

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