java程序设计作业答案

上传人:痛*** 文档编号:89320697 上传时间:2022-05-12 格式:DOC 页数:19 大小:72.50KB
收藏 版权申诉 举报 下载
java程序设计作业答案_第1页
第1页 / 共19页
java程序设计作业答案_第2页
第2页 / 共19页
java程序设计作业答案_第3页
第3页 / 共19页
资源描述:

《java程序设计作业答案》由会员分享,可在线阅读,更多相关《java程序设计作业答案(19页珍藏版)》请在装配图网上搜索。

1、JAVA程序设计作业答案一、选择题1、 编译HelloWorld.java的正确命令是:19 / 19C) javac HelloWorld.java2、 正确运行HelloWorld.java的正确命令是:A)java HelloWorld3、 下面程序代码,使用多行注释正确的是:C) /* int k=9; int j=8; k = k + j; */4、 long型的取值范围是:D)-263263-15、 下面不属于Java保留字的是:C)malloc6、 下面属于非法的Java标识符的是:D) abc-d7、 对与System.out.println()语句解释合理的是:C)执行后输出

2、一个空行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 Exint x = 1;void m()int x = 3; System.out.print( x= + x);public static void main( String args )Ex ex = new Ex();

3、ex.m();执行结果是:B)x=3 11、下面语句在编译时不会出现错误信息的是: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);

4、System.out.println(arguments1);a) 错误,静态方法不能直接引用非静态方法 b) 错误,主方法有错误c) 错误,数据定义有错误 d) 方法amethod必须被声明为String型13、编译期间会出错的是:a) import java.awt.*; package Mypackage; class Myclass b) package MyPackage; import java.awt.*; class MyClassc) /*This is a comment */ package MyPackage; import java.awt.*; class MyCl

5、ass14、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 myprogpublic static void main(String argv) System.out.println(argv2)a) myprog b) good c) morningd) Exception raised: java.lang.ArrayIndexOutOfBo

6、undsException: 216、下面不是Java保留字的是:a) if b) then c) goto d) while17、下面属于非法的标识符的是:a) 2variable b) variable2 c) _whatavariable d) _3_ e) $anothervar18、编译下面的代码,会有什么结果产生:public class MyClassstatic int i; public static void main(String argv)System.out.println(i); a) 错误,变量i 没有初始化 b) null c) 1 d) 019、编译运行下面的

7、代码,会有什么结果产生:public class Q public static void main(String argv)int anar= new int1,2,3;System.out.println(anar1); a) 1 b) 3 c) 2 d) 错误,数组anar的长度没有定义20、编译运行下面的代码,会有什么结果产生:public class Q public static void main(String argv)int anar= new int5;System.out.println(anar0); a) 编译错误 b) null c) 0 d) 5 Arrays a

8、re 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 MineBasepublic static void main(String argv)int ar = new int5;for(i = 0;i ar.length;

9、i+)System.out.println(ari);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, d

10、efault 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 displa

11、yed24、下面不会在编译时出错的是: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 =1j=2) System.out.println(OK);d) int i=1; int j = 2; if(i =1 &| j=2) System.out.println(OK);25、编译运行下面的代码,会有什么结果产生,注意,在当前目录里没有文件Hell

12、o.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(IOExce

13、ption ioe) finallySystem.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、编译运行下面的代码,会有什么结果产生:clas

14、s Base class Sub extends Base public class CExpublic 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 Exception 28、用下面的HTML去显示applet:MgAp,控制台会有什么结果显示: import java.applet.*;import java.awt.*;public class MgAp extend

15、s Appletpublic void init()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) java.lang d) java.applet30、编译运行下面的代码,会有什么结果产生:/Code startimport java.awt.*;public class Butt extends Framepublic static vo

16、id 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 layou

17、t. 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 FlowLayo

18、ut(); which would.31、Java程序是否可以在除了Windows的其他平台上运行:A) 不可以 B)可以32、对于一个Java源文件,import, class定义以及package正确的顺序是:A)package, import, class B)class, import, package C)import, package, class D) package, class, import 33、那个方法可以不能被String型对象调用:Which methods can be legally applied to a string object?A) equals(St

19、ring) B)toString()B) trim() D)round() 34、main方法中的参数正确的定义是:A) String args B)String args B) float args D)String args35、在命令行执行:java Example 12 3e you 45.6 那么main方法的参数args数组的第一个元素args0的内容是:Java B)ExampleC)12 D)3e36、下面那个不是Java的关键字:A) goto B)malloc B) extends D)while 37、编译下面的代码,结果是:public class Test publi

20、c static void main (String args ) int age;age = age + 1;System.out.println(The age is + age);A)编译运行都没有结果输出 B)编译运行后输出 The age is 1 C)编译通过,但运行时会出错 D)编译不通过 38、下面合法的char型值是:A)a B)a C) new Character(a) D) D)000a 39、能够给一个byte型变量赋值的范围是:What is the legal range of a byte integral type?A)0 - 65, 535 B)(128) 1

21、27 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 = u41、编译下面的代码,其结果是:public class Test static int age;public static void main (String args ) age = age + 1;System.out.println(The age is + age);A)编译运行都没有结果输出 B)编译运

22、行后输出 The age is 1 C)编译通过,但运行时会出错 D)编译不通过42、下面正确的是:Which of the following are correct?A)128 1 为 64 B)128 1为64 D)128 = 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 = 1

23、0;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)Strin

24、g 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)当类

25、没有其他构造方法的时候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)

26、 System.out.println(Hi); 53、若一个方法包含了一段可能引起异常的代码,那么此方法想要调用他的方法去处理这个潜在的异常的正确方法是:A)throw Exception B)throws Exception C)new Exception D)Dont need to specify anything 54、若给参数a传递4,给b传递0,那么下面程序的结果是:public void divide(int a, int b) try int c = a / b; catch (Exception e) System.out.print(Exception ); finall

27、y System.out.println(Finally);A)Prints out: Exception Finally B)Prints out: Finally C)Prints out: Exception D)No output 55、编写一个方法重载题目给出的方法add,那么他的返回类型可以是:public void add(int a) A)void B)int C)可以是任何类型 D)String56、合法的Java标示符有:A. IdoLikeTheLongNameClass B. $byte C. const /保留字 D. _okE. 3_case57下面这段代码中定义的

28、类在不同的文件中: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();编译运行

29、的结果是:A)Generates a Compiler error on the statement v= c; B)Generates runtime error on the statement v= c; C)输出: Vehicle: driveCar: driveCar: drive D)输出Prints out: Vehicle: drive Car: driveVehicle: drive58、考虑下面的这个类:1. public class Test 2. void test(int i) 3. System.out.println(I am an int.);4. 5. voi

30、d 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在

31、编译时会自动转化为一个32位长的int型,并在运行时传给void test(int i)方法。59、一个类Outer,其内部定义了一个内部类Inner,在Outer类的主方法中创建内部类对象的正确方法是:A)Inner inner = new Inner() B)Outer.Inner inner = (new Outer().new Inner()C)Outer inner = new Inner() D)Inner inner = new Outer()60、当x的值为2时,下面代码的运行结果是:switch (x) case 1:System.out.println(1);case 2:

32、case 3:System.out.println(3);case 4:System.out.println(4);A)什么都不会输出 B)3 C)34 D)1341、4) double d=999d;2、2) new3、1) System.out.println(1+1);4、2) Math.max(7,9);5、1) byte 的表示范围为 -128 to 1276、2) 编译运行通过,输出 Base7、2) public static void amethod()8、1)char c=1;System.out.println(c1);9、3) transient10、2) 输出 “Hel

33、lo Crowle”二、改错1、答案:public static void main(String args)2、答案:public class Ex2 int j; public static void main(String args) System.out.println(“Hello World!”);3、答案:z = a + b ;4、答案:1)int b = 200;2) float f = 8.9f3) char c = h4) boolean b = true5、答案:public class Ex5int x = 1; int y = 1; x = 2;6、答案:public

34、 class Ex6int x = 1; int y = 1; public static void main(String args ) System.out.print( “Hello” ); 7、package mycode.ide1;package mycode.ide2;public class Ex1 .答案:不能有两个package8、import mycode.ide1.*;package mycode.ide2;public class Ex2 .答案:imports和package 顺序颠倒9、 public abstract class Ex3void m1() Syst

35、em.out.println( “m1” );void m2(); 答案:方法m2应该被声明为abstract,或者给出m2的方法体10、public interface Ex4int j;void m1();void m2(); 答案:接口中的变量都是常量,应该给他赋初值;接口中的方法都是抽象方法,而m1不是抽象方法11、interface Parentint j = 1;void m1(); public class Ex5 extends Parent void m1() System.out.print( “m1 in child” ); 答案:将extends 改为implement

36、s12、interface Parent1void m1(); interface Parent2void m2(); public class Ex6 implements Parent1, Parent2 void m1() System.out.print( “m1 in child” ); 答案:方法m2没有在Ex6中具体定义13、 下面程序有什么错误?请指出并改正。public class Base public static void main(String argv) int t = 1,2,3,4,5,6 ;try System.out.print(t12);catch( IO

37、Exception e )e.printStackTrace();System.out.print( Ends OK ); 答:把IOException改为Exception14、下面程序有什么错误?请指出并改正。public class ExArray public static void main(String argv) int t = 1,2,3,4,5,6 ;try System.out.print(t12);System.out.print( after try block );catch( Exception e )e.printStackTrace();System.out.p

38、rint( after catch block ); 答:在try 和catch之间不能出现任何语句。因此去掉System.out.print( after try block );15、改正下面代码的错误: int a = new int3; a0 = 1; a1 = 2.0; a2 = 3;答:把a1 = 2.0改为a1 = 2三、名词解释1、 重置:在继承类之间,子类和其父类都有一个同名的方法,该方法的方法头完全一致,子类对象调用这个方法时,实际调用的是自己的,而非其父的,这种现象叫2、 异常:程序在运行期间,出现错误而不能正常退出,这种现象叫3、 Java虚拟机:在真实机器中用软件模拟

39、实现的一种想象机器。Java虚拟机代码被存储在 .class文件中;每个文件都包含最多一个public类。Java虚拟机规范为不同的硬件平台提供了一种编译Java技术代码的规范,该规范使Java软件独立于平台,因为编译是针对作为虚拟机的“一般机器”而做,这个“一般机器”可用软件模拟并运行于各种现存的计算机系统,也可用硬件来实现。4、 节点流:直接提供输入输出功能的流5、 处理流:高级流,增强了节点流的功能四、问答题1、答案:choice = 2 choice = 3 choice = default2、解释重载的概念。3、 答案:Ex6 obj = new Ex6( 3 )4、请写出全部的关系

40、运算符以及逻辑运算符。 答案: = 0 )m = n / 2 + 1;elsem = n / 2;for ( k = 0 ; k m ; k+ )System.out.println( a k k );j = k;for ( i = k + 1 ; i n - k ; i+ ) System.out.println( a i j );i-;for ( j = k + 1 ; j = k ; i- ) System.out.println( a i j );i+;for ( j = n - k - 2 ; j k ; j- ) System.out.println( a i j );2、编写程序

41、实现折半查找算法,其查找的序列为一个整数序列2,4,6,9,12,56,89,100,123,567。答案:public class binsrch private int r; private int key;public binsrch( int k )r = new int 5 ;r 0 = 2;r 1 = 5;r 2 = 7;r 3 = 18;r 4 = 21;this.key = k;public boolean Bin_srch( int a , int k )int low , high , mid;boolean found = false;low = 0;high = a.l

42、ength - 1;while( low = high & !found )mid = (low + high)/2;if( a mid k )low = mid + 1;else if( a mid = k )found = true;elsehigh = mid - 1;return found; public boolean Bin_srch1( int a , int k , int low , int high )int mid;boolean found = false;if( low = high & !found )mid = (low + high)/2;if( a mid

43、k )low = mid + 1;found = Bin_srch1( a , k , low , high );else if( a mid = k )found = true;elsehigh = mid - 1;found = Bin_srch1( a , k , low , high );return found;public static void main(String args) boolean result; binsrch bin = new binsrch( Integer.parseInt( args 0 ) );/result = bin.Bin_srch( bin.r , bin.key );result = bin.Bin_srch1( bin.r , bin.key , 0 , bin.r.length - 1 );if( result ) System.out.println(The number you search is found in the array!);elseSystem.out.println(The number you search is existed!);友情提示:部分文档来自网络整理,供您参考!文档可复制、编制,期待您的好评与关注!

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!