LearnFrenzy
Sign in Take a free test

Encapsulation · Predict the Output · Question 11

Updating two related fields together Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 11 Back to Exercise

What is the output of the following Java program?

Main.java
class Window {
    private int start = 9, end = 17;

    boolean reschedule(int s, int e) {
        if (s < 0 || e > 24 || s >= e) return false;
        start = s; end = e;
        return true;
    }

    int hours() { return end - start; }
}

public class Main {
    public static void main(String[] args) {
        Window w = new Window();
        System.out.println(w.reschedule(18, 22) + " " + w.hours() + " " + w.reschedule(20, 19) + " " + w.hours() + " " + w.reschedule(0, 25) + " " + w.hours());
    }
}
  • A true 4 false 4 false 4 Correct
  • B true 4 false -1 false 25
  • C true 4 true -1 false 4
  • D false 8 false 8 false 8
Correct Answer: Option A

Invalid requests change nothing, so start < end always holds.

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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links