Implementation a.
Implementation b.
Implementation c.
Implementation d.
Implementation e.
第1题:
A.Compilation fails.
B.Nothing is added to the file system.
C.Only a new file is created on the file system.
D.Only a new directory is created on the file system.
E.Both a new file and a new directory are created on the file system.
第2题:
下面程序的运算结果是( )。 #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
第3题:
下列程序要求将source.txt文件中的字符,通过文件输入/输出流复制到另一个dest.txt文件中。请将程序补充完整。
注意:不改动程序结构,不得增行或删行。
import java.io.*;
public class ex2
{
public static void main(String[] args) throws IOException
{
File inputFile;
File outputFile;
FileInputStream in;
FileOutputStream out;
int c;
inputFile=new File("source.txt");
utputFile=new File("dest.txt");
in=new FileInputStream(inputFile);
______(outputFile);
while((c=in.read())!=-1)
______;
in.close();
out.close();
}
}
第4题:
现有一个文件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
第5题:
下列程序的运行结果是______。 package ch1; import java.io. *; public class ex28 { public static void main (String args [] ) throws IOException { try { File f1 = new File("ch1\\dir28"); f1.mkdir(); File f2 = new File(f1, "file59.txt"); FileOutputStream fos = new FileOutputStream(f2); for(int i = 0; i < 2; i++) { String s= i + "times"; byte[] b = s.getBytes(); fos.write(b,0,b.length); } } catch(IOException ioe) { ioe.printStackTrace(); } } }
A.在目录ch1下建立一个目录dir28,并且建立文件file28.txt,在文件中写入"Otimes 1 times"
B.在目录ch1下建立一个目录dir28,并且建立文件file28.txt,在文件中写入“l times”
C.在目录chi下建立一个目录dir28,并且建立文件file28.txt,在文件中写入“Otimes”
D.抛出IOExceptin异常
第6题:
阅读下列说明和Java代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
【说明】
现欲构造一文件/目录树,采用组合(Composite)设计模式来设计,得到的类图如6—8所示:
【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(AbstractFile file);
public abstract ListgetChildren {};
}
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 ListgetChildren(){return (2) ;)
}
clasS Folder extends AbstractFile{
private ListchildList;
public Folder(String name){
thiS.name=name;
this.childList=new ArrayList{};
}
public boolean addChild(AbstractFile file){return childList.add(file);}
public boolean removeChild(AbstractFile file){return childList.remove(file);
public (3)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.addChiid(windowsFolder);
compositeFolder.addChild(file);
//打印目录文件树
printTree(rootFolder);
}
private static void printTree(AbstractFile ifile){
ifile.PrIntName();
Listchildren:ifile.getChildren ();
if(chiidren==null)return;
for(AbstractFile file:children){(5) ;
}
}
}
该程序运行后输出结果为:
C:\
composite
TestComposite.java
Windows
第7题:
下面程序的结果是 ______。 #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
第8题:
public class test { public static void main(String [] a) { assert a.length == 1; } } Which two will produce an AssertionError?()
第9题:
Which method implementations will write the given string to a file named "file", using UTF8 encoding?() IMPLEMENTATION a: public void write(String msg) throws IOException { FileWriter fw = new FileWriter(new File("file")); fw.write(msg); fw.close(); } IMPLEMENTATION b: public void write(String msg) throws IOException { OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("file"), "UTF8"); osw.write(msg); osw.close(); } IMPLEMENTATION c: public void write(String msg) throws IOException { FileWriter fw = new FileWriter(new File("file")); fw.setEncoding("UTF8"); fw.write(msg); fw.close(); } IMPLEMENTATION d: public void write(String msg) throws IOException { FilterWriter fw = FilterWriter(new FileWriter("file"), "UTF8"); fw.write(msg); fw.close(); } IMPLEMENTATION e: public void write(String msg) throws IOException { OutputStreamWriter osw = new OutputStreamWriter( new OutputStream(new File("file")), "UTF8" ); osw.write(msg); osw.close(); }
第10题:
The file “file.txt” exists on the file system and contsins ASCII text. Given: try { File f = new File(“file.txt”); OutputStream out = new FileOutputStream(f, true); } catch (IOException) {} What is the result?()
第11题:
Implementation a.
Implementation b.
Implementation c.
Implementation d.
Implementation e.
第12题:
String name= File.getParentName(“file.txt”);
String name= (new File(“file.txt”)).getParent();
String name = (new File(“file.txt”)).getParentName();
String name= (new File(“file.txt”)).getParentFile();
Directory dir=(new File (“file.txt”)).getParentDir(); String name= dir.getName();
第13题:
A.Compilation fails.
B.The file system has a new empty directory named dir.
C.The file system has a new empty directory named newDir.
D.The file system has a directory named dir, containing a file f1.txt.
E.The file system has a directory named newDir, containing a file f1.txt.
第14题:
阅读下面程序 import java.io.*; public class ByteStreamTest { public static void main(String[] args) { int[] myArray={10,20,30,40}; try { DataOutputStream dos=new DataOutputStream (new ______("ints.dat")); for(int i=0;i<myArray.length;i++)dos.writeInt(myArray[i]); dos.close(); System.out.println("Have written binary file ints.dat"); } catch(IOException ioe) { System.out.println("IOException"); } } } 为保证程序正确运行,在程序中下画线处应填人的代码是
A.FileOutputStream
B.ByteArrayOutputStream
C.BufferedOutputStream
D.FileWriter
第15题:
在程序中,用户输入一个文件名,根据用户输入显示相应文件的信息。
注意:请勿修改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();
}
}
}
第16题:
下列程序执行后的结果是______。 package ch1; import java.io.*; public class ex22 { static, String filename = "ch1\kfile22.txt"; public static void main(String[] args) { try { FileWriter fr = new FileWriter(filename); PrintWriter pr = new PrintWriter(fr); String name = "xiaoming"; String phone = "123456"; String age = "12"; pr.println(name + ',' + phone + ',' + age); pr.close(); fr.close(); } catch(IOException ioe) { ioe.printStackTrace() } } }
A.在包ch1 中新建一个文件file22.txt, 并且在其中写入一行字符串“xiaomingl2345612”
B.在包ch1中新建一个文件file22.txt,并且在其中写入一行字符串“xiaoming, 123456,12”
C.在包chi中新建一个文件file22.txt,并且在其中写入一行字符串“xiaoming'’
D.抛出IOException
第17题:
阅读下列说明和c++代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
【说明】
现欲构造一文件/目录树,采用组合(Composite)设计模式来设计,得到的类图如6—7所示:
【c++代码】
include<1ist>
include
include
using namespace std;
class AbstractFile{
protected:
string name;//文件或目录名称
public:
void printName(){cout<*getChildren()=0; //获得一个目录的子目录或文件
};
class File:public AbstractFile{
public:
File(string name){ (1) =name;)
void addChild(AbstractFile*file){return ;)
void removeChiid(AbstractFile*file){return;}(2) getChildren(){return ( 3 ) ;}
};
class Folder:public AbstractFile{
private:
listchildList; //存储子目录或文件
public:
Folder(string name){ (4) =name;}
void addChild(AbstractFile*file){childList.push back(file);}
void removeChiid(AbstractFile*file)(chiidList.remove(file);}
list*getChildren(){return (5) ;)
};
voidmain(){
//构造一个树形的文件/目录结构
AbstractFile*rootFolder=new Folder(“C:\\”);
AbstractFile*compositeFolder=flew Folder(”composite”);
AbstractFile*windowsFolder=new Folder(”windows”);
AbstractFile*file=new File(”TestComposite.java”);
rootFolder->addChild(compositeFolder);
rootFolder->addChild (windowsFolder);
compositeFolder->addChiid(file);
)
第18题:
以下程序是一个简单文本处理器,菜单项可以打开、编辑、保存一个文件。文件内容显示在下面的文本区域中(提示,打开文件通过文件选择器来完成)。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。运行结果如下图所示。
注意:不改动程序的结构,不得增行或删行。
import java.awt.*;
import java.awt.event.*;
import java.io.* ;
import javax.swing.*;
class FileFrame. extends JFrame
{
File file;
JTextPane textpane;
FileInputStream readStream;
JScrollPane scroll;
public FileFrame()
{
super ("文件浏览");
JMenu fileM = new JMenu("文件");
OpenAction pen = new OpenAction ();
SaveAction clear = new SaveAction ();
ExitAction exit = new ExitAction();
JMenuBar mb = new JMenuBar();
fileM.add(open);
fileM.add(clear);
fileM.add(exit);
mb.add(fileM);
textpane=new JTextPane();
scroll=new JScrollPane(textpane);
getContentPane().add(scroll);
getContentPane().addJMenuBar(mb);
}
class OpenAction extends AbstractAction
{
public OpenAction ()
{
super("打开");
}
public void actionPerformed( ActionEvent e )
{
JFileChooser chooser=new JFileChooser();
int state=chooser.showOpenDialog(null);
file=chooser.selectedFile();
if(file!=null&&state==JFileChooser.APPROVE_OPTION)
{
try
{
readStream=new FileInputStream(file);
textpane.read(readStream, this);
readStream.close();
}
catch(IOException ioE){}
}
}
}
class SaveAction extends AbstractAction
{
public SaveAction()
{
super("保存");
}
public void actionPerformed( ActionEvent e )
{
if(file==null)
return;
try{
FileWriter ut = new FileWriter(file);
out.read(textpane.getText());
out.close();
}
catch (IOException ioE)
{}
}
}
class ExitAction extends AbstractAction
{
public ExitAction()
第19题:
( 11 )请在下列程序的空白处,填上适当的内容:
Import java. awt. *;
Import java. util. *;
Class BufferTest{
Public static void main(string args[])
Throws IOException{
FileOutputStream unbuf=
new FileOutputStream( “ test.one ” ) ;
BufferedOutputStream buf=
new 【 11 】 (new FileOutputStream( “ test.two ” ));
System.out.println
( “ write file unbuffered: ” + time(unbuf) + “ ms ” );
System.out.println
( “ write file buffered: ” + time(buf) + “ ms ” );
}
Static int time (OutputStream os)
Throws IOException{
Date then = new Date();
for (int i=0; i<50000; i++){
os.write(1);
}
}
os.close();
return(int)(()new Date()).getTime() - then.getTime());
}
第20题:
Which gets the name of the parent directory file “file.txt”?()
第21题:
Given that file is a reference to a File object that represents a directory, which code fragments will succeed in obtaining a list of the entries in the directory?()
第22题:
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?()
第23题:
Compilation fails.
Nothing is added to the file system.
Only a new file is created on the file system.
Only a new directory is created on the file system.
Both a new file and a new directory are created on the file system.
第24题:
java test
java -ea test
java test file1
java -ea test file1
java -ea test file1 file2
java -ea:test test file1