Line 16 is never executed.
An exception is thrown at runtime.
Line 13 creates a File object named “d”.
Line 14 creates a File object named “f‟.
Line 13 creates a directory named “d” in the file system.
Line 16 creates a directory named “d” and a file “f” within it in the file system.
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();
}
}
}
第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
第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?()
第5题:
下面程序的结果是 ______。 #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
第6题:
下面程序的运算结果是( )。 #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
第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
第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.");
}
}
第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?()
第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?()