Examveda
Examveda

What is the output of the above program?
class Num  {
      Num(double x ){
              System.out.println( x ) ;
      }   
}
public class  Test  extends  Num   {
       public static void main(String[] args){
                  Num num = new Num( 2 ) ;
       }     
}

A. 0

B. 2.0

C. Compile time error

D. None of the above

Answer: Option C


This Question Belongs to Java Program >> Constructors And Methods

Join The Discussion

Comments ( 13 )

  1. Yashwant Kumar
    Yashwant Kumar :
    1 year ago

    As there is no parametrised constructor which is calling super(x) from the Test class, it is giving us error. We need to explicitely declare a super(x) in order to get 2.0 as answer,

  2. Saurabh Bishnoi
    Saurabh Bishnoi :
    3 years ago

    C is correct Answer
    The compile-time legality of a casting conversion is as follows:
    1. An expression of a primitive type may undergo casting conversion to another primitive type, by an identity
    conversion (if the types are the same), or by a widening primitive conversion, or by a narrowing primitive
    conversion, or by a widening and narrowing primitive conversion.
    2. An expression of a primitive type may undergo casting conversion to a reference type without error, by
    boxing conversion.
    3. An expression of a reference type may undergo casting conversion to a primitive type without error, by unboxing conversion.

    thus you have to manually typecast value before passing to the constructor.
    Ex :
    1. Num num = new Num((double) 2); // this is right as passing primitive type to construtor accepting reference
    type of same type. So you to do typecasting manually.
    2. Num num = new Num(new Double(2)); // is right by Rule 2.

    3. Num num = new Num(2); this is wrong as By Rule 1: as you are passing an integer value (primitive type ) to the constructor which accepts Double (reference type of different type).

  3. Ibrahim Khan
    Ibrahim Khan :
    3 years ago

    Out put is 2.0
    Because in java every number stored in double data type memory

  4. Deepak Goyal
    Deepak Goyal :
    4 years ago

    main() class must be parent class but in this example test class inherits num class which is wrong
    but if we do changes like num class inherit test class that will become right and will output 2.0

  5. Mohit Gupta
    Mohit Gupta :
    4 years ago

    incorrect answer output is 2.0

  6. Mohammad Shahrukh
    Mohammad Shahrukh :
    5 years ago

    You cannat call the constructor of base class directly without super key word.

    It will work:
    class Num{
    Num(double x)
    {
    System.out.println("Hi");
    }
    Num(int x)
    {
    System.out.println("Hi2");
    }

    }
    class Test extends Num {
    Test(){
    super(2.0);
    }
    public static void main(String args[]) {
    Test b =new Test();

    }
    }

  7. Jay Jan
    Jay Jan :
    5 years ago

    please explain why compile time error?

  8. Vasundhra Chib
    Vasundhra Chib :
    6 years ago

    If Test class doesnot extends Num then it then ans is 2. otherwise it is compile time error

  9. Dipika S
    Dipika S :
    6 years ago

    please read the question carefully......if Test class is not child class of Num then it is correct...

  10. Lakhsh Deep
    Lakhsh Deep :
    7 years ago

    ans is b

  11. Jadeja Ravirajsinh
    Jadeja Ravirajsinh :
    7 years ago

    Compile time error. becs it has not default constrcter

  12. Mohamed Abdul
    Mohamed Abdul :
    7 years ago

    Is this correct answer???

  13. Krati Agarwal
    Krati Agarwal :
    8 years ago

    Incorrect option c. correct ans is 2.0 as int will automatically converted to double data type.

Related Questions on Constructors and Methods

What is a constructor in Java?

A. A special method to create instances of classes

B. A method used for mathematical calculations

C. A method to perform string manipulations

D. An exception handling mechanism