Assume that x is a double that stores 0.362491. To output this value as 36%, you could use the NumberFormat class with NumberFormat nf = NumberFormat.getPercentInstance( ); Which of the following statements then would output x as 36%?


Assume that x is a double that stores 0.362491. To output this value as 36%, you could use the NumberFormat class with 

NumberFormat nf = NumberFormat.getPercentInstance( ); 

Which of the following statements then would output x as 36%?


a) System.out.println(x);

b) System.out.println(nf);

c) System.out.println(nf.x);

d) System.out.println(nf.format(x));

e) System.out.println(format(x));

Answer: d. Explanation: nf is an object and so must be passed a message to use it. The method to format a double is called format and the value to be formatted is the parameter passed to format. Therefore, the proper way to do this is nf.format(x). The answer in a will merely output 0.362491 while the answers to b, c and e are syntactically invalid.