Determine output:
class MyClass{
int i;
int j;
public MyClass(int i, int j){
this.i = i;
this.j = j;
}
public void call(){
System.out.print("One");
}
}
public class Test{
public static void main(String args[]){
MyClass m = new MyClass(); //line 1
m.call(); //line 2
}
}
class MyClass{
int i;
int j;
public MyClass(int i, int j){
this.i = i;
this.j = j;
}
public void call(){
System.out.print("One");
}
}
public class Test{
public static void main(String args[]){
MyClass m = new MyClass(); //line 1
m.call(); //line 2
}
}
A. One
B. Compilation fails due to an error on line 1
C. Compilation fails due to an error on line 2
D. Compilation succeed but no output.
Answer: Option B
Join The Discussion
Comments ( 5 )
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
If no constructor created,then the system check for default constructor; but here parameterized constructor defined,but calling default-so compile time error
Here the class defines a parameterized constructor due to which the default constructor is not inserted by jvm.When the new intance is created,it does not get the default constructor resulting in compilation error.
when the compiler doesn't have a default constructor, won't it create a default constructor on its own?
Sai Krishna in line one, while the object is created a compiler, will look for no argument constructor in class but as it is not defined there it will result in compile time error.
please help with this