martedì 5 novembre 2013

Q26

Given:

public class Print01 {
     public static void main(String[] args) {
          double price = 24.99;
          int quantity = 2;
          String color = "Blue";
          // insert code here. Line ***
     }
}
 
Which two statements, inserted independently at line ***, enable the program to produce the following output:


We have 002 Blue pants that cost $24.99.

A.
System.out.printf(“We have %03d %s pants that cost $%3.2f.\n”,quantity, color, price);

B.
System.out.printf(“We have$03d$s pants that cost $$3.2f.\n”,quantity, color, price);

C.
String out = String.format (“We have %03d %s pants that cost $%3.2f.\n”,quantity, color, price); System.out.println(out);

D.
String out = System.out.format(“We have %03d %s pants that cost $%3.2f.”,quantity, color, price);
System.out.println(out);

E.
System.out.format(“We have %s%spants that cost $%s.\n”,quantity, color, price); 

 

La risposta è A e C
 

A. E’ corretto.

B. No perchè $03d$s e $$3.2f.\n non sono formattate secondo la sintassi e quindi vengono stampate così come sono :
We have$03d$s pants that cost $$3.2f.

C.  E la stesso risultato di A ma usando format invece di printf

D.  No perchè si assegna ad una stringa un PrintStream infatti il codice non compila.
 

E.  E’ tentativo di usare una sintassi minimale ma è errato perché la sintassi %s stampa l'ingresso come una stringa quindi le parole risultano attaccate una all'altra e non viene "paddato" il numero 2 con gli zeri d'avanti.

We have 2Bluepants that cost $24.99.

Nessun commento:

Posta un commento