首页 > 代码库 > java API 测试题
java API 测试题
学了近两个月的java基础知识,上个月中进行了一次API的测试,很多题目都不确定,蒙错了,被罚5个俯卧撑~~~~~唉,说多了都是泪,把测试题发上来,虐虐大家,不过,有附带答案喔~~~~
1.所有异常的父类是哪项? A 通过API查看可知
A. Throwable
B. Error
C. RuntimeException
D. Exception
2.现有: BC 两种情况,一种是没有异常,一种是有异常
1. class Flow {
2. public static void main(String [] args) t
3. try {
4 . System. out .print ("before") ;
5 . doRiskyThing ( ) ;
6. System.out.print ("after ") ;
7. } catch (Exception fe) {
8. System.out.print ("catch") ;
9. }
10 . System. out .println ( " done") ;
11. }
12. public static void doRiskyThing() throws Exception{
13. // this code returns unless it throws an Exception
}}
可能会产生哪两项结果 ?(选两项)
A. before catch
B. before after done
C. before catch done
D. before after catch
3.现有: D 继承的utils,Utils里面的方法没有抛出异常,而重写的时候抛出了异常,这样是不允许的,两者应该同步
1. class Parser extends Utils (
2.public static void main (String[] args) {
3.try{System.out.print (new Parser().getlnt("42"));
4. } catch (Exception e) {
5.System.out.println("Exc"); }
6. }
7.int getlnt (String arg) throws Exception (
8.return Integer.parselnt (arg);
9. }
10. }
11. class Utils {
12. int getlnt (String arg) {return 42; }
13. }
结果为:
A. 42
B. Exc
C. 42Exc
D.编译失败
4.以下关于File类的叙述,哪两项正确?(选两项)AC 基础知识
A.File类位于java.io包中
B.创建一个File类的实例将导致打开指定文件进行读写
C.File实例不能用于封装目录
D. File实例封装了指定文件的信息
5.现有: B 基本理论
1. import java.util.*;
2. class ScanStuff {
3.public static void main (String [] args) {
4.String S= "x,yy,123";
5.Scanner sc = new Scanner (s);
6.while (sc.hasNext())
7.System.out.print (sc.next() +" ");
8. }
9. }
结果是什么?
A. x yy
B. x,yy,123
C. x yy 123
D. x,yy
E.编译失败
F.运行的时候有异常抛出
6.现有: D
31. String s="write a line to a file";
32. w.print(s+"\n");
哪一个是对的?
A.w即可以是PrintWriter类型,也可以足BufferedWriter类型。
B.w即不可以是PrintWriter类型,也不可以足BufferedWriter类型。
C.w 可以是BufferedWriter类型,但不可以是PrintWriter类型。
D. w以是PrintWriter类型,但不可以是BufferedWriter类型。
Given the exhibit. D 它的本意是想根据空格来分割,但是里面放的是”\s”,根据转义字符可知,应该是”\\s”
7.What is the result?
A. 0
B. 1
C. 4
D. Compilation fails
E. An exception is thrown at runtime
8.When comparing java. io. BufferedWriter to java.io.FileWriter, which capability exist as a method in only one of the two? E
A. closing the stream
B. flushing the stream
C. writing to the stream
D. marking a location in the stream
E. writing a line separator to the stream
9. B, D, E
10. 以下Java语句中,()可以构造一个BufferedInputStream对象。D
A. new BufferedInputStream(new FileOutputStream("abc.txt"));
B. new BufferedInputStream(new InputStream("abc.txt"));
C. new BufferedInputStream(new FileReader("abc.txt"));
D. new BufferedInputStream(new FileInputstream("abc.txt"));
11. 在JAVA中,( )类提供对文件或目录及属性进行基本操作。D
A. FileInputStream
B. FileReader
C. FileWriter
D. File
12. public class TEApp { A 基本概念
public static void ThrowException() {
throw new Exception();
}
public static void Main() {
try {
Console.WriteLine("try");
ThrowException();
}catch(Exception e) {
Console.WriteLing("catch");
}finally {
Console.WriteLing("finally");
}
}
} 请问运行代码的结果是().
A. try catch finally
B. try
C. catch finally
D. tryfinally
13. 在下列Java代码中,会出现编译错误的是( )。D OutputStreamWriter实例化的时候,只接受OutputStream的实例
A. File f =new File("/","l.dat");
B. DataInputStream din = new DataInputStream(new FileInputStream("l.dat"));
C. InputStreamReader in = new InputStreamReader(System.in);
D. OutputStreamWriter out= new OutputStreamWriter(System.in)
14.现有 A C 一个是非法的参数异常 一个是数字转换错误
1. class Calc {
2. public static void main(String [] args) {
3. try {
4. int x = Integer.parselnt ("42a") ;
5. //insert code here
6. System.out.print ("oops");
7. }
8. }
9. }
下面哪两行分别插入到第五行,会导致输 "oops" ? (选两项)
A. } catch (IllegalArgumentException e) {
B. } catch (IllegalStateException c) {
C. } catch (NumbelFormatException n) {
D. } catch (ClassCastException c) {
15.现有: D 道理和第3题类似
1. class Propeller2 {
2. pulolic static void main (String[]args)//add code here?
3. { new Propeller2().topGo(); }
4.
5.void topGo() //add code here?
6. { middleGo(); }
7.
8.void middleGo() //add code here?
9. { go(); System.out.println ("late middle"); }
void go() //add code here?
12. {throw new Exception(); }
13. }
为使代码通过编译,需要在哪一行加入声明throws Exception?
A.只在第11行
B.在第8行和第11行
C.在第5行、第8行和第11行
D.在第2行、第5行、第8行和第11行
16.在输入流的read方法返回哪个值的时候表示读取结束?C
A. 0
B. 1
C. -1
D. null
17.现有: D
一f对一个java .io.File型实例的合法引用
一fr对‘个java.io.FileReader型实例的合法引用
一br对一个java.io.BufferedReader型实例的合法引用
和:
34. String Line=null;
35.
36. //insert code here
37. System.out.println (line);
38. }
哪一行代码插入到36行将循环通过一个文本文件并存文本域中每次输出一行?
A. while( (line=f.read()) !=null) {
B. while( (line=fr.read()) !=null) {
C. while( (line=br.read()) !=null) {
D. while( (line=br.readLine()) !=null) {
E. while( (line=fr.readLine()) !=null) {
F. while( (line=f.readLine()) !=null) {
18.现有: CF
-f是一个合法的java.io.File实例的引用
- fr是一个合法的java.io.FileReader实例的引用
- br 是一个合法的java.io.BufferedReader实例的引用
哪两个是合法的?(选两项)
A. File f2=new File (f);
B. FileReader fr2=new FileReader (fr);
C. FileReader fr2=new FileReader(f);
D. FileReader fr2=new FileReader(br);
E. BufferedReader br2 =new BufferedReader (f);
F. BufferedReader br2=new BufferedReader (fr);
19.Given classes defined in two different files: C
What is required at line 5 in class Certkiller App to use the process method of Bit Utils?
A. Process (bytes);
B. BitUtils.process (bytes);
C. Util.BitUtils.process (bytes);
D. Certkiller App cannot use methods in BitUtils
E. Import util.BitUtils.*; process (bytes);
20.Given the exhibit: F
What is the result
A. B
B. The code exception is thrown at runtime
C. The cod run with no output.
D. Compilation fails because of an error in line 12.
E. Compilation fails because of an error in line 15.
F. Compilation fails because of an error in line 16.
java API 测试题