Menu
Topics Index
...
`

Class with more Methods


If you need explanation Read this topic

If you need Answer Take test on this topic

Class with more Methods
What will be the output of the following program?
public class ComplexType {
    private final int a;
    int b, total;
    protected static int c;
    ComplexType(int a, int b, int c, int total) {
        this.a = a;
        this.b = b;
        ComplexType.c = c;
        this.total = total;
    }
    ComplexType(int a, int b, int c) {
        this(a, b, c, a + c + 3);
    }
    ComplexType(int a, int b) {
        this(a, b, c, a + b + c);
    }
    public void print() {
        System.out.println("c = " + c + " total = " + total);
        c = total;
    }
    public static void main(String[] args) {
        ComplexType t1 = new ComplexType(4, 7, 2, 4 + 7 + 2);
        t1.print();
        ComplexType t3 = new ComplexType(ComplexType.c, ComplexType.c);
        ComplexType t2 = new ComplexType(t1.a, ComplexType.c, t1.b);
        t3.print();
        t2.print();
    }
}

A. c = 2 total = 13
c = 13 total = 6
c = 7 total = 14
B. c = 2 total = 13
c = 7 total = 6
c = 6 total = 14
C. c = 2 total = 13
c = 7 total = 39
c = 39 total = 14
D. c = 7 total = 6
c = 6 total = 13
c = 13 total = 14
E. Some other output
F. Compilation Error or Runtime Error
Topic: Static Keyword In Java

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. For the first object first constructor is invoked and the value of c is 2 and total is 13.This total is assigned to c. Now when second object is created, constructor with 2 arguments is invoked, which again invokes first constructor, and so on

Posted by Nikhil Sulibhavi    2014-04-17 13:15:24


Ans A: t1 object will give total as 13...dis 13 wil store as c value in print method....t3 objext will print that C value in total as where as complex type.c is 2....

Posted by Ashok Reddi    2014-04-17 13:25:18


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

Posted by Merit Campus    2014-04-18 04:19:08


© meritcampus 2019

All Rights Reserved.

Open In App