Java OCA OCP Practice Question 2617

Question

Given the following directory structure:

test -|  
      | - Finder.class  
      | - testdir -|  
                   | - subdir  
                   | - subdir2  
                   | - testfile.txt 

If test, testdir, subdir, and subdir2 are all directories, and Finder.class and testfile.txt are files, and given:

import java.io.*;  
public class Main {  
  public static void main(String[] args) throws IOException {  
    String[] files = new String[100];  
    File dir = new File(args[0]);  
    files = dir.list();  
    System.out.println(files.length);  
  } 
} 

And, if the code compiles, the invocation:.

java Main testdir 

What is the result?

  • A. 1
  • B. 2
  • C. 3
  • D. 4
  • E. 5
  • F. 100
  • G. Compilation fails.
  • H. An exception is thrown at runtime.


C is correct.

Note

File I/O stuff is risky, but it's okay for main() to throw an exception.

The list() method returns a String[] that contains the names of both the files AND subdirectories within the directory specified.

Note that the current directory and its contents are not included.




PreviousNext

Related