my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes File Class question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "File Class question" Watch "File Class question" New topic
Author

File Class question

Pandey Gautam
Greenhorn

Joined: Nov 05, 2008
Posts: 14
Imagine that you have directory called test1 in C drive containing some files,within which there is a directory called test2 (sub-directory of test1) containing some other set of files. Considering this, what statements are true about the following program?

package io;
import java.io.File;
import java.io.FileFilter;
public class DirLister {
public static void main(String[] args) {
String path = "C:\\test1";
list(path);
}
static void list(String path){
File fileObject = new File(path);
if (fileObject.exists()){
if (fileObject.isDirectory()){
System.out.println("Dir-->" + fileObject.getAbsolutePath());
File allFiles[] = fileObject.listFiles(new MyFileLister());
for (File aFile : allFiles){
list(aFile.getAbsolutePath());
}
}else{
System.out.println("File-->" + fileObject.getAbsolutePath());
}
}
}
}
class MyFileLister implements FileFilter{
@Override
public boolean accept(File pathname) {
return pathname.getAbsolutePath().endsWith(".bmp");
}
}
1. The program will list down all the files within the directory test1 and test2
2. The program will list down only the files in directory test1
3. The program will list down only the files in directory test2
4. The program will run infinitely.


SCJP6
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
Please post where you got that question from, and use the code button to maintain indentation.

What do you think the answer is? We don't simply provide such answers.
Pandey Gautam
Greenhorn

Joined: Nov 05, 2008
Posts: 14
11) Imagine that you have directory called test1 in C drive containing some files,
within which there is a directory called test2 (sub-directory of test1) containing
some other set of files. Considering this, what statements are true about the
following program?

1. The program will list down all the files within the directory test1 and test2
2. The program will list down only the files in directory test1
3. The program will list down only the files in directory test2
4. The program will run infinitely.

Hi Campbell,
I have chosen 2nd option but answer was given 1st one. I have run the code and I got 2nd option only. So it means I was right.

Thanks
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Hi Gautam,

Thanks for formatting the code, that makes it much easier for the rest of us!

Also, we always ask that when you post a mock question for review that you tell us where the question came from! This is important so that the question's author gets due credit, and it's also important to help us battle against illegal or pirated questions. So please, tell us where this question came from. Thanks!

Also, on the real exam, there are NO questions that involve or include annotations.

hth,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
Don't get me started about those stupid light bulbs.
 
subject: File Class question