Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File Class question

 
Pandey Gautam
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50268
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic