Skip to main content
What will be the output of the following program?
class Testing
{
   public static void main(String ar[])
    {
       Testing t = new Testing();
       t.show(1);
    }

   void show(byte b) {System.out.println("Byte");}
  
   void show(short s) {System.out.println("Short");}

   void show(int i) {System.out.println("Int");}

   void show(long l) {System.out.println("Long");}
  
}

(a) What will be the output when we remove the 
void show(int i)
     {System.out.println("Int");}

from the class.

(b) What will be the output when we remove last two methods from the class?

Explain the output.

Comments

  1. Output Will be 1 if Nothing is Removed

    a.) long
    b.)short

    ReplyDelete
    Replies
    1. First answer is correct but the second one is not correct.
      The main thing is to understand the concept. Why "Long" will be the output of the first (a) part.

      Delete
    2. And output will never be 1 in any case in the above program.

      Delete
  2. The Output of the above program is:

    Int


    (a) Long
    (b) Compile Time Error

    ReplyDelete
    Replies
    1. Now you have to identify the concept behind it.

      Delete
    2. How Compile Time Error Sir????

      Delete
    3. Two concepts will be applied to this question:
      1. All the numbers are by default integer type.
      2. Upcasting is done automatically in java but downcasting must be done explicitly.

      We are passing a number which is by default integer. If we have a method with long type argument then integer will be upcasted to long but if we have only short and byte type methods then downcasting will not be done. Therefore the error will be at compile time saying that no suitable method found.

      Delete
  3. Good Post! Thank you so much for sharing the nice post, it was so good to read and useful to improve
    Java Training in Electronic City

    ReplyDelete

Post a Comment