Java OCA OCP Practice Question 2604

Question

Consider the following program:

import java.util.*;

class Main {/*w w  w .j a va2 s.  c  o  m*/
        public static void main(String []args) {
                Formatter formatter = new Formatter();
                Calendar calendar = Calendar.getInstance(Locale.US);
                calendar.set(/* year =*/ 2012, /* month = */ Calendar.FEBRUARY,
                /* date = */ 1);
                formatter.format("%tY/%<tB/%<td", calendar);
                System.out.println(formatter);
        }
}

Which one of the following options correctly describes the behavior of this program?

  • a) The program throws a MissingFormatArgumentException.
  • b) The program throws an UnknownFormatConversionException.
  • c) The program throws an IllegalFormatConversionException.
  • d) The program prints the following: 12/February/01.


d)

Note

The < symbol in a format string supports relative index with which you can reuse the argument matched by the previous format specifier.

The equivalent example of passing arguments explicitly is the following:.

formatter.format("%tY/%tB/%td", calendar, calendar, calendar);

The program used a short form by reusing the argument passed to the previous format specifier:

formatter.format("%tY/%<tB/%<td", calendar);



PreviousNext

Related