ExamVeda
Login
Home
Constructors and Methods
?

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 ) ;
       }     
}

Answer & Solution
Correct Answer: Option C
The given program creates a class Num that has a constructor with a double parameter.

In the main method, an object of the Num class is created with the value 2, which is passed as a double value.

Since 2 is treated as an integer, Java automatically converts it to a double (2.0), and the constructor prints this value.

Thus, the output of the program is 2.0.
Examveda
Question posted by Examveda
Community

Join the Discussion

14 Comments
Sameer Verma
Sameer Verma 2 years ago
In this code, the Test class extends the Num class, but the Num class does not have a default constructor (a constructor with no parameters). The Java compiler automatically inserts a call to the superclass constructor (super()) at the beginning of the Test class constructor. Since the Num class only has a constructor that takes a double argument and no default constructor, the compiler will throw an error because it cannot find a Num() constructor that takes no arguments.
Yashwant Kumar
Yashwant Kumar 4 years 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,
Saurabh Bishnoi
Saurabh Bishnoi 6 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).
Ibrahim Khan
Ibrahim Khan 6 years ago
Out put is 2.0
Because in java every number stored in double data type memory
Deepak Goyal
Deepak Goyal 6 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
Mohit Gupta
Mohit Gupta 7 years ago
incorrect answer output is 2.0
Mohammad Shahrukh
Mohammad Shahrukh 7 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();

}
}
Jay Jan
Jay Jan 8 years ago
please explain why compile time error?
Vasundhra Chib
Vasundhra Chib 8 years ago
If Test class doesnot extends Num then it then ans is 2. otherwise it is compile time error
Dipika S
Dipika S 9 years ago
please read the question carefully......if Test class is not child class of Num then it is correct...
Lakhsh Deep
Lakhsh Deep 10 years ago
ans is b
Jadeja Ravirajsinh
Jadeja Ravirajsinh 10 years ago
Compile time error. becs it has not default constrcter
Mohamed Abdul
Mohamed Abdul 10 years ago
Is this correct answer???
Krati Agarwal
Krati Agarwal 10 years ago
Incorrect option c. correct ans is 2.0 as int will automatically converted to double data type.