• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Files.walk question in Boyarski, Selikoff

 
Ranch Hand
Posts: 153
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Boyarski, Selikoff OCP

Question 17 on page 502
Assuming the current directory is /animals/cute, which are possible results of executing the following code? (Choose all that apply.)

Files.walk(Paths.get("..").toRealPath().getParent()) // u1
.map(p -> p.toAbsolutePath().toString()) // u2
.filter(s -> s.endsWith(".java")) // u3
.collect(Collectors.toList())
.forEach(System.out::println);

A. It compiles but may throw an exception at runtime.
B. The code will not compile because of line u1.
C. The code will not compile because of line u2.
D. The code will not compile because of line u3.
E. It prints all .java files in the /animals directory tree.
F. It prints all .java files in the /animals/cute directory tree.
G. It prints all .java files in the root directory tree.

Answer:
A, G. The code compiles without issue, so B, C, and D are incorrect. The first line
actually resolves to the root path since .. and getParent()are conceptually equivalent.
Therefore, G is correct and E and F are incorrect. A is also correct since it may encountera file that it does not have access to read, which is common when trying to read an entire file system.

I think E and F are also true, since walk lists the file recursively.
Also I have tested the program and it prints .java fiels in /animals and animals/cute
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's true. E and F are a subset of G. We probably should have asked for the most general true statements or said two were true.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic