Java in 21 Days (7th Edition) Day 12: Responding to User Input

The cover of Teach Yourself Java in 21 Days (7th Edition) by Rogers Cadenhead

Home Feedback Other Books Previous Day Next Day

Notes and Corrections

  • View the Certification Practice quiz solution
  • Certification answer: Answer (c) is correct. When no action command has been explicitly set up for an interface component such as a JButton, the getActionCommand() method returns the text label of the component. Calling equals("Yes") on this string returns true when the text of the button that was clicked is "Yes".

    Answer (a) is incorrect. Both components that have action listeners are JButton objects, so instanceof will always be true.

    Answer (b) is incorrect. Everything is correct up to the point that the text of the component's equals("yes") method is called. The equals() method is case-sensitive, so it always returns false because none of the buttons has the text "yes".

    Answer (d) is incorrect because the program will not compile successfully with this line in it. The conditional associated with an if statement must be surrounded by parenthesis marks.

Source Files