Hi ,
We can find below type of questions in interviews. Please write the out put for below program.
public class ConstructorExample1 {
static int j;
public ConstructorExample1() {
System.out.println("default");
}
public ConstructorExample1(int i) {
this();//It will call default constructor
System.out.println("p1");
}
public ConstructorExample1(String i, int j) {
// ConstructorExample1();
this(j);//it will call current class constructor which has one integer as variable
System.out.println("p2");
}
public static void getData(){
j = 2*7;
System.out.println(j);
}
public void getData1(){
j = 2*7;
System.out.println(j);
}
public static void main(String a[]){
// ConstructorExample1 constructorExample1 = new ConstructorExample1();
// ConstructorExample1 constructorExample11 = new ConstructorExample1(1);
//Below line of code will print all constructor outputs
ConstructorExample1 constructorExample111 = new ConstructorExample1("bal", 2);
}
}
Below are the options which we will get in exam: Correct answer is option d
a.) compilation error
b) default
p1
c) runtime error
d) default
p1
p2
e) p2
We can find below type of questions in interviews. Please write the out put for below program.
public class ConstructorExample1 {
static int j;
public ConstructorExample1() {
System.out.println("default");
}
public ConstructorExample1(int i) {
this();//It will call default constructor
System.out.println("p1");
}
public ConstructorExample1(String i, int j) {
// ConstructorExample1();
this(j);//it will call current class constructor which has one integer as variable
System.out.println("p2");
}
public static void getData(){
j = 2*7;
System.out.println(j);
}
public void getData1(){
j = 2*7;
System.out.println(j);
}
public static void main(String a[]){
// ConstructorExample1 constructorExample1 = new ConstructorExample1();
// ConstructorExample1 constructorExample11 = new ConstructorExample1(1);
//Below line of code will print all constructor outputs
ConstructorExample1 constructorExample111 = new ConstructorExample1("bal", 2);
}
}
Below are the options which we will get in exam: Correct answer is option d
a.) compilation error
b) default
p1
c) runtime error
d) default
p1
p2
e) p2
No comments:
Post a Comment