Menu
Topics Index
...
`

Interest Calculation


If you need explanation Read this topic

If you need Answer Take test on this topic

Interest Calculation
What will be the output of the following program?
class Interest
{
    double amount, rate, interest = -1.0;
    static int numberOfYears;

    public static void main(String[] args)
    {
        Interest i = new Interest();
        System.out.println("i = " + i.interest);
        i.amount = 3000;
        i.rate = 4.25f;
        i.numberOfYears = 2;
        i.interest = 0;
        updateInterest(i);
        System.out.println("i = " + i.interest);
    }

    private static void updateInterest(Interest i)
    {
        System.out.println("i = " + i.interest);
        i.interest = i.amount * i.rate * i.numberOfYears / 100.0;
    }
}

A. i = -1.0
i = 0.0
i = 255.0
B. i = -1.0
i = 0.0
i = 0.0
C. i = -1.0
i = 0.0
i = 25500.0
D. Compilation Error
E. Throws NullPointerException
Topic: Static and Non Static Variables - Static and Non Static 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.

i'll go with option A... at 1st sysout in main .. the interest is "-1.0" as we haven't modified the default value..
in next steps at upadteInterest(i)
i is passed as 0 and at that point interest is printed as "0" in the called method

After the execution of the method i valued is changed to 255.0 and is printed in last sysout of main() method..

Posted by ?????????? ?????    2015-02-05 10:39:51


a

Posted by Shubham Bansal    2015-02-05 19:08:15


This dose is now closed and the winners are ?????????? ?????,  for 'First Correct Comment', ?????????? ?????,  for 'Best Comment' and Shubham Bansal for the 'Popular Comment'. The 'lucky liker' is Bolo Raven. 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    2015-02-06 03:32:30


© meritcampus 2019

All Rights Reserved.

Open In App