Java OCA OCP Practice Question 2549

Question

Consider the following program and predict the output:

public class Main {
     public static void main(String []args) {
             int a = 7, b = 10;
             System.out.printf("no:%2$s and %1$s", a, b);
             System.out.printf("\nno:2$s and 1$s", a, b);
     }
}
a)      no:10 and 7/*from   w  w w  .  j a v a 2  s.co m*/
     no:2$s and 1$s

b)      no:7 and 10
     no:2$s and 1$s

c)      no:10 and 7
     no:10 and 7

d)      no:7 and 10
     no:7 and 10

e)      This program will result in compiler error(s).


a)

Note

The format specifier string %$s indicates that you want to re-order the input values.

A number (integer) sandwiched between a % and a $ symbol is used to re-order the input values; the number indicates which input variable you want to put here.

In %2$s it indicates that you want to put the second argument.

Similarly, %1$s indicates that you want to put the first argument.




PreviousNext

Related