Menu
Topics Index
...
`

Dhoom32399


If you need explanation Read this topic

If you need Answer Take test on this topic

Dhoom32399
What will be the output of the following program?
import java.util.*;
public class Dhoom3 {
    public static void main(String[] args) {
        Katrina kaif = new Katrina();
        kaif.addObserver(new Amir());
        kaif.addObserver(new Abhishek());
        kaif.notifyObservers();
        kaif.deleteObserver(new Amir());
        kaif.notifyObservers();
    }
}
class Katrina extends Observable {
}
class Amir implements Observer {
    public void update(Observable arg0, Object arg1) {
        System.out.print("Kat | ");
    }
}
class Abhishek implements Observer {
    public void update(Observable arg0, Object arg1) {
        System.out.print("Kaif <> ");
    }
}

A. Kaif <> Kat |
B. Kaif <> Kat | Kaif <> Kat |
C. Kaif <> Kat | Kaif <>
D. No output is generated
E. Compilation Error 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 is D...two observers are present in kaif object...here we are trying to delete a new Amir() object which is not the one in the list so no effect is observed...

Posted by Santosh Munugota    2014-01-10 12:49:23


D is ans..here we havng two obsrvrs. first we add that and then delete it.so no output is generated.

Posted by Shabaaz Shaikz    2014-01-10 12:58:43


The correct option is D, but the explanation is wrong. The reason no output is generated is because although we call notifyObservers, it will only call the actual observers, only it is changed. If the observable kaif is not changed, then the observers are not called. For us to call the update method, we need to call the setChanged method on observable.

Posted by Merit Campus    2014-01-13 13:38:34


Which means no winner for this as well. :-(

Posted by Merit Campus    2014-01-13 13:38:46


© meritcampus 2019

All Rights Reserved.

Open In App