Menu
Topics Index
...
`

Creating Class with multiple constructors


If you need explanation Read this topic

If you need Answer Take test on this topic

Creating Class with multiple constructors
What will be the output of the following program?
class Example {
    int value1;
    int value2;
    Example() {
        value1 = 10;    value2 = 20;
        System.out.println("Inside 1st Constructor");
    }
    Example(int a) {
        value1 = a;
        System.out.println("Inside 2nd Constructor");
    }
    Example(int a, int b) {
        value1 = a;    value2 = b;
        System.out.println("Inside 3rd Constructor");
    }
    public void display() {    System.out.println(value1 + " " + value2);    }
    public static void main(String args[]) {
        Example d1 = new Example();
        Example d2 = new Example(30);
        Example d3 = new Example(30, 40);
        d1.display();    d3.display();    d2.display();
    }
}

A. Inside 1st Constructor
10 20
Inside 3rd Constructor
30 40
Inside 2nd Constructor
30 0
B. Inside 1st Constructor
Inside 3rd Constructor
Inside 2nd Constructor
10 20
30 40
30 0
C. Inside 1st Constructor
Inside 2nd Constructor
Inside 3rd Constructor
10 20
30 40
30 0
D. Inside 2nd Constructor
Inside 3rd Constructor
Inside 1st Constructor
10 20
30 40
30 0
E. Compilation Error or Runtime 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 C,becoz when we create a  object to the class appropriate constructor will be invoked,then each object has its own values.

Posted by Raviteja Daggupati    2014-02-28 08:37:14


C,while creating an object constructor is invoked..and the method is invoked while calling the function

Posted by Badam Swathi    2014-02-28 10:29:25


ans is C, bcz when objects are created its appropriate constructors are invoked then as per order of display methods appropriate object values are printed

Posted by Pooja Singh    2014-02-28 13:04:50


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

Posted by Merit Campus    2014-03-03 09:18:38


© meritcampus 2019

All Rights Reserved.

Open In App