C212 WRITTEN TEST UPDATED ACTUAL
QUESTIONS AND CORRECT ANSWERS
⩥ Define: list.
Answer: A list is a collection that remembers the order of
elements.
⩥ Define: set.
Answer: A set is an unordered collection of unique elements.
⩥ Define: map.
Answer: A maps keeps associations between key and value
objects.
⩥ Define: linked list..
Answer: Data structure used for collecting a sequence of objects
that allows efficient addition and removal of elements in the
middle of the sequence.
⩥ Define: list iterator..
,Answer: An iterator encapsulates a position anywhere inside the
linked list.
⩥ Do linked lists take more storage space than arrays of the
same size? Explain..
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.
⩥ Why don't we need iterators with arrays?.
Answer: We can simply access each array element with an
integer index.
⩥ 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.
⩥ 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(); }
}
⩥ Write a loop that prints every second element of a linked list
of strings called words. (Answer)..
Answer: ListIterator<String> iter = words.iterator();
int count = 0;
while(iter.hasNext()){
count++;
if (count %. 2 == 0){
System.out.println(iter.next());
}
}
⩥ Can you add an element to a set at an iterator position?
Explain why or why not..
, Answer: 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.
⩥ 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 and removing elements as well as testing for
membership is more efficient with sets.
⩥ Why are set iterators different from list iterators?.
Answer: You do not know, nor can you control, in which order
the set keeps the elements.
⩥ 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);
}
}
QUESTIONS AND CORRECT ANSWERS
⩥ Define: list.
Answer: A list is a collection that remembers the order of
elements.
⩥ Define: set.
Answer: A set is an unordered collection of unique elements.
⩥ Define: map.
Answer: A maps keeps associations between key and value
objects.
⩥ Define: linked list..
Answer: Data structure used for collecting a sequence of objects
that allows efficient addition and removal of elements in the
middle of the sequence.
⩥ Define: list iterator..
,Answer: An iterator encapsulates a position anywhere inside the
linked list.
⩥ Do linked lists take more storage space than arrays of the
same size? Explain..
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.
⩥ Why don't we need iterators with arrays?.
Answer: We can simply access each array element with an
integer index.
⩥ 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.
⩥ 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(); }
}
⩥ Write a loop that prints every second element of a linked list
of strings called words. (Answer)..
Answer: ListIterator<String> iter = words.iterator();
int count = 0;
while(iter.hasNext()){
count++;
if (count %. 2 == 0){
System.out.println(iter.next());
}
}
⩥ Can you add an element to a set at an iterator position?
Explain why or why not..
, Answer: 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.
⩥ 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 and removing elements as well as testing for
membership is more efficient with sets.
⩥ Why are set iterators different from list iterators?.
Answer: You do not know, nor can you control, in which order
the set keeps the elements.
⩥ 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);
}
}