JavaQuiz2-2012!Solution

advertisement
Java Programming: 2nd Quiz
姓名:______________
6/8 2012
學號:___Solution______
分數:______________
I (40 pts) 是非題(mark o if correct and x otherwise.)
Solution: OOOOX OX-OO OXXOX OOXOX 註: 標示”-”表題意不清,不扣分。
1.
O
Every interface is the super type of all classes which implement it.
2.
O
A class and its super class in Java can each contain a field with a same name and type.
3.
O
The statement:
Object o = “abc” ; is a legal Java statement.
4.
O
The statement:
Object x = new Object[0];
5.
X
The statement:
Long x = new Integer(1);
6.
O
A Java class may extend at most one super class but can implement more than one interface.
7.
X
An abstract class must contain at least one abstract method.
8.
-
A final method cannot be extended.
9.
O
A final abstract class is useless.
is a legal Java statement.
is a legal Java statement.
// should be changed to ‘overridden’
10. O
All methods in an interface are abstract.
11. O
If interface I extends interface J and C is a concrete class implementing I, then C must also implement all
methods declared in J.
12. X
If B is a subclass of A and both classes have a public method of the same name m, then we say the subclass
method m overrides the super class method.
13. X
The expression: “abc” instanceof Date will has value false.
14. O
If A is a class with a protected method m(), we can declare a public method m()in a class B which extends A.
15. X
Every protected variable of a class cannot be accessed from other classes of the same package.
16. O
In the code of a static method m of a class, we cannot access an instance variable x of the same class by using
// exception thrown!
the expression this.x.
17. O
Since String is a subtype of Object in Java, String[] is a subtype of Object[] and hence the statement:
Object[] o = new String[0] ; has no compile error.
18. X
Since List is a super type of ArrayList and Object is a super type of String, the statement :
List<Object> x = new ArrayList<String>() ; has no compile error.
19. O
Using java reflection API, it is possible to decide whether an input object is an array, and if it indeed is an array,
it is also possible to find the length and component type of the array.
20. X
The body of the finally-clause in a try-catch-finally statement will not be executed if the try-body throws an
exception and that exception is captured by some catch-clause.
II (51 pts) 21~32 :單選(3pts); 33~35:多選(5pts)
Solution: 21~32: bebed
dbcc(C|D)
da
33:xoxoo
34:ooxoo
21. Which of the following Java statement will produce compile error?
(a) List<String> s = new ArrayList<String>() ;
1
35:xooox
(b) ArrayList<String> s = new ArrayList() ;
(c) ArrayList s = new ArrayList<String>() ;
(d) ArrayList s = new ArrayList() ;
22. If A is a class with a private method m(), and B is a subclass of A and also declares a public method m(). Then
what’s wrong with the following Java statement: ((A)(new B())).m(); located inside B.
(a) no error and B’s m() will be executed
(b) no error and A’s m() will be executed
(c)
compile-time type-cast error
(d) runtime type cast exception
(e)
compile-time method not found/inaccessible error
23. Suppose class C extends class B, class B extends class A, and all classes declare a public field f. In order to refer to
the field f declared at class A within class C, Which of the following expressions can be used :
(a) super.super.f,
(b)
((A) this). f
(c) super.f
(d) (A) f
(e)
A. super.f
24. Suppose class C extends class B, class B extends class A, and all classes declare a public field m(). In order to refer
to the method m() declared at class A within class C, Which of the following expressions can be used :
(a) super.super.m()
25.
(b)
(A (this)). m()
(c) super.m()
(d) A.super.m() (e) none of the above
Which of the following definitions define a legal abstract class ?
(a) abstract class A { void m(); }
(b) class A { abstract void m() {} }
(c) class A { abstract void m() ; }
(d) abstract class A { void m() {} }
// abstract class need not have abstract methods
26. Which of the following is a correct interface definition?
(a) interface A { void print() {};}
(b) abstract interface A { print(); }
(c) abstract interface A extends If1,If2 { abstract void print() {}; }
(d) interface A { void print(); }
27. Consider the enhanced-for statement : for(Object s : L) System.out.println(s); . Then in order for
the statement to be correct, which of the following should not be the type of L?
(a) Collection
(b) Array
(c) List
(d) Queue
(e) Set
28. Which of the following statements is incorrect (after taking autoBoxing into account) ?
(a) double d = 3 ;
(b) double d = 3.0;
(c) Double d = 3 ;
(d) Double d = 3.0
(e) Number d = 3 ;
// Don’t select (e); it’s correct!
29. What is the output of the following code:
Integer
x1 = new Integer(2),
x2 = new Integer(2);
System.out.println(x1==x2 + “|” + x1.equals(x2) );
(a) true|true
(b) true|false
(c) false|true
(d)
false|false
30. Consider the following two classes:
2
package p1;
public class A {
x?
void m() {…}
package p2;
public class B extends p1.A {
}
void m() { … }
}
If we replace “x?” inside class A with one of the following modifier, which replacement would produce correct
definition?
(a) “public”
(b) “protected”
(c) “” ( i.e, package)
(d) “private”
(e) none of the above
31. Suppose E3 is subclass of E2 but not a subclass of E1. Consider the following try-catch statement:
try { throw new E3();
S0;
} catch(E1 e1) { S1;
} catch(E2 e2) { S2; throw new E3();
} catch(E3 e3) { S3;
}
finally { S4; }
S5;
Then which of the following is the correct execution order of all statements that would be executed provided that all
Sk(k=0..5) can be executed normally without throwing exceptions.
(a) S1 S2 S3 S4 S5
(b) S2 S3 S4 S5
(c) S3 S4 S5
(d)
S2 S4
(e) S2 S4 S5
32. Suppose Apple is a class but not a subclass of the String class. Then how many lines in the following code will
produce compile error:
(1). String
x = “abc”;
(2). Object xo = (Object) x;
(3). Apple
xa1 = (Apple) xo;
(4). Apple
xa2 = (Apple) x ;
(a) 1
(b) 2
(c) 3
(d) 4
(e) 0
33. (多選;5pts) Suppose m0 is such a method: public <E> void m0(List<E> L, E e) {… }, then
(a) We can call it by m0(new ArrayList<String>(), new Object());
(b) We can call it by m0(new ArrayList<Object>(), “abc” );
(c) We can call it by m0(new ArrayList<String>(), 123);
(d) The method body can contain L.add(e); without causing compile error .
(e) The method body can contain L.remove(e); without causing compile error .
34. (多選; 5pts) Suppose m1 is such a method: public <E> void m1(List<? extends E> L, E e) {… }, then
(a) We can call it by m1(new ArrayList<String>(), new Object()) ;
(b) We can call it by m1(new ArrayList<Object>(), “abc” );
(c) The method body can contain L.add(e) ; without causing compile error .
(d) The method body can contain L.remove(e) ; without causing compile error .
(e) The method body can contain E e2 = L.get(0); without causing compile error .
35. (多選; 5pts) Suppose m2 is such a method: public <E> void m2(List<? super E> L, E e) {… }, then
(a) We can call it by m2(new ArrayList<String>(), new Object()) ;
(b) We can call it by m2(new ArrayList<Object>(), “abc” );
(c) The method body can contain L.add(e); without causing compile error .
3
(d) The method body can contain L.remove(e) ; without causing compile error .
(e) The method body can contain E e2 = L.get(0); without causing compile error .
III (34 pts) 程式設計
36. [12pts] Use java reflection API to implement a method which, when given an Object instance x and a fully qualified
name n of an interface, will return true if and only if x is also an instance of the named interface.
import java.lang.reflect.*;
public static boolean isInstanceOf(Object x, String n) {
Class c = _x.getClass();___ ;
Class[] ifs = ___c.getInterfaces()_____ ;
for(Class c1 :
____ifs_____ )
if( _____n.equals( c1.getName())__ ) return true ;
return false ;
}
37. [12pts] Given two lists of Strings L1 and L2, find the set S1={x | x∈L1 or x∈L2} and S2 = {x | x∈L1 and
x.length()> 2} :
List<String> L1= … , L2 = … ;
// Assume java.uitl.* has been imported!
Set<String> S1 = __new HashSet(L1)___;
// or new TreeSet(L1) ;
S1 = _S1.addAll(L2);____ // “S1 = “ should be removed!
Set<String> S2 = ____new__TreeSet(L1)___;
_for(Iterator<String>
// or new HashSet(L1);
iter = S2.iterator() ; iter.hasNext();
if( !(iter.next().length() > 2)) iter.remove() ;
) {__
//
}
38. [10pts] Given a list L of possibly unknown type, find a map = { <x, y> | x ∈ L and y the number of times x occurs in
L } to record the number of times each element of L occurs in the list.
import java.uitl.*;
static <E> Map<E, Integer> freqMap(List<? extends E> L ) {
Map<E,Integer> rlt = ____new HashMap<E,Integer>()________
for(E e: L) {
if(rlt.get(e) == null) {
rlt.put(e,1) ;
} else{
rlt.put(e, rlt.get(e) + 1)
}
return rlt;
}
4
;
Download