File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Buttons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Buttons" Watch "Buttons" New topic


josephine chen
Ranch Hand

Joined: Oct 29, 2002
Posts: 216
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 given is A. How ??
Lucy C
Ranch Hand

Joined: Feb 09, 2000
Posts: 53
Well, in this code, first you add the same instance of Button, okButton, over and over again - so there's only one "OK" button, even though you've added it several times. Then you create four different new buttons with new Button(), each with the label "Cancel", and add them to the applet - so there are four "Cancel" buttons.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
the answer is b)
I agree. Here's the link:
subject: Buttons
It's not a secret anymore!