《JA V A程序设计》作业答案一、选择题1、编译HelloWorld.java的正确命令是:C) javac HelloWorld.java2、正确运行HelloWorld.java的正确命令是:A)java HelloWorld3、下面程序代码,使用多行注释正确的是:C)/* int k=9;int j=8;k = k + j; */4、long型的取值范围是:D)-263~263-15、下面不属于Java保留字的是:C)malloc6、下面属于非法的Java标识符的是:D) abc-d7、对与System.out.println()语句解释合理的是:C)执行后输出一个空行8、阅读下面的代码,回答问题,for( m = 0 ; m > -2 ; m -- ){….}For循环执行多少次:C)29、阅读下面的代码,回答问题,for( m = 0; m < 5; m++ ){System.out.print( m + "," );if( m == 3 )break;}执行结果是:C)0,1,2,3,10、阅读下面的代码,回答问题,public class Ex{int x = 1;void m(){int x = 3;System.out.print( "x= " + x);}public static void main( String[] args ){Ex ex = new Ex();ex.m();}}执行结果是:B)x=311、下面语句在编译时不会出现错误信息的是:a) float f = 1.3; b) char c = "a"; c) byte b = 257; d) boolean b = null; e) int i = 10;12、编译和运行下面的代码,会有什么结果产生:public class MyClass {public static void main(String arguments[]){amethod(arguments);}public void amethod(String[] arguments){System.out.println(arguments);System.out.println(arguments[1]);}}a) 错误,静态方法不能直接引用非静态方法b) 错误,主方法有错误c) 错误,数据定义有错误d) 方法amethod必须被声明为String型13、编译期间会出错的是:a) import java.awt.*;package Mypackage;class Myclass {}b) package MyPackage;import java.awt.*;class MyClass{}c) /*This is a comment */package MyPackage;import java.awt.*;class MyClass{}14、byte型的变量的表示范围为:a) -128 to 127 b) (-2 power 8 )-1 to 2 power 8c) -255 to 256 d) 依赖Java虚拟机而定15、在命令行运行命令:java myprog good morning会有什么结果显示出来:public class myprog{public static void main(String argv[]){System.out.println(argv[2])}}a) myprog b) good c) morningd) Exception raised: "ng.ArrayIndexOutOfBoundsException: 2"16、下面不是Java保留字的是:a) if b) then c) goto d) while17、下面属于非法的标识符的是:a) 2variable b) variable2 c) _whatavariable d) _3_ e) $anothervar18、编译下面的代码,会有什么结果产生:public class MyClass{static int i;public static void main(String argv[]){System.out.println(i);}}a) 错误,变量i 没有初始化b) null c) 1 d) 019、编译运行下面的代码,会有什么结果产生:public class Q {public static void main(String argv[]){int anar[]= new int[]{1,2,3};System.out.println(anar[1]);}}a) 1 b) 3 c) 2 d) 错误,数组anar的长度没有定义20、编译运行下面的代码,会有什么结果产生:public class Q {public static void main(String argv[]){int anar[]= new int[5];System.out.println(anar[0]);}}a) 编译错误b) null c) 0 d) 5Arrays are always initialised when they are created. As this is an array of ints it will be initalised with zeros.21、编译运行下面的代码,会有什么结果产生:abstract class MineBase {abstract void amethod();static int i;}public class Mine extends MineBase{public static void main(String argv[]){int[] ar = new int[5];for(i = 0;i < ar.length;i++)System.out.println(ar[i]);}}a) 五个0被输出b) 错误,ar使用前没有初始化c) 错误,类Mine 必须要被声明为抽象的类d) IndexOutOfBoundes Error i22、编译运行下面的代码,会有什么结果产生:int i = 1;switch (i) {case 0:System.out.println("zero");break;case 1:System.out.println("one");case 2:System.out.println("two");default:System.out.println("default");}a) one b) one, default c) one, two, default d) default23、编译运行下面的代码,会有什么结果产生:int i = 9;switch (i) {default:System.out.println("default");case 0:System.out.println("zero");break;case 1:System.out.println("one");case 2:System.out.println("two");}a) default b) default, zero c) error default clause not defined d) no output displayed24、下面不会在编译时出错的是:a) int i=0;if(i){System.out.println("Hello");}b) boolean b = true;boolean b2 = true;if(b==b2)System.out.println("So true");c) int i=1;int j = 2;if(i ==1&j==2)System.out.println("OK");d) int i=1;int j = 2;if(i ==1 &| j==2)System.out.println("OK");25、编译运行下面的代码,会有什么结果产生,注意,在当前目录里没有文件Hello.txt:import java.io.*;public class Mine{public static void main(String argv[]){Mine m = new Mine();System.out.println(m.amethod());}public int amethod(){try {FileInputStream dis = new FileInputStream("Hello.txt");}catch (FileNotFoundException fne) {System.out.println("No such file found");return -1;}catch(IOException ioe) {} finally{System.out.println("Doing finally");}return 0;}}a) No such file found b)No such file found ,-1c) No such file found, doing finally, -1 d) 026、建立一个HTML去显示一个applet时,必须要定义的tags是:a) name, height, width b) code, name c) codebase, height, width d) code, height, width27、编译运行下面的代码,会有什么结果产生:class Base {}class Sub extends Base {}public class CEx{public static void main(String argv[]){Base b = new Base();Sub s = (Sub) b;}}a) Compile and run without error b) Compile time Exception c) Runtime Exception28、用下面的HTML去显示applet:MgAp,控制台会有什么结果显示:<applet code = MgAp.class height=400 width=400 parameter HowOld=30 ></applet>import java.applet.*;import java.awt.*;public class MgAp extends Applet{System.out.println(getParameter("age"));}}a) Error no such parameter b) 0 c) null d) 30参数age没有获得从HTML给定的值,因此显示null.29、Math类包含在哪个包里:a) java.io b) java.awt c) ng d) java.applet30、编译运行下面的代码,会有什么结果产生://Code startimport java.awt.*;public class Butt extends Frame{public static void main(String argv[]){Butt MyBut= new Butt();}Butt(){Button HelloBut = new Button("Hello");Button ByeBut = new Button("Bye");add(HelloBut);add(ByeBut);setSize(300,300);setVisible(true);}}//Code enda) 两个按钮并列占据整个frame b) Hello按钮占据整个frame c) Bye按钮占据整个frame The default layout manager for a Frame is a border layout. If directions are not given (ie North, South, East or West), any button will simply go in the centre and occupy all the space. An additional button will simply be placed over the previous button. What you would probably want in a real example is to set up a flow layout as insetLayout(new FlowLayout()); which would.31、Java程序是否可以在除了Windows的其他平台上运行:A)不可以B)可以32、对于一个Java源文件,import, class定义以及package正确的顺序是:A)package, import, class B)class, import, packageC)import, package, class D) package, class, import33、那个方法可以不能被String型对象调用:Which methods can be legally applied to a string object?A)equals(String) B)toString() B)t rim() D)round()34、main方法中的参数正确的定义是:A)String[] args [] B)String [] args B)f loat args [] D)String args35、在命令行执行:java Example 12 3e you 45.6那么main方法的参数args数组的第一个元素args[0]的内容是:Java B)Example C)12 D)3e36、下面那个不是Java的关键字:A)goto B)malloc B)e xtends D)while37、编译下面的代码,结果是:public static void main (String args []) {int age;age = age + 1;System.out.println("The age is " + age);}}A)编译运行都没有结果输出B)编译运行后输出The age is 1C)编译通过,但运行时会出错D)编译不通过38、下面合法的char型值是:A)…a‟ B)"a" C)n ew Character(a) D)D)\000a 39、能够给一个byte型变量赋值的范围是:What is the legal range of a byte integral type?A)0 - 65, 535 B)(–128) – 127 C)(–32,768) – 32,767 D)(–256) – 255 40、下面哪个是非法的:Which of the following is illegal:A)int i = 32; B)float f = 45.0; C)double d = 45.0; D)char c = …u‟41、编译下面的代码,其结果是:public class Test {static int age;public static void main (String args []) {age = age + 1;System.out.println("The age is " + age);}}A)编译运行都没有结果输出B)编译运行后输出The age is 1C)编译通过,但运行时会出错D)编译不通过42、下面正确的是:Which of the following are correct?A)128 >> 1 为64 B)128 << 1为64 C)128 >> 1为–64 D)128 << 1为–64 43、下面返回true的是:A)"john" != "john" B)"john". equals("john")C)"john" = "john" D)"john".equals(new Button("john"))44、下面哪条语句不会导致运行时错误:A)"john" + " was " + " here" B)"john" + 3C)3 + 5 D)5 + 5.5E)以上四个都不会导致运行时错误45、下面哪个是位运算符:A)>= B)|| C)&& D)|46、下面那个是可以被接受的:A)Object o = new Button("A"); B)Boolean flag = true;C)Panel p = new Frame(); D)Frame f = new Panel();47、编译运行下面代码,其结果是:public class Test {static int total = 10;public static void main (String args []) {new Test();}public Test () {System.out.println("In test");System.out.println(this);int temp = this.total;if (temp > 5) {System.out.println(temp);}}}A)此类不会被编译B)编译出错在第2行C)编译出错在第9行D)编译通过,运行后输出:1048、下面正确的是:A)String temp [] = new String {"j" "a" "z"}; B)String temp [] = { "j " " b" "c"};C)String temp = {"a", "b", "c"}; D)String temp [] = {"a", "b", "c"};49、下面定义了一个抽象方法add,正确的是:What is the correct declaration of an abstract method that is intended to be public:A)public abstract void add(); B)public abstract void add() {}C)public abstract add(); D)public virtual add();500、在什么情况下,你会获得一个缺省的构造方法:A)当你定义任何类的时候B)当类没有其他构造方法的时候C)当你至少定义了一个构造方法的时候51、阅读下面的代码:public class Test {…}那个是这个类的合法构造方法:A)public void Test() {…} B)public Test() {…}C)public static Test() {…}D)public static void Test() {…}52、Java编译器不能接受的是:A)if (2 == 3) System.out.println("Hi"); B)if (2 = 3) System.out.println("Hi");C)if (true) System.out.println("Hi"); D)if (2 != 3) System.out.println("Hi");53、若一个方法包含了一段可能引起异常的代码,那么此方法想要调用他的方法去处理这个潜在的异常的正确方法是:A)throw Exception B)throws ExceptionC)new Exception D)Don't need to specify anything54、若给参数a传递4,给b传递0,那么下面程序的结果是:public void divide(int a, int b) {try {int c = a / b;} catch (Exception e) {System.out.print("Exception ");} finally {System.out.println("Finally");}A)Prints out: Exception Finally B)Prints out: FinallyC)Prints out: Exception D)No output55、编写一个方法重载题目给出的方法add,那么他的返回类型可以是:public void add(int a) {…}A)void B)int C)可以是任何类型D)String56、合法的Java标示符有:A. IdoLikeTheLongNameClassB. $byteC. const //保留字D. _okE. 3_case 57下面这段代码中定义的类在不同的文件中:class Vehicle {public void drive() {System.out.println("Vehicle: drive");}}class Car extends Vehicle {public void drive() {System.out.println("Car: drive");}}public class Test {public static void main (String args []) {Vehicle v;Car c;v = new Vehicle();c = new Car();v.drive();c.drive();v = c;v.drive();}}编译运行的结果是:A)Generates a Compiler error on the statement v= c;B)Generates runtime error on the statement v= c;C)输出:Vehicle: drive Car: driveCar: drive D)输出Prints out: Vehicle: driveCar: driveVehicle: drive58、考虑下面的这个类:1. public class Test {2. void test(int i) {3. System.out.println("I am an int.");4. }5. void test(String s) {6. System.out.println("I am a string.");7. }8.9. public static void main(String args[]) {10. Test t=new Test();11. char ch=‟y‟;12. t.test(ch);13. }14. }哪一个说明是正确的:A. 第5行编译出错,因为方法test不能被重载B. 第12行编译出错,因为方法test的参数不是char类型C. 编译运行通过,输出:I am an int.D. 编译运行通过,输出:I am a String.点评:在第12行,16位长的char型变量ch在编译时会自动转化为一个32位长的int型,并在运行时传给void test(int i)方法。