Exercises On Collections
Exercises On Collections
Java Collections
Exercise
Statement # 1: Few problem solutions have been provided for associates should analyze the
program and write down the program output. This will enhance the analyzing skills of associates
and also understand “why” part of java programming feature. The associates can then try
running the program in eclipse and check if the output with what they have written.
Stamen # 2: There are some problem statements provided similar to the final assessment and
associates need to solve it. This will enhance the programming skills of the associates.
IMPORTANT: These exercises will gear you up for the core java assessment so please
develop/analyze the exercise independently. In case you are stuck up reach out to the trainers.
Exercises:
1. Declare suitable collection at the position //insert code here
class CollectionTypes {
x.add(“one”);
x.add(“two”);
x.add(“one”);
System.out.println(x.poll());
2
Java Collections Exercise 2012
2. What is the result of compiling and running the following program?
import java.util.*;
list.add("one");
list.add(2);
System.out.println(list.get(0).length(); }
}}
3
Java Collections Exercise 2012
4. What is the result of attempting to compile and run the following code?
5. Create a class with a method which can remove all the elements from a list other than the
collection of elements specified.
6. Create a class that can accept an array of String objects and return them as a sorted List
4
Java Collections Exercise 2012
7. Create a method that returns collection that contain only unique String object in the sorted order.
8. Create a class which accepts a HashMap and returns the keys in the Map
9. Create a method that returns the current date in the format specified
10. Create a method that calculates the age of a person based on his date of birth