Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes AWT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "AWT" Watch "AWT" New topic


rag mul

Joined: Oct 31, 2003
Posts: 12
can you explain the result of this code??
In the following applet, how many Buttons will be displayed?

1: import java.applet.*;
2: import java.awt.*;
4: public class Q16 extends Applet
5: {
6: Button okButton = new Button("Ok");
8: public void init()
9: {
10: add(okButton);
11: add(okButton);
12: add(okButton);
13: add(okButton);
15: add(new Button("Cancel"));
16: add(new Button("Cancel"));
17: add(new Button("Cancel"));
18: add(new Button("Cancel"));
20: setSize(300,300);
21: }
22: }
A) 1 Button with label "Ok" and 1 Button with label "Cancel" .
B) 1 Button with label "Ok" and 4 Buttons with label "Cancel" .
C) 4 Buttons with label "Ok" and 1 Button with label "Cancel" .
D) 4 Buttons with label "Ok" and 4 Buttons with label "Cancel" .

THE Answer is B.
Jesper Ottosson
Ranch Hand

Joined: Mar 21, 2000
Posts: 51
AWT doesn't allow you to add the same instance of a component to more than one location in a layout. That's why the OK button is only added the first time. The Cancel buttons on the other hand, aren't the same instance, they just happen to have the same titles.
It is sorta covered in the JavaRanch Style Guide.
subject: AWT