Java OCA OCP Practice Question 2950

Question

Given that Germany uses a comma and the United States uses a period for the decimal point, what is the output of the following code?.

Locale.setDefault(Locale.GERMANY); 
DecimalFormat df = new DecimalFormat("#00.00##"); 
double pi = 3.141592653; 
System.out.println(df.format(pi)); 
  • A. 3,1415
  • B. 3,1416
  • C. 03,1415
  • D. 03,1416
  • E. 003,1415
  • F. 003,1416
  • G. The code does not compile.


D.

Note

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

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

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

The format also says that there must be two, three, or four digits after the decimal.

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




PreviousNext

Related