Menu
Topics Index
...
`

Daily Dose Amount


If you need explanation Read this topic

If you need Answer Take test on this topic

Daily Dose Amount
What will be the output of the following program?
public class DailyDoseAmount
{
    public static void main(String[] args)
    {
        printAmount(40, 0);
        printAmount(350, 28);
        printAmount(528, 72);
        printAmount(713, 129);
        printAmount(687, 634);
        printAmount(812, 389);
    }

    public static void printAmount(int numberOfFriends, int numberOfFollowers)
    {
        int amount = (numberOfFriends + numberOfFollowers) / 100;
        amount *= 10;
        amount = Math.max(40, amount);
        amount = Math.min(100, amount);
        System.out.println("For " + numberOfFriends + " Friends and " + numberOfFollowers + " Followers, the amount is " + amount);
    }
}

A. For 40 Friends and 0 Followers, the amount is 0
For 350 Friends and 28 Followers, the amount is 30
For 528 Friends and 72 Followers, the amount is 40
For 713 Friends and 129 Followers, the amount is 40
For 687 Friends and 634 Followers, the amount is 40
For 812 Friends and 389 Followers, the amount is 40
B. For 40 Friends and 0 Followers, the amount is 40
For 350 Friends and 28 Followers, the amount is 40
For 528 Friends and 72 Followers, the amount is 60
For 713 Friends and 129 Followers, the amount is 80
For 687 Friends and 634 Followers, the amount is 100
For 812 Friends and 389 Followers, the amount is 100
C. For 40 Friends and 0 Followers, the amount is 100
For 350 Friends and 28 Followers, the amount is 100
For 528 Friends and 72 Followers, the amount is 100
For 713 Friends and 129 Followers, the amount is 100
For 687 Friends and 634 Followers, the amount is 130
For 812 Friends and 389 Followers, the amount is 120
D. Compilation Error
Topic: Java Methods - Parameter Passing And Scope

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 B ....Here in each and every call to method  printAmount,it does the following steps  
For eg for the first method call  
1)amount = (40 + 0)/100 = 0 (since 0.4 will be rounded off to 0 since int neglects the decimal part)
2)amount *= 10; after this step amount is 0
3)amount = Math.max(40,amount) (max method returns the maximum of two numbers) amount is 40 since 40>0
4) amount = Math.min(100,amount)(min method returns the minimum of two numbers) amount is 40 since 40<100
5)we are printing the amount and other variables in the final step

the same steps are folllowed for the remaining method calls

Posted by Shashanka Mogaliraju    2014-11-14 05:38:44


Ans is B..here we have printAmount() which is receiving 2 Params..here it is static than we can call these method with out creating the object also..

initially we are passing 40,0..here
we have + ,/ and() so () wil get more priority than 40+0=40 than it wil be 40/100..here output is 0 Bcz Int/int is Than result is int

so here amount is 0...amount*10 is also 0...
next we are passing (40,0) max than it will return the 40 and stored in the amount next we are passing (100,40) to min than it will return 40 than amont is 40

2 )next we are passing 350,28
(350+28)=378
378/100=3....( int / int = int)

amount*=10 than amount =30

next we are passing (40,30) it will return max num among given nums..than it wil return 40 and stored in amount

next we are passing (100,40) to min than it will return min 40 than amount is 40...

similar process for the Remaining function calls...

Posted by Uday Kumar    2014-11-14 13:05:54


b

Posted by Shubham Bansal    2014-11-14 14:29:55


This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Shashanka Mogaliraju, Uday Kumar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Gaurav Bansal. 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-11-17 01:11:48


© meritcampus 2019

All Rights Reserved.

Open In App