Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

class MyListener implements ActionListener { public void actionPerformed(ActionE

ID: 3604578 • Letter: C

Question


class MyListener implements ActionListener { public void actionPerformed(ActionEvent event) { System.out.println(event); } }
Timer t = new Timer(interval, listener); t.start();
What is wrong with this code?
a) The Timer object should be declared before the MyListener class. b) The listener has not been attached to the Timer object. c) The Timer object must be declared as final. d) There is nothing wrong with the code.

class MyListener implements ActionListener { public void actionPerformed(ActionEvent event) { System.out.println(event); } }
Timer t = new Timer(interval, listener); t.start();
What is wrong with this code?
a) The Timer object should be declared before the MyListener class. b) The listener has not been attached to the Timer object. c) The Timer object must be declared as final. d) There is nothing wrong with the code.

class MyListener implements ActionListener { public void actionPerformed(ActionEvent event) { System.out.println(event); } }
Timer t = new Timer(interval, listener); t.start();
What is wrong with this code?
a) The Timer object should be declared before the MyListener class. b) The listener has not been attached to the Timer object. c) The Timer object must be declared as final. d) There is nothing wrong with the code.

Explanation / Answer

The correct answer would be (b.)

The listener has not been attached to the Timer object and should be attached like:-

MyListener listnener=new MyListener().

The Timer wont call the actionPerformed method of the listener object and code won't work.