Java SE OCP. Random questions

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Dir d doesn't exist; Path p=Paths.get(d); Is Exception? Files.isDirectory(p); Files.isRegularFile(p); Files.isSymbolicLink(p) ?

No exception will be thrown by: Files.isDirectory(p); Files.isRegularFile(p); Files.isSymbolicLink(p);

Will Files.copy(sourceDir,destDir) copy all files and subdirectories within the sourceDir?

No. To copy the contents of a directory, you would need to create a function to traverse the directory and copy each file and subdirectory individually.

Files.size(p) when p doesn't exist?

NoSuchFileException

Current working directory is /zoo, The path /zoo/turkey does not exist. Path p1 = Paths.get("turkey"); Path p2 = Paths.get("/zoo/turkey"); What Files.isSameFile(p1, p2) ?

NoSuchFileException. Since the path1 and path2 are not equals, this comparison will return false, forcing isSameFile() to check for the existence of both paths in the file system.

Path path = Paths.get("/home/vez"); Files.find( path, 0, (p, a) -> a.isSymbolicLink() ).forEach(System.out::println); There are 4 symbolic links inside /home/vez. What will be printed?

Nothing. ( the depth parameter specified as the second argument to find() is 0, meaning the only record that will be searched is the top-level directory. Since we know that the top directory is regular and not a symbolic link, no other paths will be visited and nothing will be printed. )

Path p= Paths.get("/home/vez/tmp/pathA"); Files.walk(p, 0) .forEach( System.out::println ); // pathA is not empty, full of files. What will be printed?

Only current path itself: /home/vez/tmp/pathA

How to get Path out from legacy java.io.File class ?

Path path = file.toPath();

Linux. What Files.isExecutable( Paths.get("/seal/baby.png") ) ? (/seal/baby.png exists)

Depends of the baby.png attribute. Can be true of false.

p1 is symbolic link. What Files.delete(p1)?

The symbolic link will be deleted, not the target of the link.

Is this correct? Will Files.copy(s,d) copying files and directories will traverse symbolic links.

YES

path=Paths.get("/animals/cute") exists and not empty. Can Files.walk(path).toRealPath().getParent()) throw runtime exception?

Yes. it may encounter a file that it does not have access to read.

What class Files.walk(path) method returns?

Stream<Path>

Windows. p1=C:\path1; // empty directory. p2=D:\path2; // empty directory. What Files.move(p1,p2)?

Successfully moved D:\path2\path1

What if invoke same method twice? Files.createDirectories(p); Files.createDirectories(p); // Exception?

Directory created, no exception.

p is not empty directory. Files.delete(p) // What output?

DirectoryNotEmptyException

path is not empty directory. Files.delete(path); Will the directory be deleted?

DirectoryNotEmptyException

Dir "/home/vez/tmp" exists and not empty. Path p = Paths.get("/home/vez/tmp"); Files.deleteIfExists(p); System.out.println( Files.exists(p) );

DirectoryNotEmptyException (deleteIfExists raises exception).

Windows. p1=C:\path1; is NOT empty dir p2=D:\path2. What Files.move(p1,p2)?

DirectoryNotEmptyException. While moving an empty directory across a drive is supported, moving a non-empty directory across a drive will throw an exception.

What is Files.readAllLines(path) .filter(s -> s.length()>2) .forEach(System.out::println) ?

Doesn't compile. readAllLines() returns Collection. .filter() operation cannot be applied to a Collection without first converting it to a Stream using the stream() method.

How to get legacy java.io.File class from Path?

File file = path.toFile();

File exists: Path file = Paths.get("monkey.txt"); Dir exists: Path dir = Paths.get("/animal"); What will be (dir or file) after Files.move(file, dir); ?

FileAlreadyExistsException

File exists: Path file = Paths.get("monkey.txt"); Dir exists: Path dir = Paths.get("/animal"); What will be (dir or file) after Files.move(file, dir); ?

FileAlreadyExistsException

Dir /bison/field exists. What is: Path p=Paths.get("/bison/field"); Files.createDirectory(p) ?

FileAlreadyExistsException. But nothing happend in case of createDirectorIES

/pathA/food.source → /pathB/food.txt Path p1 = Paths.get("/pathA/food.source"); Path p2 = Paths.get("/bamboo.txt")); Files.copy(p1,p2); Files.copy(p1,p2); Files.exists(p2);

FileAlreadyExistsException. First Files.copy(..); create p2, second invocation raises exception.

How to obtain FileSystem object?

FileSystem fileSystem = FileSystems.getDefault(); ( The FileSystem class has a protected constructor, so we use the plural FileSystems factory class to obtain an instance of FileSystem.)

Two ways to obtain to obtain an instance of a FileSystem object?

FileSystems.getDefault(); path.getFileSystem();

Files.getLastModifiedTime(path); // Which Class is returned this method?

FileTime

How to copy files and copy attributes?

Files.copy(path1,path2,COPY_ATTRIBUTES);

How to copy symbolic links without traverse symbolic links?

Files.copy(path1,path2,NOFOLLOW_LINKS);

How to copy files with replace existing?

Files.copy(path1,path2,REPLACE_EXISTING);

Method to create directory in Files.class

Files.createDirectory(path). But it was File.mkdir(string);

What difference between Files.readAllLines() vs. Files.lines() ?

Files.lines() return Stream allowing to read huge file. readAllLines() reads the entire file into memory.

Which NIO.2 method is most similar to the legacy java.io.File.listFiles() method?

Files.list() ( is the closest match since it always reads only a single directory. But it returns Stream<path> instead of files )

Path p = Paths.get("/aaa/bbb/ccc/ddd"); What p.getName(1).toAbsolutePath() ?

Full_path_to_current_dir/bbb path.getName()-returns relative path with a name element of this path (=bbb). toAbsolutePath() make it from the current dir.

Path path = Paths.get("/mammal/carnivore/raccoon.image"); What is path.subpath(1,2) ?

carnivore

new URI(String) does throw a...?

checked URISyntaxException.

Which package Files.class belong to?

java.NIO.file

Path path = Paths.get("/mammal/carnivore/raccoon.image"); What is path.subpath(1,1) ?

java.lang.IllegalArgumentException

Path path = Paths.get("/mammal/carnivore/raccoon.image"); What is path.subpath(2,4) ?

java.lang.IllegalArgumentException (because of 4)

Path p1 = Paths.get("/flamingo/tail.data"); Path p2 = Paths.get("/cardinal/tail.data"); What Files.isSameFile(p1,p2); if paths do not exist

java.nio.file.NoSuchFileException

path is symbolic link. Will the Files.walk(path) traverse through link?

no (by default. But you can use FOLLOW_LINK option as vararg to the walk() if you need to).

Object a,b not null; a.equals(b) => false; Does it mean: a.hashCode() != b.hashCode() ?

no. .equals() uses more instance variables than hashCode().

What exception thrown by Files.exists(Path)?

none. exists(path) returns true/false, but not exception.

BasicFileAttributes atts = Files.readAttributes(path, BasicFileAttributes.class); att.setTimes( FileTime.fromMills(123) ) ?

not compile. readAttributes return READ_ONLY object. For modification use View: Files.getFileAttributeView(Path,Class<V>)

BaseFileAttributeView v = Files.getFileAttributeView(path, FileAttributeView.class); v.readAttributes().isArchive()?v.readAttributes().isHidden()? v.readAttributes().isReadOnly()? v.readAttributes().isSystem()?

not compile. BaseFileAttributeView doesn't have a .readAttributes(). Only DosFileAttributeView has.

path=Paths.get("relative/file.txt"). What value is path.getRoot() ?

null. getRoot(), returns the root element for the Path object or null if the Path object is relative (~ with no "/")

Stream<Path> p = Files.list(path); What depth?

only 1. Analogue to Files.walk(path,1) and old io.File.listFiles()

Object a,b not null; a.equals(b) => true; Does it mean: a.hashCode()==b.hashCode() ?

yes.

Path p1 = Paths.get("monkey.txt"); Path p2 = Paths.get("/animal"); Will it copy attributes from p1 to p2 attributes after Files.move(p1, p2); (COPY_ATTRIBUTES flag is not set) ?

yes. Moving always preserves the metadata even if the COPY_ATTRIBUTES flag is not set.

p="/kang" is symbolic link. It exists and point to ->"/mammal/kangaroo". Files.createDirectory(p.resolve("joey")); Will path="/kang/joey" be reachable ?

yes. If the code creates a directory, it will be reachable at /kang/joey.

Path path = Paths.get(".").normalize(); What values will be: path ? path.getNameCount() ?

"" 1 getNameCount() calculates starting from current dir.

Stream<Path> stream = Files.find(path, 10,(p,a) -> p.toString().endsWith(".java") ). What mean 10 ? What mean a in (p,A)?

10-depth value, a-BasicFileAttributes, need because File.find(path, depth, BiPredicate)

Paths.get(".").toRealPath()

Absolute path to current working directory

path.getFileSystem() .getUserPrincipalLookupService().lookupPrincipalByName("jane") What if "jane" doesn't exists in the system?

UserPrincipalNotFoundException

Windows: Path path = Paths.get("/").normalize(); What values will be: path ? path.getNameCount() ?

"\" 0 This is strange behavior, but it proved.

Path p1 = Paths.get("fish.txt"); Path p2 = Paths.get("./cage/birds.txt"); What is p2.relativize(p1)?

..\..\..\fish.txt

Path p1 = Paths.get("/./fish.txt"); not exist what p1.getRoot();

/ Path object is not a file but a representation of a location within the file system. In this manner, most operations available in the Path and Paths classes can be accomplished regardless of whether the underlying file that the Path object references actually exists.

Path p = Paths.get("./a/b/c"); What Files.createDirectories(p); ?

/a/b/c will be created in current dir

Path p1 = Paths.get("/cats/../panther"); Path p2 = Paths.get("food"); p1.resolve(p2) ?

/cats/../panther/food

Symbolic link /zebra/food.source → /horse/food.txt; Current working directory is /horse/schedule. What is Paths.get("/zebra/food.source").toRealPath()?

/horse/food.txt

p1=Paths.get("/pets/../cat.txt"); p2=Paths.get("./dog.txt"); What p2.resolve(p1); ?

/pets/../cat.txt. ( calling resolve() with an absolute path as a parameter returns the absolute path )

Path path1 = Paths.get("/turkey/food"); Path path2 = Paths.get("/tiger/cage"); path1.resolve(path2)?

/tiger/cage. (Since the input parameter path2 is an absolute path, it replaces the path1 value)

Path path1 = Paths.get("/turkey/food"); Path path2 = Paths.get("tiger/cage"); path1.resolve(path2)?

/turkey/food/tiger/cage. (Since the input parameter path2 is an relative path, it was added to path1)

When Files.copy(p1,p2) throws the checked IOException?

When the file or directory does not exist or cannot be read.

System does not support atomic moves. Files.move(p1,p2,ATOMIC_MOVE) // What output?

AtomicMoveNotSupportedException

class Parent() class A extends Parent {} class B extends Parent {} A a = new A(); What (a instanceof B) ?

CompilationError. Object a can never be an instance of B.

What Files.isHidden(p); if p doesn't exist ?

IOException

Dir /bison/field exists. What is ?: Files.createDirectory(Paths.get("/bison/field/par/child"))

IOException (because /bison/field/par doesn't exist)

Path p1 = Paths.get("/primate/chimp"); Path p2 = Paths.get("chimp/bananas.txt"); path1.relativize(path2);

IllegalArgumentException (because p1 - absolute and p4 - relative)

Windows. Path p1 = Paths.get("c:\\primate\\chimpanzee"); Path p2 = Paths.get("d:\\storage\\bananas.txt"); p1.relativize(p2);

IllegalArgumentException. Because p1 and p2 belongs to different drive/root

Path path = Paths.get("/"); System.out.println( path.getName(0) ); ?

IllegalArgumentException. path.getName(0) for path="/" throws IllegalArgumentException. (But it returns "value" for path="/value".)

Files.walk(path). What maximum directory depth will be reached?

Integer.MAX_VALUE

Path path doesn't exists. What is path.toRealPath()?

Throw a checked exception if the path is not available.

Path p is symbolic link to existing file. What Files.isRegularFile(p)?

True. If the symbolic link points to a real file or directory, Java will perform the check on the target of the symbolic link.

How to connect to remote file system with FileSystem object?

URI uri = new URI("http://www.selikoff.net"); fileSystem = FileSystems.getFileSystem(uri)); Path path = fileSystem.getPath("duck.txt");

What class returned by Files.getOwner(p) ?

UserPrincipal

Path path = Paths.get("/zoo/animals/bear/koala/food.txt"); What is path.subpath(1,3) ?

animals/bear

path = Paths.get(".").normalize(); What is sout(path) ?

empty string. The normalize() method does not convert a relative path into an absolute path; therefore, the path value after the first line is just the current directory symbol.

p1=Paths.get("/flamingo/tail.data"); p2=Paths.get("/cardinal/tail.data"); Files.isSameFile(p1,p2); (paths exist)

false

path=Paths.get("relative/file.txt"). What value is path.isAbsolute() ?

false

Path path1 = Paths.get("monkey.txt"); Path path2 = Paths.get("mule.png"); Files.copy(path1,path2,COPY_ATTRIBUTES); What is Files.isSameFile(path1, path2) ?

false. (isSameFile() returns true only if the files pointed to in the file system are the same, without regard to the file contents.)

interface Parent{} interface A extends Parent {} interface B extends Parent {} A a = new MyClassImplA(); What (a instanceof B) ?

false. instanceof works with interfaces even object doesn't implement it.

Paths.get("/stripes/zebra.exe").isAbsolute() for Windows OS ?

false. Because the pathstarts with drive letter on Windows.

Path p = Paths.get("doesn't_exists"); Files.isReadable(p) Files.isExecutable(p) ?

false. These methods don't throw exception

Object obj=null; What is (obj instanceof Object)?

false. null is not an Object

File exists: Path file = Paths.get("monkey.txt"); Dir not exists: Path dir = Paths.get("/animal"); What will be (dir or file) after Files.move(file, dir); ?

file

FileSystems .getDefault() .getPath("puma.txt"); What?

relative path to puma.txt

Linux: Path p1 = Paths.get("E:\\data"); Path p2 = Paths.get("E:\\user\\home"); Path relative=p1.relativize(p2); ? Path resolvedRelative=p1.resolve(relative); ? Path normilizedResolvedRelative = resolvedRelative.normalize(); ?

relativePath=../E:\user\home resolvedRelativePath=E:\data/../E:\user\home normilizedResolvedRelativePath=E:\user\home

Files.size(p) when p is directory?

result system dependent and undefined.

What Files.lines(path) return?

return a Stream<String> - file content

Files.deleteIfExisted(path) what will be if path is symbolic list to the existed directory?

symbolic link deleted

how to convert a Path instance back to a URI instance?

the Path interface also contains a reciprocal method toUri().

Files.isDirectory(path) if the path symbolic link to directory?

true

Files.newBufferedReader(Path,Charset) , reads the file specified at the Path location using a java.io.BufferedReader object?

true

Path p1=Paths.get("/zebra/food.source") is Symbolic link to: Path p2=Paths.get("/horse/food.txt") exists; What is Files.isSameFile(p1,p2) ?

true

Path path=Paths.get("/zebra/food") doesn't exist. What Files.isSameFile(path, path); ?

true

The Files.readAllLines() method reads all of the lines of a text file and returns the results as an ordered List of String values.

true

Windows. What Files.isReadable(p) if p exists?

true

path is symbolic link to existing directory. What is Files.isDirectory(path)?

true

path is symbolic link to existing file. What Files.isSymbolicLink(path)?

true


संबंधित स्टडी सेट्स

Chapter 16: Anti-Inflammatory, Antiarthritis, and Related Agents

View Set

Personal Finance Chapter 14: starting early: retirement and estate planning

View Set

Chapter 9: Quantitative Research Design

View Set

Chapter 25 : Convertible Securities (Theories)

View Set

Chapter 40: Terrorism Response and Disaster Management: Practice Questions

View Set

anatomy and physiology chapter 6 test banks

View Set