Perfect Developer basic tutorial 3 This page last modified 2011-10-29 (JAC)

Collection Classes Quiz

1. What is the difference between a set and a bag?

Duplicate values are allowed in a bag, but not in a set.


2. Sets are considered to be simpler than bags, and bags are simpler than sequences. What is the simplest data type you could use to represent each of the following?

Hint: only use a sequence when it is important to specify the order of the values in the collection.
  1. All the prime numbers between 1 and 1000
  2. The marks obtains by a class of 30 students, anonymized (i.e. you wish to store just the marks obtained, not the names of the students), where the marks are integers
  3. The names of all the students in a class
  4. A shopping list (assume that each item in the list can be represented by a value of type string)
  5. The same shopping list, but arranged in the order in which the items appear on the shelves of the store
  1. set of int
  2. bag of int
  3. set of string if we assume that no two students have the same name (so if we get two students called "John Smith" then we would have to call them e.g. "John 1 Smith" and "John 2 Smith"). Otherwise, bag of string.
  4. set of string assuming that we never write the same item down two or more times, otherwise bag of string.
  5. seq of string because the order is important.