java期中(讲完第9章之...doc_第1页
java期中(讲完第9章之...doc_第2页
java期中(讲完第9章之...doc_第3页
java期中(讲完第9章之...doc_第4页
java期中(讲完第9章之...doc_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

备注:本文档凡注明 “X”标记的都是上次习题课未讲解的,但可以在此讲解。 其他的已经在前面的习题课中讲过。 习题001、选择填空(可多选)(1)-(E) 代码如下: public class WhileExm public static void main (String args) int x= 1, y = 6; while (y-) x-; system.out.printIn(“x=” + x “y =” + y); 结果是什么? A. The output is x = 6 y = 0 B. The output is x = 7 y = 0 C. The output is x = 6 y = -1 D. The output is x = 7 y = -1 E. 编译失败(2)-(ACD)下面关于变量及其范围的陈述哪些是对的?A. 实例变量是类的成员变量。B. 实例变量用关键字static声明。C. 在方法中定义的局部变量在该方法被执行时创建D. 局部变量在使用前必须被初始化。(3)-(AC) X哪些声明阻止了方法的overriding? A. final void methoda() B. static void methoda() C. static final void methoda() D. final abstract void methoda() (4)-(A) X哪个语句创建了一个数组实例? A. int ia = new int 15; B. float fa = new float 20; C. char ca = “Some String”; D. int ia = 4, 5, 6 1, 2, 3; 答案D的格式错误: 应该为int ia = 4, 5, 6, 1, 2, 3;(5)-(BC)哪些代码行不会产生编译错误? A. int i=0; if (i) System.out.println(“Hi”); 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”); (6)-(DE) X哪些语句可以合法的插入标明*的位置? class Person private int a;public int change(int m) return m; public class Teacher extends Person public int b;public static void main(String arg)Person p = new Person();Teacher t = new Teacher();int i;*A. i = m;B. i = b;C. i = p.a; D. i = p.change(30);E. i = t.b. (7)(源码在workspace中工程文件夹名字是testchapter07) X阅读下列程序,并给出下列程序运行的输出结果:class Person public int a=1;public int change(int m) return m; public class Teacher extends Person public int a=2;public static void main(String arg)Person p = new Person();Teacher t = new Teacher();p=t;System.out.println(p.a= +p.a);System.out.println(t.a= +t.a);(答案: p.a= 1t.a= 2)(解释: 在java中, 除了非静态方法有动态绑定之外, 实例变量和静态方法都不存在动态绑定的问题, 也就是说, 实例变量和静态方法是在编译时确定其执行内容的, 运行时不再改变 )(8)-(B)编译Java源程序文件产生的字节码文件的扩展名为 A java B class C html D exe(9)-(BC)下面哪些不是java的简单数据类型? A. shortB. BooleanC. DoubleD. float (10)-(BD) 有关构造方法,下列叙述正确的是? A. 默认的构造方法初始化方法变量。B. 默认的构造方法初始化了在类中声明的实例变量。C. 假如一个类缺少没有参数的构造方法,但是有其它的构造方法,则编译器生成一个缺省的构造方法。D. 编译器只有当一个类没有其它的构造方法时才生成缺省的构造方法。 (11)what is the result when you compile and run the following code? (D)(p96)Class ExamplePublic static void main(String args) If(new Boolean(“true”)=new Boolean(“true”) System.out.println(“True”);else System.out.println(“False”);Select all right answers: A. Compilation error;B. No compilation error, but runtime exception;C: Prints “True”;D: Prints “False”.考察两个对象的比较:两个属于包装类的对象的比较,就是两个对象的比较(12)-(C) X以下能正确定义数组并正确赋初值的语句是( )A. int n=5, ann; B. int b=2,3;C. int c=1,2,3,4,5; D. int d=1,2,3,4; 习题00: 在空白处填上合适的语句,使程序能正确运行.(System.out.println(obj.getX();) public class ClassName private int x;Classname() X=1; int getX() return x; public class Testpublic static void main(String args) ClassName obj=new ClassName(); /生成一个对象 _ /在此空白处填上输出x的语句 习题01: 分析下面的类的输出: Xpublic class TStatictester public int x=1;public static int y=2;public static void main(String args) TStatictester t1=new TStatictester(); TStatictester t2=new TStatictester(); t1.x +=1; t1.y +=1; t2.x +=2; t2.y +=2; System.out.println(T1: x=+t1.x+, y=+t1.y); System.out.println(T2: x=+t2.x+, y=+t2.y);习题02:请指出下面程序有何错误: Xpublic class pet public String name;public int age;public void readinput ()System.out.println(please enter the name .);name=SavitchIn.readLine();System.out.println(please enter the age.);age=SavitchIn.readLineInt(); public void update(String name ,int age )name=name+ty;age=age+7;public static void main (String args)pet a=new pet();a.readinput();a.update(name,age); 习题03:请指出下面程序的运行结果(因有一定篇幅,不易在黑板讲解,留做自学) Xpublic class Flower int petalCount = 0; String s = new String(null); Flower(int petals) petalCount = petals; System.out.println( Constructor w/ int arg only, petalCount= + petalCount); Flower(String ss) System.out.println( Constructor w/ String arg only, s=+ ss); s = ss; Flower(String s, int petals) this(petals);/! this(s); / Cant call two! this.s = s; / Another use of this System.out.println(String & int args); Flower() this(hi, 47); System.out.println( default constructor (no args); void print() /! this(11); / Not inside non-constructor! System.out.println( petalCount = + petalCount + s =+ s); public static void main(String args) Flower x = new Flower(); x.print(); 习题04:分析下列程序的执行结果: Xabstract class AbstractIt abstract float getfloat(); public class AbstractTest extends AbstractIt private float f1=1.0f; private float getFloat() return f1; 。 编译和执行该程序的结果是什么? 习题05:阅读下列程序: Xclass A public String tostring() return “4”; class B extends A public String tostring()return super.tostring()+”3”;public class Test public static void main(String args)B b=new B();System.out.println(b.tostring();请分析该程序的执行结果.习题06:分析下列程序的执行结果: Xclass Superclass String a=”hello”;void test() System.out.print(a);public class Subclass extends Superclass String a=”aaa”;Public static void main(String args)Subclass bar=new Subclass();bar.test();System.out.println(bar.a);本解释有点类似上述选择填空题(7)的解释(解释: 在java中, 除了非静态方法有动态绑定之外, 实例变量和静态方法都不存在动态绑定的问题, 也就是说, 实例变量和静态方法是在编译时确定其执行内容的, 运行时不再改变)习题07:编写程序打印如下图形的程序如下,请在下划线处填入适当的内容:* * * *public class ffff1public static void main(String args) int i,j; for(i=0;i4;i+) for(j=0;ji;j+) System.out.print( ); for(j=0;j_;j+) System.out.print(*); System.out.println(); 习题08:写出下列程序的运行结果: XClass Test extends TTPublic static void main(string args) Test t=new Test(“Tom”); public Test(String s)super(s); System.out.println(“how do you do?”); public Test() this(“I am Tom”);Class TTpublic TT() System.out.println(“What a pleasure!”); public TT(String s) this(); System.out.println(“I am” +s); 习题09:写出下列程序的运行结果: X import java.io.*; public class TestStatic static int a=30,b=14,c=21; int x=19,y=32,z=61; public static void main(String args) TestStatic A=new TestStatic(); TestStatic B=new TestStatic(); a=a%b; B.b=A.a%3+c; A.x=A.x+B.y; B.y=A.x+B.z; System.out.println(a=+ a + b=+ b + c=+c); System.out.println(a=+A.a+ b=+B.b+ c=+B.c); System.out.println(x=+A.x+ y=+A.y+ z=+A.z); System.out.println(x=+B.x+ y=+B.y+ z=+B.z); 习题10:写出下列程序的运行结果(第三章习题8答案)public class chapter0308test public static void main(String args) int n;System.out.println(请输入您一行要打印的最多的星号个数:(1 到 50 之间的数); n = SavitchIn.readInt(); for (int i = 1; i n; i+) for (int j = 1; j 0; i-) for (int j = 1; j = i; j+) System.out.print(* ); System.out.println(); 习题11:写出下列程序的运行结果public class ffff public static void main(String args) int i,j; for(i=0;i4;i+) for(j=0;j3-i;j+) System.out.print( ); for(j=0;ji+1;j+) System.out.print(*); System.out.println(); 习题12:编写程序打印如下图形的程序如下: * * * * * *习题00答案:System.out.println(obj.getX();习题01答案:T1: x=2, y=5T2: x=3, y=5 习题02答案:第21行错 原因:未给出正确的实参 改正:a.update(, a.age) (java运行出错:Cannot make a static reference to the non-static field nameCannot

温馨提示

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

评论

0/150

提交评论