C212 WRITTEN TEST TEST PAPER FULL
QUESTIONS AND COMPLETE ANSWERS
⩥ Define: set.
Answer: an unordered collection of unique elements
⩥ Define: map.
Answer: keeps associations between key and value objects
⩥ Define: linked list.
Answer: consists of a number of nodes, each of which has a
reference to the next node
⩥ Define: list iterator.
Answer: use a list iterator to access elements inside a linked list
⩥ 4. Do linked lists take more storage space than arrays of the
same size.
Answer: Yes, for two reasons. A linked list needs to store the
neighboring node references, which are not needed in an array.
Moreover, there is some overhead for storing an object. In a
,linked list, each node is a separate object that incurs this
overhead, whereas an array is a single object.
⩥ 5. Why don't we need iterators with arrays.
Answer: We can simply access each array element with an
integer index.
⩥ 6. Consider the types HashSet and TreeSet. Do they have
anything in common.
Answer: They both implement the Set interface. (Set
implementations arrange the elements so that they can locate
them quickly.)
⩥ 7. Write a loop that removes all strings with length less than
four from a linked list of strings called words.
Answer: ListIterator<String> iter = words.iterator();
while (iter.hasNext())
{
String str = iter.next();
if (str.length() < 4)
{ iter.remove(); }
}
,⩥ 8. Write a loop that prints every second element of a linked
list of strings called words.
Answer: ListIterator<String> iter = words.iterator();
while (iter.hasNext())
{
System.out.println(iter.next());
if (iter.hasNext ())
{ iter.next(); } // skip next element
}
⩥ 9. Can you add an element to a set at an iterator position.
Answer: The Iterator interface can not add an element at the
iterator position. It makes no sense to add an element at a
particular position in a set, because the set can order the
elements any way it likes. (A set iterator visits the elements in
the order in which the set implementation keeps them. This is
not necessarily the order in which you inserted them. The order
of elements in a hash set seems quite random because the hash
code spreads the elements into different groups. When you visit
elements in a tree set, they always appear in sorted order, even if
you inserted them in a different order.)
, ⩥ 10. Arrays and lists remember the order in which you added
elements, sets do not. Why would you want to use a set instead
of an array or list?.
Answer: Adding & removing elements as well as testing for
membership is more efficient with sets.
⩥ 11. Why are set iterators different from list iterators.
Answer: Sets do not have an ordering, so it doesn't make sense
to add an element at a particular iterator position, or to traverse a
set backwards.
⩥ 12. Write a loop that prints all elements that are in both
Set<String> s and Set<String> t..
Answer: for (String str :s)
{ if (t.contains(str))
{ System.out.println(str); }
}
⩥ 13. How do you find all keys and values in a map.
Answer: iterate through the key set and find the values that
correspond to the keys
QUESTIONS AND COMPLETE ANSWERS
⩥ Define: set.
Answer: an unordered collection of unique elements
⩥ Define: map.
Answer: keeps associations between key and value objects
⩥ Define: linked list.
Answer: consists of a number of nodes, each of which has a
reference to the next node
⩥ Define: list iterator.
Answer: use a list iterator to access elements inside a linked list
⩥ 4. Do linked lists take more storage space than arrays of the
same size.
Answer: Yes, for two reasons. A linked list needs to store the
neighboring node references, which are not needed in an array.
Moreover, there is some overhead for storing an object. In a
,linked list, each node is a separate object that incurs this
overhead, whereas an array is a single object.
⩥ 5. Why don't we need iterators with arrays.
Answer: We can simply access each array element with an
integer index.
⩥ 6. Consider the types HashSet and TreeSet. Do they have
anything in common.
Answer: They both implement the Set interface. (Set
implementations arrange the elements so that they can locate
them quickly.)
⩥ 7. Write a loop that removes all strings with length less than
four from a linked list of strings called words.
Answer: ListIterator<String> iter = words.iterator();
while (iter.hasNext())
{
String str = iter.next();
if (str.length() < 4)
{ iter.remove(); }
}
,⩥ 8. Write a loop that prints every second element of a linked
list of strings called words.
Answer: ListIterator<String> iter = words.iterator();
while (iter.hasNext())
{
System.out.println(iter.next());
if (iter.hasNext ())
{ iter.next(); } // skip next element
}
⩥ 9. Can you add an element to a set at an iterator position.
Answer: The Iterator interface can not add an element at the
iterator position. It makes no sense to add an element at a
particular position in a set, because the set can order the
elements any way it likes. (A set iterator visits the elements in
the order in which the set implementation keeps them. This is
not necessarily the order in which you inserted them. The order
of elements in a hash set seems quite random because the hash
code spreads the elements into different groups. When you visit
elements in a tree set, they always appear in sorted order, even if
you inserted them in a different order.)
, ⩥ 10. Arrays and lists remember the order in which you added
elements, sets do not. Why would you want to use a set instead
of an array or list?.
Answer: Adding & removing elements as well as testing for
membership is more efficient with sets.
⩥ 11. Why are set iterators different from list iterators.
Answer: Sets do not have an ordering, so it doesn't make sense
to add an element at a particular iterator position, or to traverse a
set backwards.
⩥ 12. Write a loop that prints all elements that are in both
Set<String> s and Set<String> t..
Answer: for (String str :s)
{ if (t.contains(str))
{ System.out.println(str); }
}
⩥ 13. How do you find all keys and values in a map.
Answer: iterate through the key set and find the values that
correspond to the keys