Saturday, 12 September 2009

Java Certification Question 0093

What will happen when you attempt to compile and run this code

//Demonstration of event handling
import java.awt.event.*;
import java.awt.*;

public class MyWc extends Frame implements WindowListener{

public static void main(String argv[]){
MyWc mwc = new MyWc();
}

public void windowClosing(WindowEvent we){
System.exit(0);
}//End of windowClosing

public void MyWc(){
setSize(300,300);
setVisible(true);
}
}//End of class

1) Error at compile time
2) Visible Frame created that that can be closed
3) Compilation but no output at run time
4) Error at compile time because of comment before import statements







Answer)

1) Error at compile time

If you implement an interface you must create bodies for all methods in that interface. This code will produce an error saying that MyWc must be declared abstract because it does not define all of the methods in WindowListener. Option 4 is nonsense as comments can appear anywhere. Option 3 suggesting that it might compile but not produce output is ment to mislead on the basis that what looks like a constructor is actually an ordinary method as it has a return type.

No comments: