What is output by the following Java program?

1
2
3
4
5
6
7
class Compute {
    static int compute() { return 42; }
    static int compute(int i) { return i+1; }
    public static void main(String[] args) {
        System.out.println(compute(compute(0)));
    }
}

Select one: a. 1 b. 42 c. 2 d. 0 e. 43

The inner call to "compute" has an argument, so it us es the second method definition, which returns 0+1, or 1. This 1 becomes the argument to the outer ca ll, which then returns 1+1, or 2. The first definition of "compute" is never used. The correct answer is: 2

Related Posts

0 Comments

12345

    00