Java OCA OCP Practice Question 2932

Question

What is the output of the following code?

import java.text.DecimalFormat;

public class Main {

   public static void main(String[] args) throws Exception {
      DecimalFormat df = new DecimalFormat("#,000.0#"); 
      double pi = 3.141592653; 
      System.out.println(df.format(pi)); 

   }//from   ww w  .j a  v  a2s .  c o  m
}
  • A. 3.141592653
  • B. 0,003.14
  • C. ,003.1
  • D. 003.14
  • E. 00.04
  • F. The code does not compile.
  • G. None of the above


D.

Note

DecimalFormat uses a constructor rather than a factory.

The 0 means a mandatory position and the # means an optional position.

The format says that there must be either three or four digits before the decimal.

Since we only have one, Java uses the smaller number.

The format also says that there must be either one or two digits after the decimal.

Since we have many digits, Java uses the larger number.




PreviousNext

Related