課程名稱︰ 物件導向程式設計
課程性質︰ 系必修
課程教師︰ 陳俊良
開課學院: 電資學院
開課系所︰ 資工系
考試日期(年月日)︰ Jun 21 2007
考試時限(分鐘): 120 min
是否需發放獎勵金: 是
(如未明確表示,則不予發放)
試題 :
1. What are IS-A and HAS-A relationships? Please compare them. [(2+2+4)%]
2. There is a famous dangling else problem associated with if statement. Is
there a corresponding catch problem associated with try statement? Why?
[2+6%]
3. Consider the following classes and interface.
interface Drinkable { }
class Cup implements Drinkable { }
class Mug extends Cup { }
class Queue<E> { }
Are they legal statements? Please explain your reason briefly (at most 3
lines). [8*(2+3)%]
(a) Cup c1 = new Mug();
(b) Mug m1 = new Cup();
(c) Drinkable d1 = new Mug();
(d) Drinkable d2 = new Drinkable();
(e) Queue<Cup> qc1 = new Queue<Mug>();
(f) Queue<Mug> qm1 = new Queue<Cup>();
(g) Queue<Drinkable> qd1 = new Queue<Mug>();
(h) Queue<Drinkable> qd2 = new Queue<Drinkable>();
4.Can it be guaranteed that, when the program stops, the value of counter is
0? Please explain your reason briefly (at most 3 lines). [4*(2+3)%]
(a) class Thread1 {
static int counter = 0;
public static void main(String[] args) {
new Thread(new Runnable1()).start();
for (int i = 0; i < 10000; i++) counter++;
}
}
class Runnable1 implements Runnable {
public void run() {
for (int i = 0; i < 10000; i++) Thread1.counter--;
}
}
(b) class Thread2 {
public static void main(String[] args) {
Thread2A t = new Thread2A();
t.run();
for (int i = 0; i < 10000; i++) t.counter++;
}
}
class Thread2A extends Thread {
int counter = 0;
public void run() {
for (int i = 0; i < 10000; i ++) counter--;
}
}
(c) class Thread3 implements Runnable{
static int counter = 0;
public static void main(String[] args) {
Thread3 t = new Thread3();
new Thread(t).start();
synchronized (t) {
for (int i = 0; i < 10000; i++) counter++;
}
}
synchronized public void run() {
for (int i = 0; i < 10000; i++) count--;
}
}
(d) class Thread4 extends Thread {
int counter = 0;
public static void main(String[] args) {
Thread4 t = new Thread4();
t.start();
synchronized (Thread4.class) {
for (int i = 0; i < 10000; i++) t.counter++;
}
}
synchronized public void run() {
for (int i =0 ; i < 10000; i++) counter--;
}
}
5.Please write down the output of the following program. [8*3%]
class Super {
public static void main(String[] arg) {
Super s1 = new Super();
System.out.println(s1.m('a')); // (a)
System.out.println(s1.m('b')); // (b)
System.out.println(s1.m('c')); // (c)
System.out.println(s1.m('d')); // (d)
Super s2 = new Sub();
System.out.println(s2.m('a')); // (e)
System.out.println(s2.m('b')); // (f)
System.out.println(s2.m('c')); // (g)
System.out.println(s2.m('d')); // (h)
}
static void throwException(char c) throws Throwable {
switch(c) {
case 'a': throw new ExpA();
case 'b': throw new ExpB();
case 'c': throw new ExpC();
}
}
int m(char c) {
int n = 0;
try {
throwException(c);
n +=1;
} catch (ExpB e) { n += 2;
} catch (ExpA e) { n += 3;
} catch (Exception e) { n += 4;
} catch (Throwable e) { n += 5;
}
return n;
}
}
}
class Sub extends Super {
int m(char c) {
int n = 0;
try {
throwException(c);
n += 1;
} catch(Throwable e) {
if (e instanceof ExpA) n +=2;
if (e instanceof ExpB) n +=3;
if (e instanceof ExpC) n +=4;
if (e instanceof Exception) n += 5;
} finally {
n += 6;
}
return n;
}
}
class ExpA extends Throwable { }
class ExpB extends Exception { }
class ExpC extends ExpA { }
--
╭ ╮ ┬ ╮│ ╮ ╭ ╭╮ ╭ ╭─┬ ╭──╮ ╮ ╭ ╭┬─╮
╰────╯ ─┼─ ╮│ │ │ ││ │ │ │ │ │ │
╭────╮ │││ ─┼╮├─┬╯ │╰╮│ │ │ ├──┤ ╰╮
│ │ │││ │ │ │ │ ││ │ │ ┬ │ │ │
╰────╯ ╯╯╰ ╰ ╯ ╰─ ╯ ╰╯ ─┴╯╰──╯ ╯ ╰ ┴
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 202.132.132.7