JAVA Programming :: Packages

Home > Technical Aptitude > JAVA Programming > Packages > General Questions

NA
SHSTTON
2
Solv. Corr.
9
Solv. In. Corr.
11
Attempted
0 M:0 S
Avg. Time

1 / 1

What is the output for the below code?
public class D {
int i;
int j;
public D(int i,int j){
this.i=i;
this.j=j;
}
public void printName() {
System.out.println("Name-D");
}
}
public class Test{                                                                                                                                                            13. public static void main (String[] args){
D d = new D();
d.printName();
}
}

AName-D

BCompilation fails due to an error on lines 3

CCompilation fails due to an error on lines 4

DCompilation succeed but no output

Answer: Option B

Explanation:

Since there is already a constructor in this class (public D(int i,int j)), the compiler won't supply a default constructor. If you want a no-argument constructor to overload the with arguments version you already have, you have to define it by yourself. The constructor D() is undefined in class D. If you define explicit constructor then default constructor will not be available. You have to define explicitly like public D(){ } then the above code will work. If no constructor into your class , a default constructor will be automatically generated by the compiler.

Workspace