Menu
Question Index
...

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


c = 2 total = 13
c = 13 total = 6
c = 7 total = 14
c = 2 total = 13
c = 7 total = 6
c = 6 total = 14
c = 2 total = 13
c = 7 total = 39
c = 39 total = 14
c = 7 total = 6
c = 6 total = 13
c = 13 total = 14
Some other output
Compilation Error or Runtime Error

Doubts

Problems

Topic: Static Keyword In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App