java训练题3答案
java 试题练习题(第3套)

大学 —— 学年第 学期 《 Java 程序设计 》课程试题 课程号: √ 考试 □ A 卷 √ 闭卷 □ 考查 □ B 卷 □ 开卷一、单项选择题(20题;每题2分,共40分) 1 、Java 程序的执行过程中用到一套JDK 工具,其中javac.exe 是指( ) A)Java 文档生成器 B)Java 解释器 C)Java 编译器 D)Java 类分解器 (难度系数 C ) 答案C 知识点:JDK 工具 2、HelloWorld.java 编译成功后会在当前目录中生成一个什么文件___。
A)Hello.java B)HelloWorld.class C)Helloworld.class D)helloWorld.class (难度系数C )答案:B 知识点:JA V A 执行过程 3、main 方法是Java Application 程序执行的入口点,关于main 方法的方法头以下哪项是合法的( )? A)public static void main ( ) B)public static void main ( String[] args ) C)public static int main (String [] arg ) D)public void main (String[] arg ) 难度系数:C 答案:B 知识点:JAVA 程序结构4、以下语句正确的是___。
A) x+1=6; B) i++=1; C) a++b=9; D) x+=1;(难度系数B )答案:D 知识点:表达式5、以下结果为真(true)的是___。
A )10>’a’B )’a’>20C ) !trueD )(3<5) && (4>10)班级:姓名: 学号:试题共页加白纸张密封线(难度系数B)答案:B知识点:表达式6、如果一个线程调用了方法wait( ),则类ng.Thread的下列_______方法可以唤醒该线程。
JAVA练习题3

JA V A训练题一、单选择题1、编译Java Application源程序文件将产生相应的字节码文件,这些字节码文件的扩展名为(.class)。
A..javaB..classC..htmlD..exe2、设x=1,y=2,z=3,则表达式y+=z--/++x的值是(3)。
‘/’求整A.3B. 3.5C.4D.53、在Java Applet程序用户自定义的Applet子类中,一般需要重载父类的(paint())方法来完成一些画图操作。
A.start()B.stop()C.init()D.paint()绘画4、不允许作为类及类成员//的访问控制符的是(static)。
A.publicB.privateC.staticD.protected5、为AB类的一个无形式参数无返回值的方法method书写方法头,使得使用类名AB作为前缀就可以调用它,该方法头的形式为(static void method())。
A.static void method()B.public void method()C.final void method()D.abstract void method()6、编译Java程序的命令是(C)A、javaB、javadocC、javacD、cd7、下列哪个选项中对应的工具可以用来开发Java语言程序(B)A、Word2003B、JBuilderC、OutlookD、Excel20008、下列哪个选项是Java的输出语句(A)A、System.out.println();B、Out.system.print();C、System.println.out();D、print.system.out();9、Java语言中的语句是以(B)为结尾的。
A、逗号B、分号C、单引号D、句号10、下列哪个选项是Java的主方法(C)A、public static main(String args[])B、private static void main(String args[])C、public static void main(String args[])(课本就是这个)公共静态空方法D、private static main(String[]args)11、编译Java Application源程序文件将产生相应的字节码文件,这些字节码文件的扩展名为(B)。
JAVA练习题含答案-answertopratice3

JAVA练习题含答案-answertopratice3Chapter 3Flow of ControlMultiple Choice1)An if selection statement executes if and only if:(a)the Boolean condition evaluates to false.(b)the Boolean condition evaluates to true.(c)the Boolean condition is short-circuited.(d)none of the above.Answer:B (see page 97)2) A compound statement is enclosed between:(a)[ ](b){ }(c)( )(d)< >Answer:B (see page 98)3) A multi-way if-else statement(a)allows you to choose one course of action.(b)always executes the else statement.(c)allows you to choose among alternative courses of action.(d)executes all Boolean conditions that evaluate to true.Answer:C (see page 100)4)The controlling expression for a switch statement includes all of the following types except:(a)char(b)int(c)byte(d)doubleAnswer:D (see page 104)Copyright ? 2006 Pearson Education Addison-Wesley. All rights reserved. 125)To compare two strings lexicographically the String method ____________ should be used.(a)equals(b)equalsIgnoreCase(c)compareTo(d)==Answer:C (see page 112)6)When using a compound Boolean expression joined by an && (AND) in an if statement:(a)Both expressions must evaluate to true for the statement to execute.(b)The first expression must evaluate to true and the second expression must evaluate to false forthe statement to execute.(c)The first expression must evaluate to false and the second expression must evaluate to true forthe statement to execute.(d)Both expressions must evaluate to false for the statement to execute.Answer:A (see page 116)7)The OR operator in Java is represented by:(a)!(b)&&(c)| |(d)None of the aboveAnswer:C (see page 116)8)The negation operator in Java is represented by:(a)!(b)&&(c)| |(d)None of the aboveAnswer:A (see page 116)9)The ____________ operator has the highest precedence.(a)*(b)dot(c)+=(d)decrementAnswer:B (see page 123)10)The looping mechanism that always executes at least once is the _____________ statement.(a)if…else(b)do…while(c)while(d)forAnswer:B (see page 132).Chapter 3 Flow of Control 311) A mixture of programming language and human language is known as:(a)Algorithms(b)Recipes(c)Directions(d)PseudocodeAnswer:D (see page 134)12)When the number of repetitions are known in advance, you should use a ___________ statement.(a)while(b)do…while(c)for(d)None of the aboveAnswer:C (see page 141)13)To terminate a program, use the Java statement:(a)System.quit(0);(b)System.end(0);(c)System.abort(0);(d)System.exit(0);Answer:D (see page 148)True/False1)An if-else statement chooses between two alternative statements based on the value of a Booleanexpression.Answer:True (see page 96)2)You may omit the else part of an if-else statement if no alternative action is required.Answer:True (see page 97)3)In a switch statement, the choice of which branch to execute is determined by an expression given inparentheses after the keyword switch.Answer:True (see page 104)4)In a switch statement, the default case is always executed.Answer:False (see page 104)5)Not including the break statements within a switch statement results in a syntax error.Answer:False (see page 104)6)The equality operator (==) may be used to test if two string objects contain the same value.Answer:False (see page 111)47)Boolean expressions are used to control branch and loop statements.Answer:True (see page 116)8)The for statement, do…while statement and while statement are examples of branching mechanisms.Answer:False (see page 130)9)An algorithm is a step-by-step method of solution.Answer:True (see page 134)10)The three expressions at the start of a for statement are separated by two commas.Answer:False (see page 137)Short Answer/Essay1)What output will be produced by the following code?public class SelectionStatements{public static void main(String[] args){int number = 24;if(number % 2 == 0)System.out.print("The condition evaluated to true!");elseSystem.out.print("The condition evaluated to false!");}}Answer:The condition evaluated to true!.Chapter 3 Flow of Control 52)What would be the output of the code in #1 if number was originally initialized to 25?Answer:The condition evaluated to false!3)Write a multi-way if-else statement that evaluates a persons weight on the following criteria: Aweight less than 115 pounds, output: Eat 5 banana splits! A weight between 116 pounds and 130 pounds, output: Eat a banana split! A weight between 131 pounds and 200 pounds, output: Perfect!A weight greater than 200 pounds, output: Plenty of banana splits have been consumed!Answer:if(weight <= 115)System.out.println("Eat 5 banana splits!");else if(weight <= 130)System.out.println("Eat a banana split!");else if(weight <=200)System.out.println("Perfect!");elseSystem.out.println("Plenty of banana splits have been consumed!");4)Write an if-else statement to compute the amount of shipping due on an online sale. If the cost ofthe purchase is less than $20, the shipping cost is $5.99. If the cost of the purchase over $20 and at most $65, the shipping cost is $10.99. If the cost of the purchase is over $65, the shipping cost is $15.99.Answer:if(costOfPurchase < 20)shippingCost = 5.99;else if((costOfPurchase > 20)&&(costOfPurchase <= 65))shippingCost = 10.99;elseshippingCost = 15.99;5)Evaluate the Boolean equation: !( ( 6 < 5) && (4 < 3))Answer:True66)Write Java code that uses a do…while loop that prints even numbers from 2 through 10.Answer:int evenNumber = 2;do{System.out.println(evenNumber);evenNumber += 2;}while(evenNumber <= 10);7)Write Java code that uses a while loop to print even numbers from 2 through 10.Answer:int evenNumber = 2;while(evenNumber <= 10){System.out.println(evenNumber);evenNumber += 2;}Answer:8)Write Java code that uses a for statement to sum the numbers from 1 through 50. Display the totalsum to the console.Answer:.Chapter 3 Flow of Control 7 int sum = 0;for(int i=1; i <= 50; i++){sum += i;}System.out.println("The total is: " + sum);9)What is the output of the following code segment?public static void main(String[] args){int x = 5;System.out.println("The value of x is:" + x);while(x > 0){x++;}System.out.println("The value of x is:" + x);}Answer:.10)Discuss the differences between the break and the continue statements when used in loopingmechanisms.Answer:When the break statement is encountered within a looping mechanism, the loopimmediately terminates. When the continue statement is encountered within a looping mechanism, the current iteration is terminated, and execution continues with the next iteration of the loop.8Programming projects:1. Write a complete Java program that prompts the user fora series of numbers to determine the smallestvalue entered. Before the program terminates, display the smallest value.Answer:import java.util.Scanner;public class FindMin{public static void main(String[] args){Scanner keyboard = new Scanner(System.in);int smallest = 9999999;String userInput;boolean quit = false;System.out.println("This program finds the smallest number"+ " in a series of numbers");System.out.println("When you want to exit, type Q");.Chapter 3 Flow of Control 9 while(quit != true){System.out.print("Enter a number: ");userInput = keyboard.next();if(userInput.equals("Q") || userInput.equals("q")){quit = true;}else{int userNumber = Integer.parseInt(userInput);if(userNumber < smallest)smallest = userNumber;}}System.out.println("The smallest number is " + smallest);System.exit(0);}10}2. Write a complete Java program that uses a for loop to compute the sum of the even numbers and thesum of the odd numbers between 1 and 25.public class sumEvenOdd{public static void main(String[] args){int evenSum = 0;int oddSum = 0;//loop through the numbersfor(int i=1; i <= 25; i++){if(i % 2 == 0){//even numberevenSum += i;}else{oddSum += i;.Chapter 3 Flow of Control 11 }}//Output the resultsSystem.out.println("Even sum = " + evenSum);System.out.println("Odd sum = " + oddSum);}}/*** Question2.java** This program simulates 10,000 games of craps.* It counts the number of wins and losses and outputs the probability* of winning.** Created: Sat Mar 05, 2005** @author Kenrick Mock* @version 1*/public class Question2{private static final int NUM_GAMES = 10000;/*** This is the main method. It loops 10,000 times, each simulate* a game of craps. Math.random() is used to get a random number,* and we simulate rolling two dice (Math.random() * 6 + 1 simulates* a single die).*/public static void main(String[] args){// Variable declarationsint numWins = 0;12int numLosses = 0;int i;int roll;int point;// Play 10,000 gamesfor (i=0; i<="" p="">{// Simulate rolling the two dice, with values from 1-6roll = (int) (Math.random() * 6) + (int) (Math.random() * 6) + 2;// Check for initial win or lossif ((roll == 7) || (roll == 11)){numWins++;}else if ((roll==2) || (roll==3) || (roll==12)){numLosses++;}else{// Continue rolling until we get the point or 7point = roll;do{roll = (int) (Math.random() * 6) + (int) (Math.random() * 6) +2;if (roll==7){numLosses++;}else if (roll==point){numWins++;}} while ((point != roll) && (roll != 7));}}// Output probability of winningSystem.out.println("In the simulation, we won " + numWins +" times and lost " + numLosses + " times, " +" for a probability of " +(double) (numWins) / (numWins+numLosses));}} // Question2.Chapter 3 Flow of Control 13 /*** Question6.java** Created: Sun Nov 09 16:14:44 2003* Modified: Sat Mar 05 2005, Kenrick Mock** @author Adrienne Decker* @version 2*/import java.util.Scanner;public class Question6{public static void main(String[] args){Scanner keyboard = new Scanner(System.in);while ( true ){System.out.println("Enter the initial size of the green crud" + " in pounds.\nIf you don't want to " +"calculate any more enter -1.");int initialSize = keyboard.nextInt();if ( initialSize == -1){break;} // end of if ()System.out.println("Enter the number of days: ");int numDays = keyboard.nextInt();int numOfRepCycles = numDays / 5;int prevPrevGen = 0;int prevGen = initialSize;int finalAnswer = initialSize;for ( int i = 0; i < numOfRepCycles; i++ ){finalAnswer = prevPrevGen + prevGen;14prevPrevGen = prevGen;prevGen = finalAnswer;} // end of for ()System.out.println("The final amount of green crud will be: "+ finalAnswer + " pounds.\n");} // end of while ()}} // Question6/*** Question7.java** Created: Sun Nov 09 16:14:44 2003* Modified: Sat Mar 05 2005, Kenrick Mock** @author Adrienne Decker* @version 2*/import java.util.Scanner;public class Question7{public static void main(String[] args){Scanner keyboard = new Scanner(System.in);String line;do{System.out.println("Enter the value of X for this calculation."); double x = keyboard.nextDouble();double sum = 1.0;double temp = 1.0;for ( int n = 1; n <= 10; n++){System.out.print("For n equal to: " + n.Chapter 3 Flow of Control 15+ ", the result of calculating e^x is: ");for ( int inner = 1; inner <= n; inner++){temp = 1.0;for ( double z = inner; z > 0; z--){temp = temp * (x/z);} // end of for ()sum += temp;} // end of for ()System.out.println(sum);sum = 1.0;} // end of for ()System.out.print("For n equal to 50, the result of " + "calculating e^x is: ");for ( int n = 1; n <= 50; n++){temp = 1.0;for ( double z = n; z > 0; z--){temp = temp * (x/z);} // end of for ()sum += temp;} // end of for ()System.out.println(sum);sum = 1;System.out.print("For n equal to 100, the result of " + "calculating e^x is: ");for ( int n = 1; n <= 100; n++){temp = 1.0;for ( double z = n; z > 0; z--){temp = temp * (x/z);} // end of for ()sum += temp;} // end of for ()System.out.println(sum);System.out.println("\nEnter Q to quit or Y to go again.");16keyboard.nextLine(); // Clear bufferline = keyboard.nextLine();} while (line.charAt(0)!='q' && line.charAt(0)!='Q'); // end of while () }} // Question7.。
奥鹏14秋《Java语言程序设计》作业3满分答案

B. Visual Basic
C. C++
D. C
?
正确答案:D
4.下列特点中,是Java虚拟机执行的特点之一的是_______。
A.字节代码
B.多进程
C.静态链接
D.编译
?
正确答案:A
5.要激活一个Java小程序Applet,需要有_______。
A. .class文件
B. .exe文件
C. .html文件
D. .java文件
?
正确答案:C
6.在Java中,程序先由编译器转换为_______。
A.机器码
B.标准字节代码
C.汇编程序
D.标准比特代码
?
正确答案:B
7.下列说法中,正确的一项是_______。
A. Java语言是以类为程序的基本单位的
B. Java语言是部分大小写的
C.多行解释语句必须以//开始
14秋《Java语言程序设计》作业3
一,单选题
1.下列哪一项不属于面向对象程序设计的基本要素?
A.类
B.对象
C.方法
D.安全
?
正确答案:D
2.在创建Applet应用程序时,需要用户考虑的问题是_______。
A.如何创建窗口
B.绘制的图形在窗口中的位置
C.程序的框架
D.事件处理
?
正确答案:B
3.下列不属于面向对象语言的是_______。
D.在Java语言中,公共类的源文件和该类名可以不相同
?
正确答案:A
8. Java语言使用_______进行解释执行。
A.字节码
B.机器码
练习3及答案

选择第7题C
选择第8题D
选择第9题A
选择第10题A
选择第11题B
选择第12题B
二、填空题答案
填空第1题protected;default;public
填空第2题Object
填空第3题ng.Character;ng.Boolean
填空第4题
What a pleasure!
void foo(double d,final float f){
String s;
final boolean b;
class Inner{
void methodInner(){
System.out.println("in the Inner");
}
}
}
public static void main(String args[])
{
Outer1 me=new Outer1();
me.foo(123,123);
System.out.println("outer");
}
}
A in the Inner outer
B outer
C in the Inner
D编译不通过
二、填空题
1.Java中类成员的限定词有以下几种:private, public,_____________,_____________。其中,_____________的限定的范围最大。
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy.MM.dd G 'at' hh:mmTime = new Date();
try {
java三级理论题--附答案

"程序员〔JAVA〕"〔三级〕的考试方式分为根本概念,应用操作和编码实践考核。
根本概念和应用操作考试采用闭卷计算机机考方式,编码实践考核采用现场实际操作方式。
根本概念考试占30%应用操作考试和编码实践考试各占35%,总分100分。
成绩皆达60分及以上者为合格一、单项选择题1.下面关于aja*的工作原理描述是错误的选项是.A. 相当于在用户和效劳器之间加了一个中间层,使用户操作与效劳器响应异步化B. 把一些效劳器负担的工作转嫁到客户端,利用客户端闲置的处理能力来处理C. 不是所有的用户请求都提交给效劳器D. 所有数据由Aja*引擎代为向效劳器提交请求2.下面关于使用aja*的理由说法错误的选项是.A. 跨平台,跨浏览器B. 基于公开标准C. 以用户体验和可用性为主D 效劳端技术确实定性3.下面关于aja*中就绪状态描述错误的选项是A. 0:请求没有发出〔在调用open() 之前〕。
B.1:请求已经建立但还没有发出〔调用send() 之前〕。
C. · 2:请求已经发出正在处理之中〔这里通常可以从响应得到容头部〕。
D. · 3:响应已完成,可以效劳器响应并使用它。
4.下面关于aja*的构成描述正确的选项是:A. aja*就是asp+java+*mlB. aja*就是applet+jsp+*mltC. aja*就是applet+java+htmltD. aja*就是javascript+*ml+dom+dhtml5.以下对请求头方法的功能说明错误的选项是:A.getCookies方法会返回Cookie头的容,解析后会存放在Cookie对象的数组中B.getHeaderNames方法返回当前请求的所有头的名称的字符串对象C.getMethod方法返回请求方法,通常是GET或者POSTD.getProtocol返回版本号6.下面关于*ml Request对象的方法描述错误的选项是:A.open():建立到效劳器的新请求B.send():向效劳器发送请求。
Java习题三

1.有关类Demo,哪句描述是正确的?public class Demo extends Base{private int count;public Demo(){System.out.println("A Demo object has been created");}protected void addOne(){count++;}}①当创建一个Demo类的实例对象时,count的值为0。
②当创建一个Demo类的实例对象时,count的值是不确定的。
③超类对象中可以包含改变count 值的方法。
④Demo的子类对象可以访问count。
2.当编译和运行下列程序段时,会发生什么?class Base {}class Sub extends Base {}class Sub2 extends Base {}public class Cex{public static void main(String argv[]){Base b = new Base();Sub s = (Sub) b;}}①通过编译和并正常运行。
②编译时出现例外。
③编译通过,运行时出现例外。
ClassCaseException3.如果任何包中的子类都能访问超类中的成员,那么应使用哪个限定词?①public②private③protected④transient4.下面的哪个选项是正确的?class ExSuper{String name;String nick_name;public ExSuper(String s,String t){name = s;nick_name = t;}public String toString(){return name;}}public class Example extends ExSuper{public Example(String s,String t){super(s,t);}public String toString(){return name +"a.k.a"+nick_name;}public static void main(String args[]){ExSuper a = new ExSuper("First","1st");ExSuper b = new Example("Second","2nd");System.out.println("a is"+a.toString());System.out.println("b is"+b.toString());}}①编译时会出现例外。
(完整版)Java程序设计习题附答案(三)

第三部分面向对象程序设计1、引用数据类型变量具有基本属性为(ABCD)A、变量名B、数据类型C、存储单元D、变量值。
2、面向对象技术的特性是(ACD)A、继承性B、有效性C、多态性D、封装性。
3、下列哪个命题为真?(C)A、所有类都必须定义一个构造函数。
B、构造函数必须有返回值。
C、构造函数可以访问类的非静态成员。
D、构造函数必须初始化类的所有数据成员。
4、关于子类与父类关系的描述正确的是(ACD)A、子类型的数据可以隐式转换为其父类型的数据;B、父类型的数据可以隐式转换为其子类型的数据;C、父类型的数据必须通过显示类型转换为其子类型的数据;D、子类型实例也是父类型的实例对象。
5、下列哪一项说法最好地描述了Java中的对象?(C)A、对象是通过import命令引入到程序中的所有事情B、对象是方法的集合,这些方法在小程序窗口或应用程序窗口中产生图形元素,或者计算和返回值C、对象是一种数据结构,它具有操作数据的方法D、对象是一组具有共同的结构和行为的类6、下面哪个关键字不是用来控制对类成员的访问的?(C)A、publicB、protectedC、defaultD、private7、Java语言正确的常量修饰符应该是(D)A、finalB、static finalC、staticD、public static final;8、接口的所有成员域都具有public 、static和final 属性。
9、接口的所有成员方法都具有public 和abstract 属性。
10、编译下列源程序会得到哪些文件?(C)class A1{}class A2{}public class B{public static void main(String args[]){}}A) 只有B.classB)只有A1.class和A2.class文件C)有A1.class、A2.class和B.class文件D) 编译不成功11、下列哪种说法是正确的?(A)A、私有方法不能被子类覆盖。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
}
catch(IOException e)
{ e.printStackTrace();
}
}
}
A. ncr B. ncr exam C. ncr exa D. ncr exami
17.在下列程序的横线下填入正确的语句,实现RandomAccessFile类的使用。( C )
int i=1;
while(pointerOfFile<lengthOfFile)
{ str=rf.readLine();
System.out.println((i++)+” ”+str);
pointerOfFile=__________;
}
rf.close();
}
catch(IOException e)
package ch;
import java.io.*;
public class ex17
{ static String filename=”ch\\file17.txt”;
public static void main(String[] args)
{ try{ String str=”zyxwvut”;
A.从内存流向硬盘的数据流B.从键盘流向内存的数据流
C.从键盘流向显示器的数据流D.从网络流向显示器的数据流
解析:常用的外部设备一般包括:键盘、显示器、硬盘、扫描仪、打印机和网络等。而中央处理器通常包括控制器和内存。输入流是指由外设流向中央处理器的数据流,因此只有选项B从键盘外设流向内存的数据流是输入流。选项C和选项D都是从外设流向外设的数据流,这一般是经过了先输入,再输出的过程,因此不属于输入流。
return fos;
}
static void writeFile(FileOutputStream o)throws IOException
{ DataOutputStream dos=null;
try{ dos=new DataOutputStream(o);
dos.writeBytes(“Hello!”);
void f() throws exception1,exception2
选项A中,程序中通过使用throw子句再次抛出异常,如:
IOException ioe=new IOException;
throw ioe;
9.当方法产生该方法无法确定该如何处理的异常时,应该如何处理?( A )
A.声明异常B.捕获异常C.抛出异常D.嵌套异常
byte[] b;
FileOutputStream out=__________;
{ e.printStackTrace();
}
}
}
A. RandomAccessFile(“r”,”ch/file.dat”) B. RandomAccessFile(“ch/file.dat”,”read”)
C. RandomAccessFile(“ch/file.dat”,”r”) D. RandomAccessFile(“read”,”ch/file.dat”)
18.下列程序使用RandomAccessFile类打印出自己的源文件,并且在每一行前面加上行号,选择正确的一个语句填入程序的横线处。( A )
package ch;
import java.io.*;
public class ex16
{ public static void main(String[] args)
13.下列哪一项不是File类的功能?( D )
A.新建文件B.新建目录C.删除文件D.修改文件
14.下列程序实现了新建一个文件file12.txt,并且在文件中写入字节数据。执行程序之后,文件file12.txt中的内容是( A )
package ch7;
import java.io.*;
public class ex12
{ try{ RandomAccessFile rf=new RandomAccessFile(“ch\\ex16.java”,”r”);
String str;
long pointerOfFile=0;
long lengthOfFile=rf.length();
rf.seek(pointerOfFile);
A.程序编译错误B.程序语法错误
C.程序自定义的异常事件D.程序编译或者运行时发生的异常事件
8.抛出异常时,应该使用下列哪个子句?( A )
A. throw B. catch C. finally D. throws
解析:选项B中,catch子句应该与try子句匹配使用,但不是用来抛出异常的;选项C中,finally子句用于为异常处理提供统一的出口;比较有迷惑性的是选项D,throws子句是用来声明异常的,它属于方法声明的一部分,位于自变量(参数)列表的后面,如:
训练题三
选择题
下面关于Java的说法不正确的是( A )
abstract和final能同时修饰一个类
抽象类做抽象父类,也可以做抽象子类和抽象方法
抽象方法不一定在抽象类中,也可以在接口中
声明为final的方法不能在子类中重新定义
下面关于接口的说法中不正确的是( C )
接口所有的方法都是抽象的
接口所有的方法一定都是public属性的
在方法中监测到错误但不知道如何处理错误时,方法就声明一个异常
一个程序抛出异常,任何其他在运行中的程序都可以捕获
任何没有被程序捕获的异常将最终被默认处理程序处理
解析:不是任何其他在运行中的程序都可以捕获一个程序抛出的异常,而是由Java虚似机中的标准异常处理程序来捕获。
11.下列数据流中,属于输入流的一项是( B )
用于定义接口的关键字是implements
接口是Java中的特殊类,包含常量和抽象方法
关于内部类下列说法不正确的是( A )
内部类不能有自己的成员方法和成员变量
内部类可用abstract修饰定义为抽象类,也可以用private或protected定义
内部类可作为其他类的成员,而且可访问它所在类的成员
A. public B. public和protected C. private D.任意修饰符都可以
6.下面哪一个Java源文件代码段是不正确的?( B )
A. package testpackage; B. import java.io.*;
public class Test{ } package testpackage;
BufferedReader br=new BufferedReader(fr);
String str;
while((str=br.readLine())!=null)
{ float f=Float.parseFloat(str);
System.out.println(f);
}
br.close();
package ch;
import java.io.*;
public class ex15
{ public static void main(String[] args)
{ try{ RandomAccessFile in=_________________;
in.close();
}
catch(Exception e)
12.下列程序从标准输入设备键盘读入一个字符,然后再输出到显示器上,选择正确的一项填入x处,使程序编译通过。( B )
import java.io.*;
public class Test10
{ public static void main(String[] args)
{ char ch;
try{ //x
System.out.println(ch);
}
catch(IOException e)
{ e.printStackTrace();
}
}
}
A. ch=System.in.read(); B. ch=(char)System.in.read();
C. ch=(char)System.in.readln(); D. ch=(int)System.in.read();
public class Test{ }
C. import java.io.*; D. import java.io.*;
class Person{ } import java.awt.*;
public class Test{ } public class Test{ }
7.关于异常的含义,下列描述中最正确的一个是( D )
{ static String file=”ch7\\file12.txt”;
public static void main(String[] args)
{ try{ FileOutputStream fos=createFile();
writeFile(fos);
}
catch(IOException ioe)
{ try{ File f=new File(fileName);
FileInputStream fis=new FileInputStream(f);
byte[] b=new byte[8];
int i=fis.read(b);
fis.close();
for(int j=0;j<b.length;j++)
解析:在Java语言中,一个方法生成一个异常时,该方法不一定能确定应该如何处理异常,如找不到文件之类的异常,必须将异常传递给调用方法,由调用它的方法来处理这类异常,通过方法声明异常抛出,让异常对象可以从调用堆栈向后传递,直到有相应的方法捕获它为止。