Question 2
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
class Lamp {
boolean on;
void switchOn() { on = true; }
}
public class Main {
public static void main(String[] args) {
Lamp l = new Lamp();
l.switchOn();
l.switchOff();
}
}
- A Lamp needs a constructor
- B Lamp declares no switchOff() method, so javac cannot find the symbol Correct
- C switchOn must return boolean
- D l must be final
Correct Answer: Option B
Only methods declared in the class (or inherited) can be called.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!