0% found this document useful (0 votes)
64 views

Tutorial 01-02-2013 Exercises

This document provides questions and exercises on the Java Collections Framework and Generics to help students understand these features for a software workshop worksheet. It asks questions about the differences between Map and HashMap, Vector and ArrayList, List and Set. It also asks about Iterators, traversing lists, generics terminology, using primitives as type arguments, autoboxing/unboxing, sample code examples, and provides a link for more information on the Java Collections Framework, Maps and Generics.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Tutorial 01-02-2013 Exercises

This document provides questions and exercises on the Java Collections Framework and Generics to help students understand these features for a software workshop worksheet. It asks questions about the differences between Map and HashMap, Vector and ArrayList, List and Set. It also asks about Iterators, traversing lists, generics terminology, using primitives as type arguments, autoboxing/unboxing, sample code examples, and provides a link for more information on the Java Collections Framework, Maps and Generics.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

06-06994/21699 Software Workshop

Spring Semester 2012-13


Tutorial 01/02/2013

The University of Birmingham


School of Computer Science
Apostolos Giannakidis

Java Collections and Generics


Worksheet 3 requires the use of Collections and Generics. Following are some questions
and exercises on the Java Collections Framework (JCF) and Generics to help you
understand these features.

1.
2.
3.
4.
5.

What is the difference between Map and HashMap?


What is the difference between Vector and ArrayList?
What is the difference between List and Set?
What is an Iterator?
What are the three ways to traverse a List and what are the limitations of each
one?
6. What do the terms formal type parameter (or type parameter),
actual type parameter (or type argument), parameterized type,
generic type and generic type invocation mean?
7. Can a primitive type be used as a type argument?
a. What does the term boxed primitive type mean?
b. What is autoboxing and unboxing?

8. Consider the following code:


List myList = new ArrayList();
myList.add(one);
myList.add(two);
myList.add(10);
for (String s : myList)
System.out.print(s);
What is the result of the above code?
Hint: Are we using generics?

9. Consider the following code:


Set<Integer> numbers = new HashSet<Integer>();
numbers.add(new Integer(80));
numbers.add(70);
numbers.add(new Integer(80));
numbers.add(null);
Iterator iter = numbers.iterator();
while (iter.hasNext())
System.out.print(iter.next());
What is the result of the above code?

10. Write a generic method to swap of two different elements in an array. The array
may hold any type of elements. Create also a sample main method to call your
method.

Read more on the Java Collections Framework, Maps and Generics:


http://www.cs.bham.ac.uk/internal/courses/java/msc/resources/tutorialHandouts/collectionsAndGenerics.pdf

You might also like