Menu
Question Index
...

What will be the output of the following program?

public class Total {
    int a, b, c, total;
    Total(int a, int b, int c, int total) {
        this.a = a;
        this.b = b;
        this.c = c;
        this.total = total;
    }
    Total(int a, int b, int c) {
        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) {
        Total t1 = new Total(4, 7, 2, 4 + 7 + 2);
        Total t2 = new Total(t1.a, t1.b, t1.c);
        t1.print();
        t2.print();
        t1.print();
    }
}


C = 2 Total = 13
C = 2 Total = 13
C = 13 Total = 13
C = 2 Total = 13
C = 2 Total = 13
C = 2 Total = 13
C = 2 Total = 13
C = 13 Total = 13
C = 13 Total = 13
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