Menu
Topics Index
...
`

Add Interest244


If you need explanation Read this topic

If you need Answer Take test on this topic

Add Interest244
What will be the output of the following program?
class AddInterest
{
    public static void main(String s[])
    {
        Deposit d1 = new Deposit(5000.0, 6.25, 2.0);
        System.out.println("Principal before adding interest : " + d1.principal);
        addInterest(d1);
        System.out.println("Principal after adding interest : " + d1.principal);
    }

    public static void addInterest(Deposit deposit)
    {
        Deposit d2 = deposit;
        d2.principal += d2.principal * d2.rate * d2.years / 100.0;
    }
}

class Deposit
{
    double principal;
    double rate;
    double years;

    Deposit(double principal, double rate, double years)
    {
        this.principal = principal;
        this.rate = rate;
        this.years = years;
    }
}

A. Principal before adding interest : 5000.0
Principal after adding interest : 5625.0
B. Principal before adding interest : 5000.0
Principal after adding interest : 5000.0
C. Compilation Error
D. Principal before adding interest : 5000.0
Principal after adding interest : 625.0
Topic: Is Java Pass by Reference or Pass by Value

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.

Answer is A.In the function call we are passing reference of the object.Called function though it assigns the reference to new variable, it actually modifies the actual variable i.e d1 object

Posted by Shashanka Mogaliraju    2014-09-05 12:33:33


ans is A

Posted by Raju VarshnEy    2014-09-05 13:13:07


Anser is A .....as all the refrence variable in class deposit(principle,rate,interest) are by default friendly...and by making the refrence variable of class deposit which is d1 we can access all the variables of class deposit with d1......now the reference of d1 is passed into the function argument which is deposit.....now refrence of d1 comes in deposit and from deposit it comes in d2 refrence variable.....now d2 refrence variable points all the values of variable which is point by d1.....now with d2 all the values are modified of variables.....and (principle,interest,rate)...

Posted by Gaurav Kandhari    2014-09-05 14:17:48


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

Posted by Merit Campus    2014-09-08 04:48:48


© meritcampus 2019

All Rights Reserved.

Open In App