Menu
Topics Index
...
`

Collections1690


If you need explanation Read this topic

If you need Answer Take test on this topic

Collections1690
What is the output of the following program?
import java.util.*;
class Watcher1 implements Observer {
    public void update(Observable obs, Object arg) {
        System.out.println("Watcher " + ((Integer) arg).intValue());
    }
}
class Watcher2 implements Observer {
    public void update(Observable o, Object arg) {
        if (((Integer) arg).intValue() == 0) {
            System.out.println("Done");
        }
    }
}
class Watcher extends Observable {
    void counter(int input) {
        for (; input >= 0; input--) {
            setChanged();
            notifyObservers(new Integer(input));
            clearChanged();
            try {
                Thread.sleep(50);
            } catch (Exception e) {
                System.out.println("Sleep Interrupted");
            }
        }
    }
}
public class MultipleObserves {
    public static void main(String[] args) {
        Watcher observed = new Watcher();
        Watcher1 observing1 = new Watcher1();
        Watcher2 observing2 = new Watcher2();
        observed.addObserver(observing1);
        observed.addObserver(observing2);
        observed.counter(3);
    }
}

A. Watcher 3
Watcher 2
Watcher 1
Watcher 0
Done
B. Watcher 0
Watcher 1
Watcher 2
Watcher 3
Done
C. Watcher 3
Watcher 2
Watcher 1
Done
Watcher 0
D. Watcher 3
Done
E. Compiles and executes without any output
F. Compilation or Runtime Error
Topic: Java Observable

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

ans: E, String index out of range so error will occure

Posted by Anvesh Chowdary    2014-02-24 13:50:36


Answer is c...as  interface object is passed to the method of watcher class the arg value will be 3 then 2 and then 1 then "done" is printed..then watcher 0...

Posted by Sneha Rapool    2014-02-24 13:55:52


Answer is A.On default observers are notified in the order they are registered

Posted by Kumarcs Cskc    2014-02-24 16:05:59


Congratulations Sneha Rapool. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-02-25 04:34:40


© meritcampus 2019

All Rights Reserved.

Open In App