File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Mock Exam Errata and the fly likes Applied Reasoning mock exam question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Applied Reasoning mock exam question" Watch "Applied Reasoning mock exam question" New topic

Applied Reasoning mock exam question

Peter Chirco

Joined: Nov 20, 2001
Posts: 4
I have a problem with one of the applied reasoning questions. The question reads like this:
Select each of the code fragments that would allow a newly created java.awt.Button to generate ActionEvents:
a. button.enableActionEvents();
b. button.addActionListener(anActionListener);
c. button.enableEvents(true);
d. button.enableEvents(AWTEvent.ACTION_EVENT_MASK);
I answered both b and d. He suggetst b is the only correct answer, and the reasoning for d not being correct is that , since enableEvents is protected, it can only be called by a subclass. Since enableEvents is defined in Component, and since Button derives from Component, shouldn't it be able to call it? Also, looking at the Exam Cram Sun Certified Java Programmer book, they have an example that is almost identical to this.
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Peter,
The correct answer is b and only b.
Choices a and c are not viable choices because they don't match any existing method signatures.
Why is d not a valid answer?
A protected method can only be called from the class in which it was created (java.awt.Button) and any subclasses of it (extends java.awt.Button).
The question states: a newly created java.awt.Button which rules out a subclass of java.awt.Button and the Button code itself. Knowing this we can say that d is not a valid choice.
Or arguing the other way we can say that the only way that I can create a java.awt.Button is with the new operator. By using this, I have not subclassed it or changed its code, therefore, I can not call any protected code.
Another great argument is to code it up and see that the compiler will not even let you get away with calling a protected method without being a subclass or the originating class.
Peter Chirco

Joined: Nov 20, 2001
Posts: 4
OK, I did some coding, and I am getting the results you mentioned, but I am still not sure why. I created a frame, and added 2 components to it: a Button, and a myButton, which I defined. I first tried to call button1.enableEvents(), and I got an error telling me that method was not visible. That is what you told me to expect. I then used the usual process, added an ActionListener, and got it to work fine. For myButton, I called enableEvents() in the constructor, and then overrode the processActionEvent() method. I added that to my frame, and it works also. So I have a frame with two buttons, both of which respond to action events, one through an event listener and one through explicit event enabling.
I still have the question, though, why I could not call button1.enableEvents(). since the method is defined in Component, as a protected final method, I should be able to call that method from any subclass of Component, or any class in the same package as Component.
import java.awt.*;
import java.awt.event.*;
public class ActionTest extends Frame implements ActionListener
class windowHandler extends WindowAdapter {
public void windowClosing(WindowEvent e) {
Button button1;
MyButton button2;
public ActionTest()
addWindowListener(new windowHandler());
button1 = new Button("Button");
button2 = new MyButton("MyButton");
setLayout(new GridLayout(1,2));
// button1.enableEvents(AWTEvent.ACTION_EVENT_MASK);
public void actionPerformed(ActionEvent ae)
Button tempbutton = (Button)ae.getSource();
if(tempbutton == button1)
System.out.println("Button 1");
public static void main(String[] args)
ActionTest at = new ActionTest();
at.setBounds(10, 10, 50, 200);;
I agree. Here's the link:
subject: Applied Reasoning mock exam question
It's not a secret anymore!