venerdì 13 settembre 2013

Q4

Given:
import java.io.File;
import java.nio.file.Path;
 
public class Test12 {
       static String displayDetails(String path, int location) {
             Path p = new File(path).toPath();
             String name = p.getName(location).toString();
             return name;
       }
 
       public static void main(String[] args) {
             String path = "project//doc//index.html";
             String result = displayDetails(path, 2);
             System.out.print(result);
       }
}
What is the result?
A.
doc


B.
index.html


C.
an IllegalArgumentException is thrown at runtime.


D.
An InvalidPthException is thrown at runtime.


E.
Compilation fails.


Risposta B.
Si crea un File con ingresso "project//doc//index.html". Poi dal File si estrae il path. Infine si stampa il 3° elemento del percorso :
p.getName(0).toString()) à project
p.getName(1).toString()) à doc
p.getName(2).toString()) à index.html

Nessun commento:

Posta un commento