[ Quiz Answers | Chapter Index | Main Index ]

## Quiz on Chapter 10

This page contains questions on Chapter 10 of Introduction to Programming Using Java. You should be able to answer these questions after studying that chapter. Sample answers to these questions can be found here.

Question 1:

What is meant by generic programming and what is the alternative?

Question 2:

Why can't you make an object of type LinkedList<int>? What should you do instead?

Question 3:

What is an iterator and why are iterators necessary for generic programming?

Question 4:

Suppose that integers is a variable of type Collection<Integer>. Write a code segment that uses an iterator to compute the sum of all the integer values in the collection. Write a second code segment that does the same thing using a for-each loop.

Question 5:

Interfaces such as List, Set, and Map define abstract data types. Explain what this means.

Question 6:

What is the fundamental property that distinguishes Sets from other types of Collections?

Question 7:

What is the essential difference in functionality between a TreeMap and a HashMap?

Question 8:

Explain what is meant by a hash code.

Question 9:

Modify the following Date class so that it implements the interface Comparable<Date>. The ordering on objects of type Date should be the natural, chronological ordering.

    class Date {
       int month;  // Month number in range 1 to 12.
       int day;    // Day number in range 1 to 31.
       int year;   // Year number.
       Date(int m, int d, int y) {
          month = m;
          day = d;
          year = y;
       }
    }

Question 10:

Suppose that syllabus is a variable of type TreeMap<Date,String>, where Date is the class from the preceding exercise. Write a code segment that will write out the value string for every key that is in the month of December, 2018.

Question 11:

Write a generic class Stack<T> that can be used to represent stacks of objects of type T. The class should include methods push(), pop(), and isEmpty(). Inside the class, use an ArrayList to hold the items on the stack.

Question 12:

Write a generic method, using a generic type parameter <T>, that replaces every occurrence in an ArrayList<T> of a specified item with a specified replacement item. The list and the two items are parameters to the method. Both items are of type T. Take into account the fact that the item that is being replaced might be null. For a non-null item, use equals() to do the comparison.

Question 13:

Suppose that words is an array of Strings. Explain what is done by the following code:

    long n = Arrays.stream(words)
                   .filter( w -> (w != null) )
                   .map( w -> w.toLowerCase() )
                   .distinct()
                   .count();

Question 14:

Use the stream API to print all the even integers from 2 to 20. Start with IntStream.range and apply a filter operation.

Question 15:

Write a generic method countIf(c,t) with type parameter <T>, where the first parameter, c, is of type Collection<T>, and the second parameter, p, is of type Predicate<T>. The method should return the number of items in the collection for which the predicate is true. Give two versions, one using a loop and the other using the stream API.

[ Quiz Answers | Chapter Index | Main Index ]