Multiple choice

What is the ouput of the following Java program?

interface A{ double pi=3.14; public double Area(int r); } class C implements A { double pi=3.1; public double Area(int r) { return(pi*r*r); } } public class test { public static void main(String[] args) { C c=new C(); System.out.println(c.Area(2));
} }

  1. 12.56

  2. 12.4

  3. 12

  4. Error in the program

  5. None of the above

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Here class C implements interface A, by analysing the program, the output is pi*r*r; Already r=2 by calling c. Area(2), we are supplying r=2; in interface pi=3.14, but in class C , pi=3.1, as pi=3.1 is local in class C, hence it hides the actual value of pi=3.4, which is drawn from interface A. Hence pi=3.1,  3.1*2*2=12.4 It prints 12.4 as ouput.