收藏 分销(赏)

2024年java程序设计作业答案.doc

上传人:精**** 文档编号:8193583 上传时间:2025-02-07 格式:DOC 页数:36 大小:71.54KB 下载积分:12 金币
下载 相关 举报
2024年java程序设计作业答案.doc_第1页
第1页 / 共36页
2024年java程序设计作业答案.doc_第2页
第2页 / 共36页


点击查看更多>>
资源描述
《JAVA程序设计》作业答案 一、选择题 1、 编译HelloWorld.java的正确命令是: C) javac HelloWorld.java 2、 正确运行HelloWorld.java的正确命令是: A)java HelloWorld 3、 下面程序代码,使用多行注释正确的是: C) /* int k=9; int j=8; k = k + j; */ 4、 long型的取值范围是: D)-263~263-1 5、 下面不属于Java保存字的是: C)malloc 6、 下面属于非法的Java标识符的是: D) abc-d 7、 对与System.out.println()语句解释合理的是: C)执行后输出一个空行 8、 阅读下面的代码,回答下列问题, for( m = 0 ; m > -2 ; m -- ){….} For循环执行多少次: C)2 9、 阅读下面的代码,回答下列问题, 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=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); 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 8 c) -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) morning d) Exception raised: "java.lang.ArrayIndexOutOfBoundsException: 2" 16、下面不是Java保存字的是: a) if b) then c) goto d) while 17、下面属于非法的标识符的是: a) 2variable b) variable2 c) _whatavariable d) _3_ e) $anothervar 18、编译下面的代码,会有什么成果产生: public class MyClass{ static int i; public static void main(String argv[]){ System.out.println(i); } } a) 错误,变量i 没有初始化 b) null c) 1 d) 0 19、编译运行下面的代码,会有什么成果产生: 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) 5 Arrays 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 i 22、编译运行下面的代码,会有什么成果产生: 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) default 23、编译运行下面的代码,会有什么成果产生: 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 displayed 24、下面不会在编译时犯错的是: 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 ,-1 c) No such file found, doing finally, -1 d) 0 26、建立一个HTML去显示一个applet时,必须要定义的tags是: a) name, height, width b) code, name c) codebase, height, width d) code, height, width 27、编译运行下面的代码,会有什么成果产生: 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 Exception 28、用下面的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{ public 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.applet 30、编译运行下面的代码,会有什么成果产生://Code start import 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 end a) 两个按钮并列占据整个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 in setLayout(new FlowLayout()); 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(String) B)toString() B) trim() D)round() 34、main措施中的参数正确的定义是: A) String[] args [] B)String [] args B) float args [] D)String args 35、在命令行执行:java Example 12 3e you 45.6 那么main措施的参数args数组的第一个元素args[0]的内容是: Java B)Example C)12 D)3e 36、下面那个不是Java的核心字: A) goto B)malloc B) extends D)while 37、编译下面的代码,成果是: public class Test { public 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) – 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 1 C)编译通过,但运行时会犯错 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" + 3 C)3 + 5 D)5 + 5.5 E)以上四个都不会导致运行时错误 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)编译通过,运行后输出:10 48、下面正确的是: 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 Exception C)new Exception D)Don't 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 "); } finally { 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)String  56、合法的Java标示符有: A. IdoLikeTheLongNameClass B. $byte C. 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: drive Car: drive D)输出Prints out: · Vehicle: drive Car: drive Vehicle: drive 58、考虑下面的这个类: 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)措施。 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: case 3: System.out.println(3); case 4: System.out.println(4); } A)什么都不会输出 B)3 C)34 D)134 1、4) double d=999d; 2、2) new 3、1) System.out.println(1+1); 4、2) Math.max(7,9); 5、1) byte 的表示范围为 -128 to 127 6、2) 编译运行通过,输出 Base 7、2) public static void amethod(){} 8、1) char c=’1’; System.out.println(c>>1); 9、3) transient 10、2) 输出 “Hello 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.9f 3) char c = ‘h’ 4) boolean b = true 5、答案: public class Ex5 { int x = 1; int y = 1; x = 2; } 6、答案: public class Ex6 { int 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 { …. } 答案:不能有两个package 8、import mycode.ide1.*; package mycode.ide2; public class Ex2 { …. } 答案:imports和package 次序颠倒 9、 public abstract class Ex3 { void m1() { System.out.println( “m1” ); } void m2(); } 答案:措施m2应当被申明为abstract,或者给出m2的措施体 10、public interface Ex4 { int j; void m1(){}; void m2(); } 答案:接口中的变量都是常量,应当给他赋初值;接口中的措施都是抽象措施,而m1不是抽象措施 11、interface Parent { int j = 1; void m1(); } public class Ex5 extends Parent { void m1() { System.out.print( “m1 in child” ); } } 答案:将extends 改为implements 12、interface Parent1 { void m1(); } interface Parent2 { void 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(t[1][2]); } catch( IOException e ) { e.printStackTrace(); } System.out.print( "Ends OK" ); } } 答:把IOException改为Exception 14、下面程序有什么错误?请指出并更正。 public class ExArray { public static void main(String argv[]) { int[][] t = { {1,2,3},{4,5},{6} }; try { System.out.print(t[1][2]); } System.out.print( "after try block" ); catch( Exception e ) { e.printStackTrace(); } System.out.print( "after catch block" ); } } 答:在try 和catch之间不能出现任何语句。因此去掉System.out.print( "after try block" ); 15、更正下面代码的错误: int[] a = new int[3]; a[0] = 1; a[1] = 2.0; a[2] = 3; 答:把a[1] = 2.0改为a[1] = 2 三、名词解释 1、 重置:在继承类之间,子类和其父类都有一个同名的措施,该措施的措施头完全一致,子类对象调用这个措施时,实际调用的是自己的,而非其父的,这种现象叫~ 2、 异常:程序在运行期间,出现错误而不能正常退出,这种现象叫~ 3、 Java虚拟机: 在真实机器中用软件模拟实现的一个想象机器。Java虚拟机代码被存储在 .class文献中;每个文献都包括最多一个public类。Java虚拟机规范为不一样的硬件平台提供了一个编译Java技术代码的规范,该规范使Java软件独立于平台,因为编译是针对作为虚拟机的“一般机器”而做,这个“一般机器”可用软件模拟并运行于各种现存的计算机系统,也可用硬件来实现。 4、 节点流:直接提供输入输出功效的流 5、 处理流:高级流,增强了节点流的功效 四、问答题 1、答案:choice = 2 choice = 3 choice = default 2、解释重载的概念。 3、 答案:Ex6 obj = new Ex6( 3 ) 4、请写出所有的关系运算符以及逻辑运算符。 答案:> < >= <= != == ! && || ^ 简明解释下面存取控制符的作用。 public protected privat 答案:1)任何类都可访问 2)继承类和同一软件包的类可访问 3)只有在其修饰的数据和措施所在类可访问 5、下面的体现式会产生100以内的随机整数吗? 100*Math.r
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服