Menu
Topics Index
...
`

Adding behavior methods to classes


If you need explanation Read this topic

If you need Answer Take test on this topic

Adding behavior methods to classes
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();
    }
}

A. C = 2 Total = 13
C = 2 Total = 13
C = 13 Total = 13
B. C = 2 Total = 13
C = 2 Total = 13
C = 2 Total = 13
C. C = 2 Total = 13
C = 13 Total = 13
C = 13 Total = 13
D. 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 a.
here constructor with 3 and 4 arguments used.
in main class object for class total get created.

t1 obj ve 4 arguments.
t2 ve 3.

1st method t1 print called.
Total constrctor initialize all instance variable value using this keyword.

hre a 4,b7,c2. total 13
print method print c value  and total so 2 and 13.

t2 .print called with 3 arguments.
here also values of a,b,c and total initialized using this method.

so here c 2 total 13.

3rd func called here already this func executed. so c value assigned with total.

sop print c 13 and total 13

Posted by Maheshwari Natarajan    2014-10-27 06:18:47


Ans is A...here we have  4 instance variables..

in main we create an instance named with t1 by passing the 4 arguments..than memory wil allocated for the instance variables than constructor with 4 args Invoked...

in Constructor we are assigning the values to intance variables..here in constructor we have 4 local variables named similar to instance variables using this we are differentiating the Local variables and Instance variables..
this--->refers to the current Object

this.a=a
this.a refers a in the calling Object..
than it wil be..
t1--->a=4
t1--->b=7
t1--->c=2
t1--->total=13

creating another instance named with t2 by passing three values as t1.a t1.b,t1.c(4,7,2)

three arg constructor wil get invoked there invoking constrcutor with 4 arg by using this

this() which is used invoke the constructor with in the class

after initilization it wil be

t2--->a=4
t2--->b=7
t2--->c=2
t2--->total=13
here we are calling print() with instance of t1 there we are printing the value of c and total and assigning c=total after this t1 wil be

t1--->a=4
t1--->b=7
t1--->c=13
t1--->total=13
next we are calling print() with t2 than values get printed  c =total

t2--->a=4
t2--->b=7
t2--->c=2
t2--->total=13

at last we are calling print with t1 than there newly modified values get printed

Posted by Uday Kumar    2014-10-27 15:59:58


a

Posted by Shubham Bansal    2014-10-27 18:09:45


This dose is now closed and the winners are Maheshwari Natarajan,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Šâí Râm for the 'Popular Comment'. The 'lucky liker' is Maheshwari Natarajan. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-10-28 03:36:08


© meritcampus 2019

All Rights Reserved.

Open In App