Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Document preview thumbnail
Preview 4 out of 51 pages
Exam (elaborations)

C212 WRITTEN TEST TEST PAPER FULL QUESTIONS AND COMPLETE ANSWERS

Document preview thumbnail
Preview 4 out of 51 pages

C212 WRITTEN TEST TEST PAPER FULL QUESTIONS AND COMPLETE ANSWERS

Content preview

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

Document information

Uploaded on
June 9, 2026
Number of pages
51
Written in
2025/2026
Type
Exam (elaborations)
Contains
Questions & answers
$13.99

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
TopGradeInsider
4.1
(9)
Sold
130
Followers
2
Items
46228
Last sold
1 day ago


Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions