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

Practice MCQs

  1. What will be the Output of the following code? class X{                  static int x = 1;                        static class Y {                              static int y = x++;                                static class Z                                                                       {                                                                                         static int z = ++y;                                                                        }                                               }                }   class MainClass{     public static void main(String[] rk)    {                             System.out.print(X.x);                                             System.out.print(X.Y.y);                             System.out.print(X.Y.Z.z);         } } A. 113 B. 123 C. 111 D. None of These Ans: A 2.  Which of the following statement is TRUE about t he following code? public class Outer  {    private i

Practice Set

1. Write a program to create a class Voter which contains attributes name, date_of_birth and voter_id and voter has a Voter_Card. Provide appropriate constructor to initialize all the attributes of the Voter but voter id must be assigned automatically only when the age of the voter is greater than or equal to 18 years. VoterCard is a nested class with attributes voter_id and Voter_name. Make sure that voter card is created only when user is a valid voter and if it is already created then must not be assigned the new voter id. 2. Write a program to define two interfaces UGC and AICTE both having a default method int getAdmission() to take the admission and an abstract method String payFee(). getAdmission() in UGC must ask the percentage in qualifying exam and if the percentage >= 60 then generate the registration number and return. getAdmission() is AICTE must ask the user to join the counseling after 5 days and display the date of counseling and return the counseling token numb

Mock Test - 1

1. Which of the following is a byte stream class? A. ObjectInputStream B. Writer C. PrintWriter D. Scanner Ans: A 2. Which exception is thrown by read() method in InputStream class? A. IOException B. InterruptedException C. FileNotFoundException D. None of These Ans: A 3. Which of the following class is subclass of FilterInputStream? A. InputStream B. ObjectOutputStream C. FileInputStream D. BufferedInputStream  Ans: D 4. Which of these class contains the print() and println() methods? A. System B. out C. PrintStream D. BUfferedOutputStream Ans: C 5. Which of these is a method to clear all the data present in output buffer without terminating the stream? A. clear() B. fflush() C. flush() D. close() Ans: C 6. Which of these classes defined in java.io and used for file-handling are abstract? (i) InputStream        (ii) PrintStream    (iii) Reader        (iv) FileInputStream A. Only i     B. i and ii    C. Only iii    D. i and iii Ans: D 7. What is the output of this program? class MyE