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

Popular posts from this blog

ETE Practice Set-1

                                                                 Mock Test-1 -----------------------------------------------------------------------------------------------------------------------------   1 (a) Write a program that reads 10 integers between 1 and 100 and counts the occurrences of each. Assume the input ends with 0.                                                         ...

Practice Set: Unit-4

                                                                                                                Practice Set: Unit 4 1. What are the uses of super keyword in Java? Explain with a suitable example. 2. What are the various uses of final keyword in java? An abstract method can not be declared final. Why? 3. Differentiate between Abstract class and Interface. What do you mean by Functional interface and Lambda Expression?  ...

OLA-UBER Problem

Create two interfaces OLA and UBER both having a method public static Cab getCab() which returns the Cab object where Cab is a nested class inside OLA and UBER. Cab must have attributes cab_number and driver_name. A user rides 5 times using cabs of OLA and UBER randomly which are stored in an array. Now implement a method which should display the name of the driver and the company name i.e. OLA/UBER when the valid cab_number is entered by the user. Display "Invalid CAB details" when the cab_number entered by the user is incorrect.