• Tidak ada hasil yang ditemukan

ID Number:

N/A
N/A
Protected

Academic year: 2023

Membagikan "ID Number:"

Copied!
7
0
0

Teks penuh

(1)

3 September 2007

First name: Last name:

ID Number:

Instructions

1. Write your name and ID number into the spaces provided above.

2. There are five questions of equal value.

3. All questions have to be answered.

4. Time allowed is 50 minutes.

5. Write your answers in the spaces provided. Do not use your own paper! There is a blank page at the end of the test that you can use. You can get extra paper from us if necessary.

DO NOT TURN THE PAGE UNTIL YOU

ARE ASKED TO DO SO!

(2)

Circle the correct answer. All statements are about the Java language.

SWING is thread-safe. YES NO

SWING uses double-buffering to avoid flickering. YES NO ActionListeners must call paint() to update the screen. YES NO MouseAdapter is an abstract utility class simplifying MouseListeners. YES NO

Swing and AWT have nothing in common. YES NO

Non-modal dialog demand user input before allowing further action. YES NO Generic types help the compiler catch more errors. YES NO Generic types reduce the number of casts needed. YES NO

Threads stop once the run method finishes. YES NO

Callables are like Runnables, but also return a result. YES NO You cannot not have more threads than CPU cores in a ThreadPool. YES NO Synchronized methods are used to avoid inconsistencies. YES NO

Any object in Java can be serialized. YES NO

Readers and Writers are for textual IO only. YES NO

Serialization handles shared structure correctly. YES NO Java uses automatic garbage collection, therefore has no memory leaks YES NO

(3)

Regular expressions: for each of the five method calls below work out the respective result

"comp204b test".replaceFirst(" ",":");

"comp204b test".match(" ");

"comp204b test".match("^\d+");

"comp204b test".split("\W");

"comp204b test".split("\d+");

(4)

Constructor confusion: what is the output produced by this program?

class Confuse {

public static void main(String[] args) {

A object1 = new A(7);

System.out.println(object1);

System.out.println("");

A object2 = new B(3);

System.out.println(object1);

System.out.println(object2);

System.out.println("");

B object3 = new B();

System.out.println(object1);

System.out.println(object2);

System.out.println(object3);

} }

class A {

public A() { _a++; } public A(int a) { _a = a; }

public String toString() { return "A: _a = " + _a + "; "; } private static int _a;

}

(5)

Overloading: what is the output produced by this program?

[Hint: the string representation of the result of a receiver.getClass() call is a string

”class X”, if the actual class of the receiver at runtime was class X.]

class X {

String m( X arg ) { return "in X got a " + arg.getClass(); } }

class Y extends X {

String m( Y arg ) { return "in Y got a " + arg.getClass(); } }

public class OverloadTest {

public static void main(String[] args) { X x = new Y();

Y y = (Y) x;

System.out.println( x.m(x));

System.out.println( x.m(y));

System.out.println( y.m(x));

System.out.println( y.m(y));

} }

(6)

Immutable objects can be very useful, especially with respect to program correctness as well as efficiency in multi-threaded processing: as they only allow read-access, they cannot be accidentally modified, and access can be performed in parallel. Fill out missing bits below for an immutable Map implementation, which is based on a HashMap by way of Composition (also called the Wrapper pattern). You need to fill in proper types into the marked spaces, as well as appropriate code for all three methods.

public final class ImmutableMap<K,V> implements Map<K,V> { HashMap<K,V> mapping;

//

// constructor which initialises from a given map of appropriate type //

public ImmutableMap<K,V>( Map<? extends K, ? extends V> someMap ) { mapping = new HashMap( someMap);

} //

// return the size of this map.

//

public ____ size() {

} //

// retrieve the value associated with the given key //

public ____ get( ____ key) {

(7)

Referensi

Dokumen terkait