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 d1)
    {
        Deposit d2 = new Deposit(d1.principal, d1.rate, d1.years);
        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;
    }
}


Principal before adding interest : 5000.0
Principal after adding interest : 5625.0
Principal before adding interest : 5000.0
Principal after adding interest : 5000.0
Compilation Error
Principal before adding interest : 5000.0
Principal after adding interest : 625.0

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