What will be the output of the following Java code?

class overload 
  {
       int x;
	double y;
       void add(int a , int b) 
       {
           x = a + b;
       }
       void add(double c , double d)
       {
           y = c + d;
       }
       overload() 
       {
           this.x = 0;
           this.y = 0;
       }        
   }    
   class Overload_methods 
   {
       public static void main(String args[])
       {
           overload obj = new overload();   
           int a = 2;
           double b = 3.2;
           obj.add(a, a);
           obj.add(b, b);
           System.out.println(obj.x + " " + obj.y);     
       }
  }

a) 6 6
b) 6.4 6.4
c) 6.4 6
d) 4 6.4

1 thought on “What will be the output of the following Java code?”

  1. d
    Explanation: For obj.add(a,a); ,the function in line number 4 gets executed and value of x is 4. For the next function call, the function in line number 7 gets executed and value of y is 6.4
    output:
    $ javac Overload_methods.java
    $ java Overload_methods
    4 6.4

Leave a Comment