Menu
Question Index
...

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();
    }
}


Inside 1st Constructor
10 20
Inside 3rd Constructor
30 40
Inside 2nd Constructor
30 0
Inside 1st Constructor
Inside 3rd Constructor
Inside 2nd Constructor
10 20
30 40
30 0
Inside 1st Constructor
Inside 2nd Constructor
Inside 3rd Constructor
10 20
30 40
30 0
Inside 2nd Constructor
Inside 3rd Constructor
Inside 1st Constructor
10 20
30 40
30 0
Compilation Error or Runtime Error

Doubts

Problems

Topic: Behavior Of Java Classes Using Methods

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App