JP2012Final

advertisement
Java Programming: Final Examination
姓名:______________
學號:_______________
6/22 2012
分數:______________
I (105 pts) 選擇題[Questions marked with * (ex: Question 9) mean they have more than one answer ]
1.
Fill in the blank in: “Comparable _____ c = new Date();” so that c.compareTo( new Date()) executes normally.
a) <?>
2.
b) <E>
4.
b) ArrayList
c) HashSet
d) LinkedList
e) TreeSet
Which method do you use to test if an element e is in a set or list named x?
a) x.include(e)
b)
(e instanceof List) || (e instanceof Set)
c) x.in(e)
d)
x.contain(e)
e) x.contains(e)
Which of the following is correct to perform the set intersection of two sets s1 and s2?
a) s1.intersect(s2)
5.
d) <Date>
If you want to store non-duplicated objects in the order in which they are inserted, you should use ________.
a) LinkedHashSet
3.
c) <String>
b) s1.intersection(s2)
c) s1.join(s2)
d) s1.retainAll(s2)
The printout of the following code is ________.
LinkedHashSet<String> set1 = new LinkedHashSet<String>();
set1.add("New York");
LinkedHashSet<String> set2 = (LinkedHashSet<String>)(set1.clone());
set1.add("Atlanta");
set2.add("Dallas");
System.out.println(set2);
6.
a) [New York, Atlanta, Dallas]
b) [New York, Dallas]
c) [New York]
d) [New York, Atlanta]
Suppose set s1 is [1, 2, 5] and list L2 is [2, 3, 6]. After s1.addAll(L2), s1 is ________.
a) [1, 2, 2, 3, 5, 6]
7.
c) [1, 5]
d) [1, 2, 3, 5, 6]
e) runtime exception
Suppose set s1 is [1, 2, 5] and set s2 is [2, 3, 6]. After s1.removeAll(s2), s1 is ________.
a) [1, 5]
8.
b) [2]
b) [1, 2, 2, 3, 5, 6]
c) [1, 2, 3, 5, 6]
d) [2]
The ________ method in the Queue interface retrieves and removes the head of this queue, or null if this queue is
empty.
a) remove()
9.
b) poll()
c) element()
d) peek()
* To create a set that consists of string elements "red", "green", and "blue", use
a) new HashSet(new String[ ]{"red", "green", "blue"})
b) new LinkedHashSet(Arrays.asList(new String[ ]{"red", "green", "blue"}))
c)
new Set(Arrays.asList(new String[ ]{"red", "green", "blue"}))
d) new HashSet(Arrays.asList(new String[ ]{"red", "green", "blue"}))
e) new HashSet({"red", "green", "blue"})
10.
Which of the following is correct to create a list from an array?
a) new ArrayList(new String[ ]{"red", "green", "blue"})
b) new List({"red", "green", "blue"})
c)
new LinkedList(new String[ ]{"red", "green", "blue"})
1
d) Arrays.asList(new String[ ]{"red", "green", "blue"})
e) new List(new String[ ]{"red", "green", "blue"})
11.
Which of the following is correct to sort the elements in a list lst?
a) lst.sort()
b) Collections.sort(lst)
c) new LinkedList(new String[ ]{"red", "green", "blue"})
d) Arrays.sort(lst)
12.
To get an iterator from a set, you may use the ________ method.
a) iterators
13.
b) findIterator c) getIterator
d) iterator
Which of the data types below could be used to store elements in their natural order based on the compareTo
method.
a) Collection
14.
b) ArrayList
b) LinkedList
e) LinkedHashSet
d) TreeSet
e) LinkedList
d) Vector
e) Set
Which of the following statements is not defined in the Object class?
b) toString()
c) notifyAll()
d) wait() e) notify()
* You can use the ________ method to temporarily release time for other threads.
a) suspend()
18.
c) Map
c) Stack
a) sleep(long milliseconds)
17.
d) TreeSet
Which of the data types below does not allow duplicates?
a) List
16.
c) Set
Which of the following data types does not implement the Collection interface?
a) HashSet
15.
b) HashSet
b) stop()
c) yield()
d) sleep(long milliseconds)
Which of the following expressions must be true if you create a thread using Thread =
Thread(object)?
19.
a) object instanceof Runnable
b) object instanceof Thread
c) object instanceof String
d) object instanceof Number
Analyze the following code:
public abstract class Test implements Runnable {
public void doSomething()
{
};
}
a) The program will not compile because it does not contain abstract methods.
b) The program will not compile because it does not implement the run() method.
c)
The program compiles fine.
d) None of the above.
20.
*Which of the following methods are in the Collection interface?
a) isEmpty()
21.
c) size()
d) getSize()
*The Collection interface is the base interface for ________.
a) Map
22.
b) clear()
b) List
c) Set
d) LinkedList
e) ArrayList
When you run the following program, what will happen?
public class Test extends Thread {
public static void main(String[ ] args) {
Test t = new Test();
t.start();
t.start();
public void run() {
}
System.out.println("test");
a) The program displays test twice.
b) Nothing is displayed.
2
}
new
c)
The program displays test once.
d) An illegal java.lang.IllegalThreadStateException may be thrown because you just started thread and thread
might have not yet finished before you start it again.
23.
You can use the ________ method to force one thread to wait for another thread to finish.
a) sleep(long milliseconds)
24.
b) yield()
c) suspend()
d) join()
e) stop()
Which of the following declarations use raw type?
a) ArrayList<String> list = new ArrayList<String>();
b) ArrayList<Object> list = new ArrayList<Object>();
c)
ArrayList list = new ArrayList();
d) ArrayList<Integer> list = new ArrayList<Integer>();
25.
To create a list to store integers, use
a) ArrayList<Object> list = new ArrayList<Integer>();
b) ArrayList<Number> list = new ArrayList<Integer>();
c)
ArrayList<Integer> list = new ArrayList<Integer>();
d) ArrayList<int> list = new ArrayList<int>();
26.
27.
To declare a class named A with a generic type, use
a) public class A(E) { ... }
b) public class A(E, F) { ... }
c) public class A<E, F> { ... }
d) public class A<E> { ... }
Which of the following methods in Thread throws InterruptedException?
a) setPriority(int)
28.
b) sleep(long)
c) yield()
d) run()
e) start()
Suppose there are three Runnable tasks, task1, task2, task3. How do you run them in a thread pool with 2 fixed
threads?
a) ExecutorService executor = Executors.newFixedThreadPool(2); executor.execute(task1);
executor.execute(task2); executor.execute(task3);
b) new Thread(task1).start(); new Thread(task2).start(); new Thread(task3).start();
c)
ExecutorService executor = Executors.newFixedThreadPool(1); executor.execute(task1);
executor.execute(task2); executor.execute(task3);
d) ExecutorService executor = Executors.newFixedThreadPool(3); executor.execute(task1);
executor.execute(task2); executor.execute(task3);
29.
Which of the following method is a static in java.lang.Thread?
a) join()
30.
b) setPriority(int)
c) sleep(long)
d) run()
) start()
The equals method is defined in the Object class. Which of the following is correct to override it in the String
class?
31.
a) public boolean equals(String other)
b) public static boolean equals(Object other)
c) public static boolean equals(String other)
d) public boolean equals(Object other)
Which of the statements regarding the super keyword is incorrect?
a) You can use super to invoke a super class constructor.
b) You cannot invoke a method in superclass's parent class.
c)
You can use super.super.p to invoke a method in superclass's parent class.
d) You can use super to invoke a super class method.
32.
You can assign ________ to a variable of Object[ ] type.
3
a) new String[100]
33.
d) new char[100]
Suppose A is an inner class in Test. A is compiled into a file named ________.
a) A.class
34.
b) new int[100] c) new double[100]
b) A$Test.class
c) Test$A.class
d) Test&A.class
Which statement is true about a non-static inner class?
a) It must be final if it is declared in a method scope.
b) It can access private instance variables in the enclosing object.
c) It can only be instantiated in the enclosing class.
d) It is accessible from any other class.
e) It must implement an interface.
35.
36.
*Which of the following statements is correct?
a) Comparable<String> c = new Date();
b) Comparable<Object> c = new Date();
c) Comparable<String> c = new String("abc");
d) Comparable<String> c = "abc";
A variable defined inside a method is referred to as ________.
a) a method variable b) a local variable
37.
38.
a) public static main(String[ ] args)
b) public static Main(String args[ ])
c) public static void main(String[ ] args)
d) public void main(String[ ] args)
Suppose your method does not return any value, which of the following keywords can be used as a return type?
b) double
c) public d) int
e) None of the above
Which of the following statements are correct?
a) char[ ][ ][ ] charArray = new char[2][2][ ];
b) char[ ][ ][ ] charArray = new char[][2][2];
c) char[ ][ ][ ] charArray = {{'a', 'b'}, {'c', 'd'}, {'e', 'f'}};
40.
41.
d) a global variable
All Java applications must have a method ________.
a) void
39.
c) a block variable
d) char[2][2][ ] charArray = {'a', 'b'};
Which of the following statements are correct?
a) char[2][2] charArray = {{'a', 'b'}, {'c', 'd'}};
b) char[ ][ ] charArray = {'a', 'b'};
c) char[ ][ ] charArray = {{'a', 'b'}, {'c', 'd'}};
d) char[2][ ] charArray = {{'a', 'b'}, {'c', 'd'}};
Which of the following declarations are correct?
a) public static void print(String... strings, double... numbers)
b) public static void print(double... numbers, String name)
c)
public static void print(int n, double... numbers)
d) public static double... print(double d1, double d2)
42.
Which of the following kind of nested classes can be instantiated without the need of a containing object?
a) Anonymous inner class.
b) Local class.
c) Member inner class
d) Static nested class.
43.
The ________ method copies the sourceArray to the targetArray.
a) System.arrayCopy(sourceArray, 0, targetArray, 0, sourceArray.length);
b) System.copyArrays(sourceArray, 0, targetArray, 0, sourceArray.length);
c)
System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);
d) System.copyarrays(sourceArray, 0, targetArray, 0, sourceArray.length);
44.
Assume int[ ] t = {1, 2, 3, 4}. What is t.length?
4
a) 5
45.
46.
b) 0
a) int i = new int(30);
b) char[ ] c = new char();
c) double d[ ] = new double[30];
d) int[ ] i = {3, 4, 3, 2};
e) char[ ] c = new char[4]{'a', 'b', 'c', 'd'};
If you declare an array double[ ] list = {3.4, 2.0, 3.5, 5.5}, the highest index in array list is ________.
b) 4
c) 0
d) 1
e) 2
What is the representation of the third element in an array called a?
a) a[3]
48.
d) 3
*Which of the following statements is valid?
a) 3
47.
c) 4
b) a(3)
c) a(2)
d) a[2]
You can declare two variables with the same name in ________.
a) two nested blocks in a method (two nested blocks means one being inside the other)
b) a block
c)
different methods in a class
d) a method one as a formal parameter and the other as a local variable
49.
50.
To prevent a class from being instantiated, ________
a) use the static modifier on the constructor.
b) don't use any modifiers on the constructor.
c) use the private modifier on the constructor.
d) use the public modifier on the constructor.
A method that is associated with an individual object is called ________.
a) a static method
51.
b) public variables
d) an object method
c) instance variables d) private variables
The default value for data field of a boolean type, numeric type, object type is ________, respectively.
a) false, 0, null b) true, 1, Null c) true, 1, null
53.
c) a class method
Variables that are shared by every instances of a class are ________.
a) class variables
52.
b) an instance method
d) true, 0, null e) false, 1, null
Given the declaration Circle x = new Circle(), which of the following statement is most accurate?
a) x contains a reference to a Circle object. b) You can assign an int value to x.
c) x contains an int value.
54.
55.
________ is invoked to create an object.
a) The main method
b) A method with the void return type
c) A method with a return type
d) A constructor
Which of the following statements are correct? (Choose all that apply.)
a) Number i = 4.5;
56.
b) Double i = 4.5;
c) Integer i = 4.5;
d) Object i = 4.5;
*Which of the following classes are immutable?
a) Integer
57.
d) x contains an object of the Circle type.
b) String
c) Double
d) BigInteger
e) BigDecimal
*Suppose A is an interface, B is a concrete class with a default constructor that implements A. Which of the
following is correct?
a) B b = new B();
58.
b) A a = new A();
c) A a = new B();
d) B b = new A();
*Suppose A is an abstract class, B is a concrete subclass of A, and both A and B have a default constructor. Which
of the following is correct?
a) B b = new B();
59.
b) A a = new A();
Given the class definition : class A {
c) B b = new A();
d) A a = new B();
class A1 { } } ,which of the following is the correct way to create an
instance of class A1
a)
new A.A1()
b) (new A()) . new A.A1()
c)
5
new A(new A.A1() )
d)
new A.A1(new A() )
60.
Which of the following class definitions defines a legal abstract class?
a) public class abstract A { abstract void unfinished(); }
b) class A { abstract void unfinished(); }
61.
c)
class A { abstract void unfinished() {
}}
d)
abstract class A { abstract void unfinished(); }
Inheritance means ________.
a) that data fields should be declared private
b) that a variable of supertype can refer to a subtype object
c)
that a class can extend another class
d) that a class can contain another class
62.
63.
64.
Encapsulation means that ________.
a) a class can extend another class
b) a class can contain another class
c) data fields should be declared private
d) a variable of supertype can refer to a subtype object
Polymorphism means that ________.
a)a class can contain another class
b) a variable of supertype can refer to a subtype object
c) data fields should be declared private
d) a class can extend another class
Which of the following classes cannot be extended?
a) class A {
65.
66.
private A();}
b) class A { }
c) class A { protected A();}
d) final class A { }
The visibility of Java modifiers increases in which of the following order:
a) private, package, protected, and public.
b) private, protected, package, and public.
c) package, private, protected, and public.
d) package, protected, private, and public.
What modifier should you use on a class so that a class in the same package can access it but a class in a different
package cannot access it?
a) protected
67.
c) public d) package (default).
Object-oriented programming allows you to derive new classes from existing classes. This is called ___.
a) inheritance
68.
b) private
b) abstraction
c) encapsulation
d) generalization
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 Exception2 is a superclass of Exception3 while Exception1 is not.
a) S6
69.
b) S7
c) S6,S7
d) S4,S6,S7
e) S4,S5,S6,S7
Which of the following method would cause compile error if we put the code : "L.add(e) ;"
a)
public <E> void m0(List<E> L, E e) {… }
b) public <E> void m1(List<? extends E> L, E e) {… }
c)
public <E> void m2(List<? super E> L, E e) {… }
d) public void m2(List L, E e) {… }
6
into their bodies?
70.
*Which of the following methods can be called by "m(new ArrayList<String>(), new Object() ) " without causing
compile error ?
a)
public <E> void m(List<E> L, E e) {… }
b) public <E> void m(List<? extends E> L, E e) {… }
c)
public <E> void m(List<? super E> L, E e) {… }
d) public void m2(List L, E e) {… }
II 程式設計 (30 pts)
1. Given the following class :
public class Sum implements Runnable {
public int rlt, from, to;
Sum(int f, int t) { from = f;
to = t}
public void run() { for(int k = from; k<= to; k++) rlt += k; }
Suppose we want to compute (1+2+…+2000) using multithreads. Therefore we partition this task into two subtasks: task1
perform (1+…1000) and task2 performs (1001+…+ 2000). Now, with the Sum class given, try to design a method which would
create and start two threads to perform each subtask, and finally, after they complete, combine their results to get the final one.
public static void man() {
Sum task1 =
__________________________, task2 =
_____________________________
Thread t1 =
__________________________, t2 = _________________________________ ;
;
// start both threads
______________________________________________________________________________________________
//wait them to complete
___________________________________________________________________________________________________
int rlt = _______________________; out.println(“The sum from 1 to 2000 is “ + rlt); }
2. Design a method duplicate(List L), which, when given a list of objects, can return the set of all objects which occur more than
one time in L.
import java.uitl.*;
static <E> Set<E> duplicate(List<? extends E> L){
Set<E>
s1 =
________________________________ ;
}
7
Download