Download Java Programming

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Java Programming: Final Examination
姓名:______________
學號:__Solution__________
6/24 2016
分數:______________
I (50 pts) 是非題 OXXXO XOOXX XOOOO OXOOX OOOXO
1.
If A is a concrete subclass of an abstract class B which implements an interface C, then A must provide
implementation of all methods declared in C which were not implemented in B.
2.
We can declare some instance variables in an interface.
3.
If A is an abstract class with constructor A(), then we can create an instance of it with the expression: new A().
4.
The text: public void m(){} is a legal declaration of a public void abstract method m() without parameters,
5.
An abstract class may override a concrete method of its super class with an abstract one.
6.
The text: Set<long> x = new Set<>(); is a legal java statement.
7.
In Java we have the rule that for all types A and B, if A is a subtype of B, then A[] is a subtype of B[].
8.
The text: Object o = new int[10]; is a legal java statement.
9.
If A is a class with a public method m(), we can override m() with a new protected m() in a subclass of A.
10. We can change the content of a java.lang.String instance after it has been created.
11. Suppose the method m has the head: int m( List<Number> x). We call it using m(new ArrayList<Double>() ).
12. Suppose the method m has the head: int m( List<? super Number> x). Then we can call it using m( new
ArrayList<Object>() ).
13. TreeSet<Integer> is a subtype of Set<? extends Number>.
14. For a method with the head: int m(List<?> x), we can call it using m( new ArrayList<Integer>() ).
15. Every java class but Object has exactly one parent class.
16. If A is a concrete class which implements an interface C, then it must implement also all super interfaces of C.
17. Like class, every Java interface can extend at most one interface.
18. In java, a class can also contain other classes as members.
19. We can use the enhanced for-loop of the form: "for(Type t : c){… }" for every instance c of a class which
implements Collection<Type>.
20. Every instance of the generic class LinkedList<E>, such as LinkedList<String> and LinkedList<Integer> will be
complied into a different bytecode.
21. If t is an instance of java class A and A implements interface C, then t is also an instance of C.
22. LinkedList is a more rational choice among List implementations than ArrayList if we need frequent insertion or
deletion operations on a list.
23. A non-static java member class cannot contain static nested classes.
24. int[] is a subtype of Number[].
25. Instances of RuntimeException are unchecked exceptions.
1
II (45 pts) 單選題 abcbd cdcae dcbcc
26. Suppose lst is an instance of List<Double>, then which of the following casting can be used in java without causing
warning/errors?
(a). (List<?>) lst
(b) (List<Object>) lst
(c) (List<? extends Number>) lst
(d) (List<? super Double>) lst
27. To declare an interface named A with two type parameters, use
(a) public interface A<E> { ... }
(b) public interface A<E, F> { … }
(c) public interface A(E) { ... }
(d) public interface A(E, F) { ... }
28. What is the output of the following program?
class Test { public static void main(String[] args) {
try { System.out.print("A");
System.out.print("B");
int i = 0;
int y = 2 / i;
} finally {
System.out.print("C");
}}}
(a). "A", then an error message.
(b)
"AC", then an error message.
(c)
(d)
An error message only.
"ABC", then an error message.
29. Polymorphism means that ________.
(a) a class can contain another class
(b) subtype objects can be used in place of supertype objects.
(c) data fields should be declared private
(d) a class can extend another class
30. Which of the following statements will not throw an exception?
(a). int
x=
(new int[3]) [4];
(c) double d = 10 / 0;
(b) char
c = “abcd”.charAt(-1);
(d)
x=
int
Integer.MAX_VALUE + Integer.MAX_VALUE ;
31. Suppose A is an abstract class, B is a concrete subclass of A, C is a subclass of B, and both A and B have a default
constructor. Which of the following statement will not cause error?
(a). A a = new A();
(b) B b = new A();
(c) A a =
new B();
(d)
C c = new B();
32. Which of the following is a checked exception?
(a). java.lang.IllegalArgumentException
(b)
java.lang.RuntimeException
(c)
(d)
java.lang.Exception
java.lang.ArithmeticException
33. Which of the following classes are NOT immutable?
(a). Double
(b) String (c) StringBuffer (d) BigInteger
34. Which of the following java types in java.util package is NOT an interface ?
(a) Hashtable
(b) Queue
(c) Iterator
(d) Set
(e) Map
35. Which of the following types is not a subtype of java.util.Collection ?
(a) Queue
(b) List
(c) Deque
(d) Set
(e) Map
36. Which of the following code can be put inside a generic class C<T> declaration without causing error?
(a)
T[] ts = new T[10] ;
(b) List<T>[] lst = new ArrayList<>[10];
(c)
T t = new T();
(d) List<T>[] lst = (List<T>[]) new ArrayList<?>[10]
37. Which of the following method would cause compile or runtime error if we put the code:" E e = (E) lst.get(0); " into
2
their bodies?
(a) public <E> void m0(List<E> lst) {… }
(b) public <E> void m1(List<? extends E> lst) {… }
(c) public <E> void m2(List<? super E> lst) {… }
(d) public <E> void m3(List lst) {… }
38. Which of the following method would cause compile or runtime error if we put the code:" lst.add(e); " into their
bodies?
(a) public <E> void m0(List<E> lst, E e) {… }
(b) public <E> void m1(List<? extends E> lst, E e) {… }
(c) public <E> void m2(List<? super E> lst, E e) {… }
(d) public <E> void m3(List lst,E e) {… }
39. Which of the following statement has no compile error?
(a) Map<Long,Long> m = new Map<>();
(b) HashMap<Number,Number> list = new HashMap<Long, Number>();
(c) Map<Number,Number> list = new TreeMap<>();
(d) HashMap<long,long> list = new HashMap<>();
40. Suppose that class Bar is defined as follow and let b be an instance of Bar:
public class Bar { … int getX() {…}
static int getS(){…} … }
Then which of the following invocation statement is NOT correct?
(a). b.getX();
(b) b.getS();
(c) Bar.getX();
(4) Bar.getS();
III (50 pts) 程式設計
1.
[15pts] Using Java to create two sets of Integers s1 = {2,4,6,8,10 } and s2 = {3,4,6,7,9}, and then find their union
s3, intersection s4 and symmetric difference s5 = (s1 - s2)  (s2-s1). Note that the contents of s1 and s2 should not
be changed by the computation.
public class Main {
pubic static void main(String[] args ) {
Set<Integer> s1 = new HashSet<>( Arrays.asList(2,4,6,8,10)) ;
Set<Integer> s2 = new HashSet<>(Arrays.asList(3,4,6,7,9));
Set<Integer> s3 = new HashSet<>(s1);
s3.addAll(s2);
Set<Integer> s4 = new HashSet<>(s1);
s4.retainAll(s2);
Set<Integer> s5 = new HashSet<>(s3); s5.removeAll(s4);
// (s1-s2) U (s2- s1) = (s1 U s2) – (s1 s2)
}
2.
[15 pts] Implement the following java method: powerset(List<T> s) which, when given a list lst of distinct
elements, can return a list consisting of all subsets of elements of s.
public static <T> List<Set<T>> powerset(List<T> s) {
List<Set<T>> ss1 = new ArrayList<>(); // [ ]
ss1.add(new HashSet<>()); // ss1 is now [{} ]
3
for (T t : s) {
List<Set<T>> ss2 = new ArrayList<>(); // ss2 is [ ]
for (Set<T> s1 : ss1) {
Set<T> s2 = new HashSet<>(s1); // s2 is a duplicate of s1 for each s1 in ss1
s2.add(t); // s2 is s1 U {t}
ss2.add(s2); // ss2 is
[ s1 U {t} | s1 in ss1
]
}
ss1.addAll(ss2);
}
return ss1;
}
3.
[Generics][20 pts] (a) Define a generic class called Pair with two type Parameters T1 and T2 which is intended to
store two values of type T1 and T2, respectively. Pair has two fields: the first one storing a value of type T1 is called
t1 and the second storing a value of type T2 is called t2. Your implementation of Pair must not cause errors in the
following use of the class. [ 12pts]
Pair<String, Integer> pair1 = new Pair<>("abc", 3) ;
Pair<Integer, String> pair2 = new Pair<>(4, "abcd") ;
System.out.println( pair1.t1.equals("abc") ) // return true
System.out.println( pair1.t2 .equals(new Integer(3) ) // return true
public class Pair<___T1,T2__> { //required members: 2 fields + 1 constructor
_public T1 t1;____
_public T2 t2;____
_public Pair(T1 t2, T2 t2) { this.t1 = t1; this.t2 = t2;}__
}
(b) [8pts] Extends the above class by embedding the following two methods: equals(Object) and hashcode () so that
we can test equality of two pairs p1 and p2 and the result would be true iff (t1,t2) of p1 and p2 are equal to each
other.
public boolean equals(Object o) {
if( ! (o instanceof Pair<?,?>)) return false;
Pair<T1,T2> p2 = (Pair<T1,T2>) o ;
return t1.equals(p2.t1) && t2 .equals(p2.t2) ;
}
public int hashCode() {
// the only requirement: Equal objects must have same hashCode();
return t1.hashCode() ^ t2.hashCode();
}
4
Related documents