Menu
Topics Index
...
`

Constructors Using This


If you need explanation Read this topic

If you need Answer Take test on this topic

Constructors Using This
What will be the output of the following program?
public class ConstructorsUsingThis {
    public static void main(String[] args) {
        new A().print();
        new A(15).print();
        new A('A').print();
        new A(15, 34.5).print();
        new A(16, 16.0, 16.0f).print();
    }
}
class A {
    int i = 20;
    double j = 35.6;
    float k = 121.3f;
    char c = 'C';
    A() {}
    A(int i) { this.i = i; }
    A(int i, double j) { this(i); this.j = j; }
    A(int i, double j, float k) { this((int) j, i); this.k = k; }
    A(char c) { this(c, c); k = c; this.c = c; }
    public void print() {
        System.out.print("[" + i + "," + j + "," + k + "," + c + "]");
    }
}

A. [20,35.6,121.3,C][15,35.6,121.3,C][65,65.0,65.0,A][15,34.5,121.3,C][16,16.0,16.0,C]
B. [20,35.6,121.3,C][15,35.6,121.3,C][20,35.6,121.3,A][15,34.5,121.3,C][16,16.0,16.0,C]
C. Some other output
D. Compilation Error
Topic: Behavior Of Java Classes Using Methods

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is A..wen empty constr is called it wl print initial i,j,k,c values...for 2nd Obj i value wl b changed and all values get printed..3rd Obj..i,j,k values set 65 bcz 'A'=65  by calling abv Constructors using this()..n dat all values get printed..for 4th obj i n j values wl changed to values which is passed..n similar to other Obj also..

Posted by Uday Kumar    2014-04-09 15:59:05


Congratulations Uday Kumar. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-04-10 03:55:21


© meritcampus 2019

All Rights Reserved.

Open In App