Download JavaFinal-2010!Solution

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

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

Document related concepts
no text concepts found
Transcript
Java Programming: Final Examination
姓名:______________
學號:_______________
6/28 2010
分數:______________
I (40 pts) 是非題 :
Solution: oxxox xoxox xoxxo xoxxx
1.
It is permitted to define a Java class inside another Java class.
2.
After defining a generic class such as List<E>, we will use many instances of it like List<String>,
List<Number> etc. Hence the Java compiler will create different bytecode for each instance of it.
3.
Let C be a Java class which overwrites the equals(Object) and hashCode() methods of the Object class. Now
if c1 and c2 are both instances of C, then, according to the java Object contract, it is admissible that
c1.equals(c2) returns true while c1.hashCode() and c2.hashCode() have different values.
4.
An instance of a class A can be assigned to a variable whose declared type is an interface implemented by a
ancestor class of A.
5.
A concrete class may leave some abstract methods of its super class unimplemented.
6.
We start a thread by executing its run() methods.
7.
After compiled into Java byte code, every java class must have at least one constructor even if it is abstract.
8.
The statement:
9.
Object[] is a subtype of Object.
Map<String,Long> x = new Map<String,Long>(); is a legal java statement.
10. If A is a class with a public method m(), we can override m() with a new protected m() in a subclass of A.
11. We can change the content of a java object of the type java.lang.String after it has been created.
12. In the code of a static method m of a class, we cannot access an instance variable x of the same class by
using the expression this.x.
13. We should use ArrayList instead of LinkedList if we need to insert or remove elements of a list frequently.
14. Suppose the method m has the head: int m( List<Number> x). We can call it using m(new
ArrayList<Integer>() ).
15. Suppose the method m has the head: int m( List<? extends Number> x). Then we can call it using m( new
ArrayList<Integer>() ).
16. Suppose the method m has the head: int m( List<? super Number> x). Then we can call it using m( new
ArrayList<Integer>() ).
17. If the method m has the head: int m( Number[] x), then we can call it using m( new Integer[0] ).
18. If the method m has head:
int m( Integer[] x), then we call it using m( new Number[0] ).
19. Since the method wait() is defined at Object class, we can call it inside any method body without causing
errors.
20. HashMap is a thread-safe implementation of Map while Hashtable is not.
II (60 pts) 單選
Solution: aabcd bdaad addac ddcad bccbc dcbdb
Answer Question 21-23 according to the following assumption: Suppose that list1 is a list that contains
the Integer objects 1, 3,5 and that list2 is another list that contains the Integer objects 1,2,4. Note we use
[1,3,5] to denote a list containing the sequence 1,3,5 of elements.)
1
21. What is list1 after executing list1.addAll(list2)?
(a).[1,3,5, 1,2,4]
(b) [1,3,5,2, 4]
(c)
[1,1,2,3,4,5]
(d) [1,3,5, [1,2,4] ]
22. What is list1 and list2 after executing list1.removeAll(list2)?
(a). [3,5]
(b) [1,3,5,1,2, 4]
(c) [3,5,2,4]
(d)
[2,4]
23. What is list1 after executing list1.retainAll(list2)?
(a). [3,5]
(b) [1]
(c) [3,5,2,4]
(d) [2,4]
Answer Question 24-26 according to the following code:
Map map = new HashMap();
map.put(“B”, 123), map.put(“D”,111), map.put(“B”, 456), map.put(“A”, 222);
Map map2 = new TreeMap(map); map2.put(“H”, 789);
24. What is the value of the invocation: map1.size() ?
(a). 5
(b)
4
(c) 3
(d) 2
25. What is the value of the code: map2.values().iterator().next() ?
(a). 123
(b) 111
(c) 456
(d) 222
26. What is the value of the invocation: map2.size()?
(a). 5
(b) 4
(c)3
(d) 2
27. Which of the following is NOT a valid Java identifier?
(a). applet
(b) _123
(c) $No_2
(d)
R#4
28. Which of the following is not a Java keyword?
(a). Class
(b) protected
(c) int (d) interface
29. When will the Java expressions: a/b, and a%b both be evaluated to positive results, if the pair (a,b) of int
variables is given as follows:
(a). (78, 5)
(b) (78,-5)
(c)
(-78, 5)
(d)
(-78, -5)
30. Which of the following is NOT a correct literal for Java floating-point number?
(a). 12.3
(b) 23.4E-2
(c) 39f
(c) 5.23 e2
31. Which of the following statements will produce compile-time error? Suppose y is a variable of long type..
(a). int x = y;
(b). float f = y;
(c). double d = y;
(d). long x = y;
32. Which of the following is NOT a possible value of the expression:
(a). 5.05
(b)
0.234
(c)
0.0
(d)
10*Math.random() ?
23.4
33. What is the value of y after executing the following statements:
x = 3; y = 3; switch( x + 4) { case 6 : y++;
(a). 3
(b) 4
(c) 5
case 7 : y++;
case 8: y++;
default: y++; }
(d) 6
34. What is the value of count after executing the following statements:
int n = 40; int count = 5 ; while(count <= n) {count = count + 3 ;}
(a). 41
(b) 42
(c) 43
(d) 40
35. What is the value of (x,y) after executing the following double for-loop ?
int x,y; for(x = 1; x < 5; x++) { for(y=1; y < 5; y++) { if( x <= y) break; };
2
if(x*y >5) break; }
(a). (2,2)
(b) (2,3)
(c) (3,3)
(d) (3,4)
36. Which of the following is not a correct way of declaring an array?
(a) int[][]
a = new int[3][]; ;
(b) int[]
a = new int[3];
(c) int[]
a = {1,2,3} ;
(d) int[3]
a ={1,2,3};
37. Which of the following is the head of a legal declaration of a method with variable-length argument?
(a). public void m(String … s, double… ns )
(b). public void m(double … ns, String s)
(c). public double … m(String
s, double ns)
(d). public double m(String s, double … ns)
38. What is the value of the variable rlt after executing the following statements?
int[] xs = new int[10]; for(int k= 0; k < 10; k++) xs[k] = k+2;
int rlt = 0; for(int t : xs) { rlt += t; }; rlt = rlt / 10;
(a) 4
(b) 5
(c) 6
(d) 7
39. Suppose class A extends class B, class B extends class C, and all 3 classes declare a public field f. In order
for program inside class A to refer to the field f declared at class C, which of the following expressions can
be used :
(a)
((C) this). f
(b)
super.super.f
(c) C.super.f
(d)
(C) f
40. Suppose class A extends class B, class B extends class C, and all 3 classes declare a public field m(). In
order for program inside class A to refer to the method m() declared at class C, which of the following
expressions can be used :
(a) super.super.m()
(b)
(C (this)). m()
(c) super.m()
(d) none of the above
int[][] scores = {{9,2,1},{5,2,4,7}, {1,2},{}};
41. Given the code :
What is the value of the
expression: scores[1][3] ?
(a) 4
(b) 7
(c) 2
(d) 1
(e) run-time error
42. Suppose that class Foo is defined as follow and let f be an instance of Foo:
public class Foo { int i; static String s;
void getI() {}; static void getS(){} }
Then which of the following invocation statement is NOT correct?
(a). f.getI();
(b) f.getS();
(c) Foo.getI();
(4) Foo.getS();
43. Suppose class Foo and object reference f are defined as in the preceding problem. Then which of the
following expressions is NOT legal ?
(a). f.i
(b) f.s
(c) Foo.i
(d) Foo.s
44. Which of the following definitions define a legal abstract class?
(a) class A { abstract void m() ; }
(b) abstract class A { void m() {} }
(c) abstract class A { void m(); }
(d) class A { abstract void m() {} }
45. What kind of error would occur in the following code?
1.
Class Student extends Person {}
3
2.
Class Person {
3.
public static void main(String[] args) {
4.
Object
person = new Student();
5.
Student student =
person;
}}
(a) compile-time error at line 4
(b) run-time error at line 4
(d) run-time error at line 5
(e) no error
(c) compile-time at line 5
46. If the statement S2 cause an exception (and all other Sk( k = 1..7) do not) in the following statement:
try { S1;
S2;
S3;
} catch(Exception1 e1) { S4;
} catch(Exception3 e3) { throw new Exception3(); }
} catch(Exception2 e2) { S5; }
finally { S6; }
S7;
Then which of the following statement sequences is the correct sequence of statements executed after S2, if
the exception is of the type Exception3? Suppose that Exception1 is NOT a superclass of Exception3 but
Exception2 is.
(a) S3,S4,S5,S6,S7
(b) S5,S6,S7
(c) S6,S7
(d) S6
(e) S7
47. Which of the following statements will not throw an exception?
(a). int
x=
(new int[4]) [4];
c = “abc”.charAt(3);
(b). char
(c). double d = 1.0 /0 ;
(d). String
s1 = (String) new Object();
48. Which of the following Java Thread methods is deprecated and should not be used?
(a). run
(b) stop
(c) sleep
(d) join
49. Which of the following Java methods will not cause the target thread to transition to blocked state?
(a). join
(b) sleep
(c) wait
(d)
yield
50. Given the following code:
String s1 = “Welcome to Java”;
String s2 = new String(“Welcome to Java”);
String s3 = s2.intern();
Which of the following expressions evaluates to true?
(a) s1 == s2
(b)
s1 == s3
(c)
s2 == s3
4
(d) none of the above
III (30 pts) 程式設計
1.
[10pts] Using Java to create two sets of Strings s1 = {"A", "B", "C", "D", "E" } and s2 = {"B", "D",
"F","H" }, and then find their union s3, difference s4 and intersection s5. Note that the values of s1 and s2
should not be changed by the computation.
public class Main {
pubic static void main(String[] args ) {
Set<String> s1 = new HashSet<String>(Arrays.asList(New String[] {"1","2","3","4","5"} )) ;
Set<String> s2 = new HashSet<String>(Arrays.asList(New String[] {"2","4","6","8"} )) ;
Set<String> s3 = new HashSet<String>(s1);
Set<String> s4 = new HashSet<String>(s1);
Set<String> s5 = new HashSet<String>(s1);
s3.addAll(s2);
s4.removeAll(s2);
s5.retainAll(s2);
}
2.
[20pts]Implement two Java methods: one for matrix addition and one for multiplication. Note that before
computing the result, you must check if the input matrices m1 and m2 are suitable for the intended
operations: for addition, both matrices must has the same number of rows/columns whereas for
multiplication, if m1 is a mxn matrix, then m2 must be a nxp matrix for some positive numbers m, n and p.
If either m1 or m2 is null or empty, or if m1 and m2 do not satisfy the above condition, then your code
should throw an “Illegal Arguments” Runtime Exception. You can assume that both arguments of the
methods are rectangle arrays.
public static double[][] addition ( double[][] m1,
double[][] m2 ) {
if( ( m1 == null || m2 == null ) || ( m1.length == 0 || m1[0].length == 0) )
|| (m2.length == 0 ||
m2[0].length == 0))
throw new RuntimeException(“IllegalArgumentsException”);
if( m1.length != m2.length || m1[0].length != m2[0].length)
throw new RuntimeException(“IllegalArgumentsException”);
// main logic …
double[][] rlt = new double[m1.length][m1[0].length];
for(int x = 0 ; x < m1.length; x++) {
for(int y = 0; y < m1[0].length; y++) {
rlt[x][y] = m1[x][y] + m2[x][y] ;
}}
return rlt;
}
5
public static double[][] multiplication ( double[][] m1,
double[][] m2 ) {
if( ( m1 == null || m2 == null ) || ( m1.length == 0 || m1[0].length == 0) )
|| (m2.length == 0 || m2[0].length == 0))
throw new RuntimeException(“IllegalArgumentsException”);
// additional m1 and m2 format checking …
if( m1[0].length != m2.length)
throw new RuntimeException(“IllegalArgumentException”);
// main logic …
double [][] rlt = new double[m1.length][m2[0].length];
for(int x = 0 ; x < rlt.length; x++) {
for(int y = 0; y < rlt[0].length; y++) {
for(int k = 0; k < m1[0].length; k++)
rlt[x][y] += m1[x][k] + m2[k][y] ;
}}
return rlt;
}
6