Menu
Topics Index
...
`

FillFromMugsIntoBuckets


If you need explanation Read this topic

If you need Answer Take test on this topic

FillFromMugsIntoBuckets
What will be the output of the following program?
public class FillFromMugsIntoBuckets {

    public static int NUMBER_OF_MUGS_PER_BUCKET = 8;
    public static int NUMBER_OF_LITERS_PER_MUG = 1.5;

    public static void main(String[] args) {
        System.out.print(encode(100) + " " + encode(50) + " " + encode(25) + " " + encode(12.5));
    }

    public static String encode(double size) {
        String result = "";
        while (size > NUMBER_OF_MUGS_PER_BUCKET * NUMBER_OF_LITERS_PER_MUG) {
            result += "B";
            size -= NUMBER_OF_MUGS_PER_BUCKET * NUMBER_OF_LITERS_PER_MUG;
        }
        while (size > NUMBER_OF_LITERS_PER_MUG) {
            result += "M";
            size -= NUMBER_OF_LITERS_PER_MUG;
        }
        result += String.format("%1.1f", size);
        return result;
    }
}

A. BBBBBBBBMM1.0 BBBBM0.5 BB1.0 B0.5
B. BBBBBBBBMM 1.00 BBBBM 0.50 BB 1.00 B 0.50
C. BBBBBBBBBMMMMMMMMM 1.00 BBBBMMMMMMMMM 1.00 BBMMMM 1.00 BMM 0.50
D. Some other output or Compilation Error
Topic: Unknown

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.

D) Compilation error.double value 1.5 is assigned to integer variable.

Posted by Priyanka Madiraju    2013-10-14 09:15:44


D.comilation error as loss of presscion occurs.as public static int liters=1.5 is not correct...it should b double type..

Posted by Asad Ahmed    2013-10-14 09:45:14


d

Posted by Sharath Kumar    2013-10-14 11:20:44


answer is C the given float is rounded off to an integer value

Posted by Shiva Goud    2013-10-14 12:37:08


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

Posted by Merit Campus    2013-10-15 09:46:16


Thank you Merit Campus Got the recharge! :)

Posted by Priyanka Madiraju    2013-10-15 10:13:32


© meritcampus 2019

All Rights Reserved.

Open In App