SCJP考题8

来源:互联网 发布:网络拓扑图素材黑底 编辑:程序博客网 时间:2024/05/18 00:14

880. If you run the following code on a on a PC from line directory c:/ source:

import java.io.*;

class Test {

public static void main(String[] args) throws Exception{

File file = new File("Ran.test");

System.out.println(file.getAbsolutePath());

}

}

What do you expect the output to be?

Select the one right answer.

A. Ran.test

B. source/Ran.test

C. c:/source/Ran.test

D. c:/source

E. null

正确答案: C

 

881. What happens if the file "Ran.test" does not yet exist and you attempt to compile and run the following code?

import java.io.*;

class Test {

public static void main(String[] args) throws IOException{

RandomAcessFile out=

new RandomAccessFile("Ran.test", "rw");

out.writeBytes("Ninotchka");

}

}

Select the one right answer.

A. The code does not compile because RandomAccessFile is not created correctly.

B. The code does not compile because RE. andomAccessFile does not implement the writeBytes() method.

C. The code compiles and runs but throws an IOExeption because "Ran.test" does not yet exist.

D. The code compiles and runs but nothing appears in the file"Ran.test" that it creates.

E. The code compiles and runs and "Ninotchka" appears in the file "Ran.test" that it creates.

正确答案: E

 

882. What modes are legal for creating a new RandomAccessFile object?

A. "w"

B. "r"

C. "x"

D. "rw"

E. "xrw"

正确答案: B D

 

883. To invoke read() from an InputStream subclass, you must handle what type of exception?

Fill in the blank

正确答案: "IOExeption" or "java.io.IOException"

 

884. What does the following code do?

File f=new File("hello.test");

FileOutputStream out=new FileOutputStream(f);

Select the one right answer.

A. Create a new file named "hello.test" if it does not yet exist. It also opens the file so you can write to it and read from it.

B. Create a new file named "hello.test" if it does not yet exist. The file is not opened.

C. Open a file named "hello.test" so that you can write to it and read from it, but does not create the file if it does not yet exist.

D. Open a file named "hello.test" so that you can write to it but cannot read from it.

E. Create an object that you an now use tp create and open the file named "hello.test," and write to and read from the file.

正确答案: A

 

885. Given this code:

import java.io.*;

class Write{

public static void main(String[] args)throws Exception{

File file=new File("temp.test");

FileOutputStream stream=

new FileOutputStream(file);

//write integers here...

}

}

How can you replace the comment at the end of main() with code that will write the integers 0 through 9?

A. DataOutputStream filter=new DataOutputStream(stream);

for(int i=0; i<10; i++)

filter.writeInt(i);

B. for(int i=0; i<10; i++)

file.writeInt(i);

C. for(int i=0; i<10; i++)

stream.writeInt(i);

D. DataOutputStream filter=new DataOutputStream(stream);

for(int i=0; i<10; i++)

filter.write(i);

E. for(int i=0; i<10; i++)

stream.write(i);

正确答案: A

 

886. Which of the following classes can handle the Unicode?

A. InputStreamReader

B. BufferedReader

C. Writer

D. PipedInputStream

正确答案: A B C

 

887. Which classes are used to implement TCP/IP client and servers?

A. ServerSocket

B. Server

C. Socket

D. DatagramPacket

E. DatagramSocket

正确答案: A C

 

888. Which is possible parameter that can be passed into the constructor of a FilterOutputStream?

A. File

B. PrintStream

C. FileOutputStream

D. FileInputStream

E. RandomAccessFile

正确答案: C

 

889. A socket object has been created and connected to a standard internet service on a remote network server. Which construction give the most suitable means for reading ASCII data online at a time from the socket?

A. InputStream in =s.getInputStream();

B. DataInputStream in =new DataInputstream(s.getInputStream());

C. ByteArrayInputStream in=new ByteArrayInputStream(s.getInputStream());

D. BufferedReader in =new BufferedReader(new InputStreamReader(s.getInputStream()),"8859-1");

正确答案: D

 

890. FilterOutoutStream is the parent class for BufferedOutputStream. DataOutputStream and PrintStream. Which classes is valid argument for the constructor of a FilterOutputStream?

A. InputStream

B. OutputStream

C. File

D. RandomAcessFile

E. StreamTokenizer

正确答案: B

 

891. You have an 8-bit file using the character set defined by ISO 8859-8. You are writing an application to display this file in a TextArea. The local encoding is already set to 8859-8. How can you write a chunk of code to read the first line from this file?

You have three variables accessible to you, myfile is the name of the file you want to read, stream is an InputStream object associated with this file, s is a String object.

Select all valid answers.

A. InputStreamReader reader=new InputStreamReader(stream,"8859-8");

BufferedReader buffer=new BufferedReader(stream);

s=buffer.readLine();

B. InputStreamReader reader=new InputStreamReader(stream);

BufferedReader buffer=new BufferedReader(stream);

s=buffer.readLine();

C. InputStreamReader reader=new BufferedReader(reader);

s=buffer.readLine();

D. InputStreamReader reader=new InputStreamReader(myfile);

BufferedReader buffer=new BufferedReader(reader);

s=buffer.readLine();

E.FileReader reader=new FileReader (myfile);

BufferedReader buffer=new BufferedReader(reader);

s=buffer.readLine();

正确答案: A B E

 

892. The ISO ode for the language you are interested in is 8859-5.Assume you have a stream in a variable named mystream that's associated with a file generated in this 8-bit character set. if the default conversion to Unicode in your environment is for the encoding 8859-1, how can you create a new instance of InputStreamReader that will perform the inversion from ISO 8859-5 to Unicode, automatically?(Start by writing the keyword new and do not add a semicolon at the end.)

Fill in the bland.

正确答案: new InputStreamRedaer(mystreeam,"8859-5")

 

893. What will be output if you try to compile and run the following code, but there is no file called Hello.txt in the current directory?

import java.io.*;

public class Test{

public static void main(String argv[]){

Test m=new Test();

System.out.println(m.amethod());

}

public int amethod(){

try{

FileInputStream dis=

new FileInput Stream("Hello.txt");

}catch(FileNotFoundExeption fne){

System.out.println("No such file found");

return-1;

}catch(IOException ioe){

} finally{

System.out.println("Doing finally");

}

return 0;

}

}

Select all valid answers.

A. No such file found

B. No such file found,-1

C. No such file found, Doing finally,-1

D. 0

正确答案: C

 

894. How would you go about opening an image file called MyPicture.jpg?

A. Graphics.getGraphics("MyPicture.jpg");

B. Image inage=Tooldit.getDefaultToolkit().getImage("MyPicture.jpg");

C. Graphics.openImage("MyPicture");

D. Image m=new Image("MyPicture");

正确答案: B

 

895. How can you change the current working directory using an instance of the File class called FileName?

A. FileName.chdir("DirName")

B. FileName.cd("DirName")

C. FileName.cwd("DirName")

D. The File class does not support directly changing the current directory.

正确答案: D

 

896. Which of the following can you perform using the File class?

A. Change the current directory

B. Return the name of the parent directory

C. Delete a file

D. Find if a file contains text or binary information

正确答案: B C

 

897. Which of the following will compile without error?

A. File f=new File("/","autoexec.bat");

B. DataInputStream d=new DataInputStream(System.in);

C. OutputStreamWriter o=new OutputStreamWriter(System.out);

D. RandomAccessFile r=new RandomAcessFile("OutFile");

正确答案: A B C

 

898. Which of the following are true statements?

A. The RandomAccessFile class allows you to move directly to any point a file.

B. The creation of a named instance of the File class creates a matching file in the underlying operating system only when the close method is called.

正确答案: A

 

899. Assuming any exception handing has been set up, which of the following will create an instance of the RandomAccessFile class?

A. RandomAccessFile raf=new RandomAccessFile("myfile.txt","rw");

B. RandomAccessFile raf=new RandomAccessFile(new DataInputStream());

C. RandomAccessFile raf=new RandomAccessFile("myfile.txt");

D. RandomAccessFile raf=new RandomAccessFile(new file("myfile.txt"));

正确答案: A

 

900. You need to read in the lines of a large text file containing tens of megabytes of data. Which of the following would be most suitable for reading in such a file?

A. new FileInputStream("file.name")

B. new InputStreamReader(new FileInputStream("file, name"))

C. new BufferedReader(new InputStreamReader(new FileInputStream("file name")));

D. new RandomAccessFile raf=new RandomAccessFile("myfile.txt","+rw");

正确答案: C

 

901. There are two computers are connected to internet, one computer is trying to open a socket connections to read the home page of another computer, what are the possible exceptions thrown while connection and reading InputStream?

A. IOException

B. MalformedURLException

C. NetworkException

D. ConnectException

正确答案: A B

 

902. What line of a given program will throw FileNotFoundException?

import java.io.*;

public class Test{

public static void main(String args[]){

try{

FileReader fileReader=

new FileReader("MyFile.java");

BufferedReader bufferedReader=

new BufferedReader(fileReader);

String strString;

fileReader.close();

while((strString=bufferedReader.readLine())!=null{

System.out.printIn(strString);

}

}catch(IOExeption ie){

System.out.println(ie.getMessage());

}

}

Select the one right answer.

A. This program never throws FileNotFoundException.

B. The line fileReader.close() throws FileNotFoundException.

C. At instantiation of FileReader object.

D. While constructing the BuffereReader object.

正确答案: C

 

903. When the following program will throw an IOException?

import java.io.*;

class Test{

public static void main(String args[]){

try{

String strString=

"Now is the time to take Sun Centification";

char buffer[]=new char[strString.length()];

strString.getChars(0,strString.length(),buffer,0);

FileWriter f=new FileWriter("myFile1.txt");

FileWriterf1=f;

f1.close();

for(int i=0; i<buffer.length;i+=2){

f.write(buffer[i]);

}

f.close();

FileWriter f2=new FileWriter("MyFile2.txt");

f2.writer(buffer);

f2.close();

} catch(IOException ie){

System.out.println(ie.getMessage());

}

}

}

Select the one right answer.

A. This program never throws IOException.

B. The line f1.close(0 throws IOException.

C. While writing to the stream f.write(buffer[i])throws an IOExcpetion.

D. While constructing the FileWriter object.

正确答案: C

 

904. Which line of the program could be throwing an exception, if the program is as listed below. Assume that "MyFile2.txt" is a read only file?

import java.io.*;

class FileWrite{

public static void main(String args[]){

try{

String strString=

"Updating the critical data section"

char buffer[]=new char[strString.length()];

strString.getChars(

0,strString.length(),buffer,0);

FileWriter f=new FileWriter("MyFile1.txt");

FileWriter f1=f;

for(int i=0; i<buffer.length; i+=2){

f1.write(buffer[i]);

}

f1.close();

FileWriter f2=new FileWriter("MyFile2.txt");

f2.write(buffer);

f2.close();

}atch (IOException ie){

System.out.println(ie.getMessage());

}

}

}

Select the one right answer from the following.

A. This program never throws IOException.

B. The line f1.close() tjrpwsIOException.

C. While writing to the stream f1.write(buffer[i])throws an IOException.

D. Which constructing the FileWriter f2=new FileWriter("MyFile2.txt");

正确答案: D

 

905. Select all the correct answers about File Class?

A. A File class can be used to create files and directories.

B. A File class has a method mkdir() to create directories.

C. A File class has a method mkdirs() to create directory and its parent directories.

D. A File cannot be used to create directories.

正确答案: A B C

 

906. Using File class, you can navigate the different directories and list all the files in those directories.

A. True

B. False

正确答案: A

 

907. Select all the constructor definitions of "FileOutputStream".

A. FileOutputStream(FileDescriptor fd)

B. FileOutputStream(String fileName, boolean append)

C. FileOutputStream(RandomAccessFile raFile)

D. FileOutputStream(String dirName, String filename)

正确答案: A B

 

908. If you run the following code on a PC from the directory c:/source:

import java.io.*;

class Test{

public static void main(String[] args) throws Exception{

File file=new File("Ran.test");

System.out.println(file.getAbsolutePath());

}

}

What do you expect the output to be? Select the one right answer.

A. Ran.test

B. source/Ran.test

C. c:/source/Ran.test

D. c:/source

E. null

正确答案: C

 

909. Which of the following used to read and write to network sockets, which are super classes of Low level streams?

A. InputStream

B. StreamReaders

C. OutputStream

D. Writers

E. Readers

F. Streams

正确答案: A C

 

910. Low Level Streams read input as bytes and writes as bytes, then select the correct declarations of Streams.

A. FileInputStream FIS=new FileInoutStream("test.txt")

B. File file=new File("test.txt");

C. File file=new File("c://");

   File file1=new File(file,"test.txt");

   FileOutputStream FOS=new FileOutputStream(file1);

D. FileInputStream FIS=new FileInputStream("c://","test.txt")

正确答案: A B C

 

911. Choose all valid forms of the argument list for the FileOutputStream constructor shown below:

A. FileOutputStream(FileDescriptor fd)

B. FileOutputStream(String n, boolean a)

C. FileOutputStream(boolean a)

D. FileOutputStream()

E. FileOutputStream(File f)

正确答案: A B E

 

912. What is the class that has "mode" argument such as "r" or "rw" is required in the constructor?

A. DataInputStream

B. InputStream

C. RandomAccessFile

D. File

正确答案: C

 

913. What is the output displayed by the following code?

import java.io.*;

class Test{

public class Test{

public static void main(String args[]) throws IOException{

RandomAccessFile file=

new RandomAccessFile("test.txt","rw");

file.writeBoolean(true);

file.writeInt(123456);

file.writeInt(7890);

file.writeIntLong(1000000);

file.writeInt(777);

file.writeFloat(.0001f);

file.seek(5);

System.out.println(file.readInt());

file.close();

}

}

Select the correct answer.

A. 123456

B. 7890

C. 1000000

D. .0001

正确答案: B

 

914. Which is not a method of the class inputStream?

A. int read(byte[])

B. void flush()

C. void close()

D. int available()

正确答案: B

 

915. Which class is not subclass of FilterInputStream?

A. DataInputStream

B. BufferedInputStream

C. PushbackInputStream

D. FileInputStream

正确答案: D

 

916. Which classes can be used as the argument of the constructor of the class FileInputStream?

A. InputStream

B. File

C. FileOutputStream

D. String

正确答案: B D

 

917. Which classes can be used as the argument of the constructor of the class FilterInputStream?

A. FilterOutputStream

B. File

C. InputStream

D. RandomAccessFile

正确答案: C

 

918. True or False: Readers have methods that can read and return floats and doubles.

A. Ture

B. Flase

正确答案: B

 

919. Which of the following correctly illustrate how an InputStreamReader can be created?

Select all correct answers.

A. new InputStreamReader(new FileInputStream("data"));

B. new InputStreamReader(new FileReader("data"));

C. new InputStreamReader(new FileBufferedReader("data"));

D. new InputStreamReader("data");

E. new InputStreamReader(System.in);

正确答案: A E

 

920. Which of the following is a legal way to construct a RandomAcessFile?

Select the most appropriate answer.

A. RandomAccessFile("data","r");

B. RandomAccessFile("r","data");

C. RandomAccessFile("data","read");

D. RandomAccessFile("read","data");

正确答案: A

 

921. Which Method Returns the parent part of the pathname of this File object, or null if the name has no parent part?

A. getParent()

B. getParentDir()

C. getParentDirectory()

D. parentDirectory()

正确答案: A

 

922. Which of the following is the correct form of constructor for PrintWriter?

A. LineNumberWriter

B. PrintWriter

C. PrintStream

D. PrintOutoutReader

正确答案: B

 

923. Which of the following is the correct form of constructor for PrintWriter?

A new PrintWriter(new OutputStreamReader(new File("./hai.txt");

B. new PrintWiter(new File("./hai.txt");

C. new PrintWiter(new OutputStream());

正确答案: C

 

924. Suppose this file exists in your directory. Which of the following can be used to write a line of text to the end of this file?

File f=new File("aa.txt");

Select the most appropriate answer from following.

A. RandomAccessFile f1=new RandomAccessFile(f,"r");

B. RandomAccessFile f1=new RandomAccessFile(f,"a");

C. RandomAccessFile f1=new RandomAccessFile(f,"rw");

D. RandomAccessFile f1=new RandomAccessFile(f,"w");

E. RandomAccessFile f1=new RandomAccessFile(f,"wr");

正确答案: C

 

 
原创粉丝点击