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 ) ;
}
}
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
Join The Discussion
Comments ( 13 )
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
In Java, which method is automatically called when an object is created?
A. start()
B. main()
C. init()
D. constructor()
What is method overloading in Java?
A. Defining multiple methods with the same name in the same class
B. Calling methods from another class
C. Using methods to load data from a file
D. Running methods in parallel threads
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,
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).
Out put is 2.0
Because in java every number stored in double data type memory
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
incorrect answer output is 2.0
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();
}
}
please explain why compile time error?
If Test class doesnot extends Num then it then ans is 2. otherwise it is compile time error
please read the question carefully......if Test class is not child class of Num then it is correct...
ans is b
Compile time error. becs it has not default constrcter
Is this correct answer???
Incorrect option c. correct ans is 2.0 as int will automatically converted to double data type.