Saturday, August 21, 2010


Courtesy of Sun Web Learning Center

1. import java.util.*;
2. public class Gen2 {
3.   public static void go(Set<Animal> a) { }
4.   public static void main(String [] args) {
5.     // insert code here
6.     go(t);
7.   }
8. }
9. class Animal { }
10. class Dog extends Animal { }

And the four statements:

s1.    TreeSet t = new TreeSet();
s2.    TreeSet<Dog> t = new TreeSet<Dog>();
s3.    TreeSet<Animal> t = new TreeSet<Dog>();
s4.    TreeSet<Animal> t = new TreeSet<Animal>();

Which, inserted independently at line 5, will compile?

A     only s1
B     only s4
C     only s1 and s2
D     only s1 and s4
E     only s1, s2, and s4
F     only s1, s3, and s4
G     All of the codes will compile.

D is correct. Codes s2 and s3 attempt to pass a TreeSet of Dogs into the go method that can only accept Sets of Animals.

No comments:

Post a Comment