SCJP程序员认证考试

多选题10. class MakeFile {  11. public static void main(String[] args) {  12. try {  13. File directory = new File(”d”);  14. File file = new File(directory,”f”);  15. if(!file.exists()) {  16. file.createNewFile();  17. }  18. } catch (IOException e) {  19.

题目
多选题
10. class MakeFile {  11. public static void main(String[] args) {  12. try {  13. File directory = new File(”d”);  14. File file = new File(directory,”f”);  15. if(!file.exists()) {  16. file.createNewFile();  17. }  18. } catch (IOException e) {  19. e.printStackTrace  20. }  21. }  22. }  The current directory does NOT contain a directory named “d.” Which three are true?()
A

Line 16 is never executed.

B

An exception is thrown at runtime.

C

Line 13 creates a File object named “d”.

D

Line 14 creates a File object named “f‟.

E

Line 13 creates a directory named “d” in the file system.

F

Line 16 creates a directory named “d” and a file  “f”  within it in the file system.

G

Line 14 creates a file named f inside of the directory named “d” in the file system.

如果没有搜索结果,请直接 联系老师 获取答案。
如果没有搜索结果,请直接 联系老师 获取答案。
相似问题和答案

第1题:

(11 )下面程序运行时输出结果为

C:\Program Files is a directory

请将程序补充完整。

import Java.io.*;

public class DirTest{

public static void main(String[] args){

File myDir = new File( " C:/Program Files/ " );

System.out.println

(myDir + ( 【 11 】 .isDirectory() ? " is " : " is not " ) + " a directory. " );

}

}


正确答案:

第2题:

在程序中,用户输入一个文件名,根据用户输入显示相应文件的信息。

注意:请勿修改main()主方法和其他已有语句内容,仅在横线处填入适当语句。

______java.io.*;

public class basic

{

public static void main(String[] args)

{

InputStreamReader reader;

BufferedReader in;

System.out.println("请输入文件名: ");

try

{

reader=new InputStreamReader(______);

in=new BufferedReader(reader);

String filename=in.readLine();

File file=new File(filename);

System.out.println("文件名:"+file.______);

System.out.println("路径:"+file.getAbsolutePath());

System.out.println("大小:"+file.length());

}

catch(Exception e)

{

e.printStackTrace();

}

}

}


正确答案:import System.in getName()
import System.in getName() 解析:本题考查知识点:Java类库中常用类和接口、文件和文件I/0、输入输出。解题思路:题中reader从系统获得输入流,从这个流中得到用户输入的字符串作为文件名,找到文件,进而得到文件的相关信息。Java的类库需要引入以后才能使用,关键字import就是声明需要引入的类或包。因此第1个空的答案是import。Java的输入输出是以流的形式来完成的。InputStreamReader的对象reader从系统输入中读取输入流,保存在相应的缓冲区中,因此第2个空的答案是System.in。BufferedReader对象则是从这个缓冲区中读取数据,使用BufferedReader类的readLine()方法即可获得输入流中的一行输入。在Java程序中,文件作为类的一个实例来处理,File类具有很多与文件相关的方法,比如获得上级目录名(getParent()方法)、路径(getPath()方法)等,第3个空就是使用getName()方法获取文件的文件名。

第3题:

【Java代码】

import Java.util.ArrayList;

import java.util.List;

(1) class AbstractFile{

protected String name;

public void printName(){System.out.println(name);}

public abstract boolean addChild(AbstractFile file);

public abstract boolean removeChild(AbstractF ile file);

public abstract List<AbstractFile> getChildren();

class File extends AbstractFile{

public File(String name){this.name=name;}

public boolean addChild(AbstractFile file){return false;}

public boolean removeChild(AbstractFile file){return false;}

public List<AbstractFile> getChildren(){return (2) ;}

class Folder extends AbstractFile{

private List <AbslractFile> childList;

public Folder(String name){

this.name=name;

this.childList=new ArrayList<AbstractFile>();

public boolean addChild(AbstractFile file) { return childList.add(file);}

public boolean removeChild(AbstractFile file){return childList.remove(file);}

public (3) <AbstractFile> getChildren(){return (4) ;}

public class Client{

public static void main(String[] args){

//构造一个树形的文件/目录结构

AbstractFile rootFolder= new Folder("c:\\ ");

AbstractFile compositeFolder=new Folder("composite");

AbstractFile windowsFolder=new Folder("windows");

AbstractFile file=new File("TestComposite.java");

rootFolder.addChild(compositeFolder) ;

rootFolder.addChild(windowsFolder);

compositeFolder.addChild(file) ;

//打印目录文件树

printTree(rootFolder);

private static void printTree(AbslractFile ifile){

ifile.printName();

List <AbslractFile> children=ifile.getChildreno:

if(children==null) return;

for (AbstractFile file:children) {

(5) ;

}

该程序运行后输出结果为:

c:\

composite

TestComposite.java

Windows


正确答案:
(1)abstract
(2)null
(3)List
(4)childList
(5)printTree(file)

第4题:

1. package foo;  2.    3. import java.util.Vector;  4.    5. private class MyVector extends Vector {  6. int i = 1;  7. public MyVector()  {  8. i = 2;  9.    }  10. }  11.    12. public class MyNewVector extends MyVector {  13. public MyNewVector ()  {  14. i = 4;  15. }  16. public static void main (String args [])  {  17. MyVector v = new MyNewVector();  18.   }  19. }     The file MyNewVector.java is shown in the exhibit.  What is the result?()  

  • A、 Compilation will succeed.
  • B、 Compilation will fail at line 5.
  • C、 Compilation will fail at line 6.
  • D、 Compilation will fail at line 14.
  • E、 Compilation will fail at line 17.

正确答案:B

第5题:

下面程序的结果是 ______。includeclass A{ public:virtual voidfun()=0{};};class

下面程序的结果是 ______。 #include<iostream.h> class A{ public: virtual void fun()=0{}; }; class B:public A{ public: void fun () {cout<< "new file" ;} }; class C: public A{ public: void fun (){cout<<"open file"<< " " } }; class D: public A{ public: void fun () {cout<< "save file\n" ;} }; void main() { A a,*p; B b; C c; D d; p=&c; p->fun (); p=&b; p->fun (); p=&d; p->fun(); }

A.new file open file save file

B.new file new file new file

C.编译出错

D.open file new file save file


正确答案:C

第6题:

下面程序的运算结果是()。includeusing namespace std;class A{public:virtual void f

下面程序的运算结果是( )。 #include<iostream> using namespace std; class A { public: virtual void fun()=0; }; class B:public A } public: void fun() {cout<<"new file"<<" ";} }; class C:public A { public: void fun() { cout<<"open file"<<" ";} }; void main() { A a, * p; B b;C c; p=&c; p->fun(); p=&b; }

A.new file open file

B.new file new file

C.编译出错

D.open file new file


正确答案:C
解析:语句Aa,*p;用抽象类说明了一个对象,这是不允许的。若把其改为A*p;则程序运行结果为D。

第7题:

现有一个文件file21.txt,其内容是: abCdEf, 执行下列程序之后,输出的结果是______。 package ch1; import java,io.*; public class ex21 { static String name = "ch1\\file21.txt"; public static void main(String[] args) { try { readFile (); } catch(IOException ioe) { System.out.println(ioe.getMessage()); } } static void readFile () throws IOException { BufferedReader br = null; try { File f = new File(name); FileReader fr = new FileReader(f); br = new BufferedReader(fr); String str= br.readLine(); System.out.println(str.toLowerCase()); } finally if(br != null) br.close (); } } }

A.AbCdEf

B.abcdef

C.aBcDeF

D.ABCDEF


正确答案:B

第8题:

下面程序运行时输出的结果为

C:\Program Files is a directory.

将程序补充完整。

Import java.io.*;

public class DirTest {

public static void main(String[] args) {

File myDir=Flew File("C:/Program Files/");

System.out.println(myDir+(______.isDirectory()?"is":"is not")+"a directory.");

}

}


正确答案:myDir
myDir 解析:本程序首先创建File类的一个实例myDir对象,输出结果实际上是对文件属性的测试,即是否是一个目录。File类提供isDirectory()方法来测定文件是否是目录,所以本题空白位置所要填入的内容就是创建的myDir对象。

第9题:

11. static class A {  12. void process() throws Exception { throw new Exception(); }  13. }  14. static class B extends A {  15. void process() { System.out.println(”B”); }  16. }  17. public static void main(String[] args) {  18. new B().process();  19. }  What is the result?() 

  • A、 B
  • B、 The code runs with no output.
  • C、 Compilation fails because of an error in line 12.
  • D、 Compilation fails because of an error in line 15.
  • E、 Compilation fails because of an error in line 18.

正确答案:A

第10题:

1. public class Test { 2. public static String output =””; 3.  4. public static void foo(int i) { 5. try { 6. if(i==1) { 7. throw new Exception(); 8. } 9. output += “1”; 10. } 11. catch(Exception e) { 12. output += “2”; 13. return; 14. } 15. finally { 16. output += “3”;17. } 18. output += “4”; 19. } 20.  21. public static void main(String args[]) { 22. foo(0); 23. foo(1); 24.  25. }26. } What is the value of the variable output at line 23?()


正确答案:13423

更多相关问题