Menu
Question Index
...

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.principal += deposit.principal * deposit.rate * deposit.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;
    }
}


Principal before adding interest : 5000.0
Principal after adding interest : 5625.0
Principal before adding interest : 5000.0
Principal after adding interest : 5000.0
Principal before adding interest : 5000.0
Principal after adding interest : 625.0
Compilation Error - since the compound assignment operator (+=) can not be applied on member variables

Doubts

Problems

Topic: Is Java Pass by Reference or Pass by Value

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App