JAVA练习题(第3章)_第1页
JAVA练习题(第3章)_第2页
JAVA练习题(第3章)_第3页
JAVA练习题(第3章)_第4页
JAVA练习题(第3章)_第5页
已阅读5页,还剩55页未读 继续免费阅读

下载本文档

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

文档简介

1、JAVA程序设计练习题 第3章一、单选择题1、Java语言中,只限子类或者同一包中的类的方法能访问的访问权限是( ) A.public B.private C.protectedD.2、设有数组定义int x=1,2,3,4,5,6,;,则x.length的值为( ) A.3B.4 C.6D.7【答案答案】:C【答案答案】:B3、以下关于继承的叙述正确的是( )。A、在Java中类只允许单一继承B、在Java中一个类只能实现一个接口C、在Java中一个类不能同时继承一个类和实现一个接口D、在Java中接口只允许单一继承4、下列哪些语句关于Java内存回收的说明是正确的? ( )A、程序员必须创

2、建一个线程来释放内存B、内存回收程序负责释放无用内存C、内存回收程序允许程序员直接释放内存D、内存回收程序可以在指定的时间释放内存对象【答案答案】:A【答案答案】:B5、有以下程序片段,下列哪个选项不能插入到行1。( ) 1. 2 .public class Interesting 3. /do sth 4. A、import java.awt.*;B、package mypackage; C、class OtherClass D、public class MyClass 【答案答案】:D6、定义一个类,必须使用的关键字是( )A、publicB、classC、interface D、stat

3、ic7、应用程序的main方法中有以下语句,则输出的结果是 ( )。 String s1=new String(abc); String s2=new String(abc); boolean b1=s1.equals(s2); boolean b2=(s1=s2); System.out.print(b1+ +b2); A、true false B、 false true C、 true true D、false false 【答案答案】:B【答案答案】:A8、应用程序的main方法中有以下语句,则输出的结果是 ( )。 int b=1, 2,2, 2,2,2; int sum=0; for

4、(int i=0;ib.length;i+) for(int j=0;jbi.length;j+) sum*=bij; System.out.println(sum=+sum); A、32 B、11 C、 2 D、 3 【答案答案】:A9、应用程序的main方法中有以下语句,则执行后输出的结果是 ( )。 int x=125,21,5,168,98; int max=x0; for(int i=1;i max)max =xi; System.out.println(max); A、 125 B、5 C、 98 D、 168 【答案答案】:D10、程序Test.java编译运行后输出的结果是(

5、)。 public class Test String s1=java; public static void main(String args) int z=2; Test t=new Test(); System.out.println(t.s1+z); A、 java2 B、2 C、没有输出结果 D、java 【答案答案】:A11、应用程序的main方法中有以下语句,则输出的结果是 ( )。 int b=1, 1, 1, 2,2, 3; int sum=0; for(int i=0; ib.length; i+) for(int j=0; jbi.length; j+) sum+=bij

6、; System.out.println(sum=+sum); A、10 B、6 C、 9 D、 13 【答案答案】:A12、下列修饰符中与访问控制无关的是()Aprivate BpublicCprotectedDfinal13、void的含义:()A方法没有返回值B 方法体为空C没有意义 D.定义方法时必须使用14、return语句正确的使用:()A只能让方法返回数值B方法都必须含有C方法中可以有多句return D不能用来返回对象【答案答案】:D【答案答案】:A【答案答案】:C15、下列说法哪个正确? ()A不需要定义类,就能创建对象B对象中必须有属性和方法C属性可以是简单变量,也可以是一

7、个对象D、属性必须是简单变量16、构造函数何时被调用?()A、创建对象时 B、类定义时C、使用对象的方法时 D、使用对象的属性时17、关于继承的说法正确的是:()A、子类将继承父类所有的属性和方法。B、子类将继承父类的非私有属性和方法。C、子类只继承父类public方法和属性D、子类只继承父类的方法,而不继承属性【答案答案】:C【答案答案】:A【答案答案】:B18、关于构造函数的说法哪个正确?()A、一个类只能有一个构造函数B、一个类可以有多个不同名的构造函数C、构造函数与类同名D、构造函数必须自己定义,不能使用父类的构造函数19、关于super的说法正确的是( )A、是指当前对象的内存地址B

8、、是指当前对象的父类对象的内存地址C、是指当前对象的父类D、可以用在main()方法中【答案答案】:C【答案答案】:B20、覆盖与重载的关系是()A、覆盖只有发生在父类与子类之间,而重载可以发生在同一个类中B覆盖方法可以不同名,而重载方法必须同名Cfinal修饰的方法可以被覆盖,但不能被重载D覆盖与重载是同一回事21、请说出下列代码的执行结果 : ( )String s = abcd; String s1 = new String(s); if (s = = s1) System.out.println(the same); if (s.equals(s1) System.out.printl

9、n(equals); A. the same equals B. equals C. the same D. 什么结果都不输出【答案答案】:A【答案答案】:B22、编译并运行以下代码将发生什么?( ) class MyClass int x; MyClass(int i) x = i; public static void main(String args)MyClass m1 = new MyClass(100);MyClass m2 = new MyClass(100);if(m1.equals(m2)System.out.println(Both are equal);elseSyste

10、m.out.println(Both are not equal); A、代码编译时报出错误提示信息“equals() 方法未定义” B、编译通过,抛出运行期异常.C、输出Both are equal.D、输出Both are not equal.【答案答案】:D23、以下代码段的输出结果是?( ) 1. int myArray = new int1010; 2. if (myArray00 10) 3. 4. System.out.println(good question); 5. A、第 1行编译错误B、第 2行运行期异常C、输出good questionD、以上都不对【答案答案】:C2

11、4、以下哪个选项的代码编译不能通过?( )A、 int i = 10; int j = 4; System.out.println(i|j);B、 int i = 10; int j = 4; System.out.println(i|j);C、 boolean b1=true; boolean b2=true; System.out.println(b1|b2);D、 boolean b1=true; boolean b2=true; System.out.println(b1|b2);【答案答案】:A25、以下哪个选项的代码可以用来定义该类的构造器?( ) public class Tes

12、t . A、public void Test() . B、public Test() . C、public static Test() . D、public static void Test() .【答案答案】:B26、下列对封装的描述中,错误的( )。A. 封装体包含了属性和行为 B. 封装体中的属性和行为的访问权限是相同的 C. 被封装的某些信息在封装体外是不可见的 D. 封装使得抽象的数据类型提高了可重用性 27、下列关于继承性的描述中,错误的是( )。A. 一个类可以同时生成多个子类 B. 子类继承了父类的所有成员 C. Java语言支持单继承和多重继承 D. Java语言通过接口可使

13、子类使用多个父类的成员【答案答案】:B【答案答案】:C28、下列循环语句的循环次数是( )。 int i=5; do System.out.println(i ); i ; while (i!=0);A. 0 B. 1 C. 5 D. 无限【答案答案】:D29、第十行的声明将调用哪些方法.( ) 1 class Person 2 public void printValue(int i, int j) 3 public void printValue(int i) 4 5 public class Teacher extends Person 6 public void printValue(

14、) 7 public void printValue(int i) 8 public static void main(String args) 9 Person t = new Teacher(); 10 t.printValue(10); 11 12 n A .第2行的方法 B. 第3行的方法1. C. 第6行的方法 D. 第7行的方法【答案答案】:D二、填空题1、创建一个名为 MyPackage 的包的语句是; ,该语句应该放在程序的位置为: 。2、设有数组定义:int MyIntArray = 10 , 20 , 30 , 40 , 50 , 60 , 70; 则执行以下几个语句后的输

15、出结果是 。 int s = 0 ; for (int i = 0 ; i = 0; i-)System.out.print(ai + );6、构造方法是一种特殊的成员方法,构造方法名与( ) 相同。7、Java语言只允许单继承,指每个类只能有一个 ( )。6 5 4 3 2类名类名父类父类三、判断题判断题1Java的源代码中定义几个类,编译结果就生成几个以.class为后缀的字节码文件。( ) 2Java程序里,创建新的类对象用关键字new,回收无用的类对象使用关键字free。 ( )3Java有垃圾回收机制,内存回收程序可在指定的时间释放内存对象。 ( )4构造方法用于创建类的实例对象,构

16、造方法名应与类名相同,返回类型为void。 ( )5拥有abstract方法的类是抽象类,但抽象类中可以没有abstract方法。 ( )6在Java中对象可以赋值,只要使用赋值号(等号)即可,相当于生成了一个各属性与赋值对象相同的新对象。 ( )四、写出下面程序的运行结果1、 import java.io.*; public class abc public static void main(String args ) AB s = new AB(Hello!,I love JAVA.); System.out.println(s.toString( ); class AB String s

17、1; String s2; public AB(String str1, String str2) s1 = str1; s2 = str2; public String toString( ) return s1+s2; Hello! I love JAVA. 2、 import java.io.* ; public class abc public static void main(String args ) int i, s = 0 ; int a = 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 ; for ( i = 0 ; i a.lengt

18、h ; i + ) if ( ai%3 = = 0 ) s += ai ; System.out.println(s=+s); s = 180 3、import java.io.* ; public class abc public static void main(String args System.out.println(a=+a+nb=+b); class SubClass extends SuperClass int c; SubClass(int aa, int bb, int cc) super(aa, bb); c=cc; class SubSubClass extends S

19、ubClass int a; SubSubClass(int aa, int bb, int cc) super(aa, bb, cc); A = aa+bb+cc; void show() System.out.println(a=+a+nb=+b+nc=+c); a=60 b=20 c=30 4、class StringTest1 public static void main(String args) String s1=hello;String s2=new String(hello);if(s1.equals(s2)System.out.println(相等);elseSystem.

20、out.println(不相等); 相等相等 5、public class TestArray public static void main(String args ) int i , j ;int a = 5,9,6,8,7; for ( i = 0 ; i a.length-1; i + ) int k = i; for ( j = i ; j a.length ; j+ ) if ( ajak ) k = j; int temp =ai; ai = ak; ak = temp; for ( i =0 ; ia.length; i+ ) System.out.print(ai+ ); S

21、ystem.out.println( ); 5 6 7 8 9 6、class Animal Animal( ) System.out.print (Animal ); public class Dog extends Animal Dog( ) System.out.print (Dog ); public static void main(String args) Dog snoppy= new Dog(); Animal Dog7、public class Person String name;int age;public Person(String name, int age) thi

22、 = name;this.age = age;public static void main(String args) Person c = new Person(Peter, 17);System.out.println( + is + c.age + years old!); Peter is 17 years old! 8、public class Course private String cNumber;private String cName;private int cUnit;public Course(String number, String name

23、, int unit) cNumber = number;cName = name;cUnit = unit;public void printCourseInfo() System.out.println(课程号: + cNumber + 课程名: + cName + 学分: + cUnit);class CourseTest public static void main(String args) Course c;c = new Course(101, ASP, 3);c.printCourseInfo();课程号课程号:101 课程名课程名:ASP 学分学分:3 9、public cl

24、ass Father String name, address, tel;int age;public Father(String name, int age) = name;this.age = age;void out() System.out.print(姓名: + name);System.out.print( 年龄: + age);void outOther() System.out.print( 家庭住址: + address);System.out.print( 电话: + tel);class Son extends Father String school

25、;public Son(String name, int age) super(name, age);void out() super.out();super.outOther();System.out.println( 学校: + school);public static void main(String args) Son son = new Son(Tom, 15);son.address = 金水区;son.school = 九中;son.tel = 66123456;son.out();姓名姓名:Tom 年龄年龄:15 家庭住址家庭住址:金水区金水区 电话电话:66123456 学

26、校:九中学校:九中 10、public class MyClass int a = 1, 2, 3, 4, 5 ;void out() for (int j = 0; j a.length; j+)System.out.print(aj + );public static void main(String args) MyClass my = new MyClass();my.out();1 2 3 4 5 11、public class Sumpublic static void main(String args)int j=10;System.out.println(j is : +j);

27、calculate(j);System.out.println(At last, j is : +j);static void calculate (int j)for (int i = 0;i10;i+)j+;System.out.println(j in calculate() is: +j);j is : 10 j in calculate() is : 20At last j is : 10 12、 public class Father int a=100; public void miner() a-; public static void main(String args) Fa

28、ther x = new Father(); Son y = new Son(); System.out.println(y.a); System.out.println( y.getA(); y.miner(); System.out.println(y.a); System.out.println(y.getA(); class Son extends Father int a = 0; public void plus() a+; public int getA() return super.a; 010009913、class A int a; A (int i) a=i; class

29、 B extends A int a,b; B (int i , int j) super ( i ); b=j; a=i+j; public class AB public static void main (String args) A a1=new A(9), a2; B b=new B(3,5); System.out.println(b.a); a2=a1; System.out.println(a2.a); a2=b; System. out.println(a2.a); b=(B)a2; System.out.println(b.a);8 9 3 814、public class

30、 Break public static void main (String args ) for(int i=1; i=4; i+ ) b1: b2: b3: System.out.println(i=+i); if(i=1) break b1; else if(i=2) break b2; else if(i=3) break b3; else System.out.println(in block b3); System.out.println(in block b2); System.out.println(in block b1);i=1 i=2in block b1 i=3in b

31、lock b2in block b1 i=4in block b3in block b2in block b1 15、 class C int c; C (int a) c=0; for(int i=0; i=a; i+) c+=i; C (C b) c=b.c; class Example public static void main(String args ) C c1=new C(6); C c2=new C(new C (4); System.out.println(c1: +c1.c); System.out.println(c2: +c2.c); c1:21 c2:1016、pu

32、blic class Example public static void main (String args ) int i; for(i=1; i2) System.out.println(AB); else System.out.println(BA); if (i= =5) break; System.out.println(i=+i);BABA ABABAB i=517、public class Example public static void main (String args ) A: for(int i=0; i5; i+) for(int j=0; j 10 ); pub

33、lic static void main(String arg) int i=10; Test t= new Test(); t.printValue(i); 1021、class Parent String one, two; public Parent(String a, String b) one = a; two = b; public void print() System.out.println(one); public class Child extends Parent public Child(String a, String b) super(a,b); public vo

34、id print() System.out.println(one + to + two); public static void main(String args) Parent p = new Parent(south, north); Parent t = new Child(east, west); p.print(); t.print(); southeast to west22、public class A public static void main (String args) A a=new B( ); a.test(); void test() System.out.pri

35、nt (A); class B extends A void test() super.test(); System.out.println(B); AB23、public class T1 public static void main (String args) T1 a=new T1(); a.method(8); a.method(1.2f); void method(float i) System.out.println(float: +i); void method(long i) System.out.println(long: +i); long: 8float: 1.224、

36、public class Testpublic static void main(String args ) String s= hello; String t = hello; char c = h,e,l,l,o; if(s.equals(t) System.out.println(A); if(t.equals(c) System.out.println(B); if(s=t)System.out.println(C); if(t.equals(new String(hello)System.out.println(D); ACD25、class A int a=1; double d=

37、2.0; void show( ) System.out.println(Class A: a=+a +td=+d); class B extends A float a=3.0f; String d=Java program.; void show( ) super.show( ); System.out.println(Class B: a=+a +td=+d); (1) 若在应用程序的main方法中有以下语句:A a=new A();a.show();则输出的结果如何?(2) 若在应用程序的main方法中定义类B的对象b:A b=new B();b.show();则输出的结果如何? Cl

38、ass A: a=1 d=2.0 Class A: a=1 d=2.0Class B: a=3.0 d=Java program.26、class V1 public int x=2; V1() System.out.println(V1); class V2 extends V1 public int x=3; V2() System.out.println(V2); public class H public static void main(String s) V1 e1=new V2(); System.out.println(e1.x); V1 V2 227、class SuperC

39、lass int a,b; SuperClass(int x,int y) a=x; b=y; voidshow() System.out.println(a=+ a + nb=+ b); class SubClass extends SuperClass int c; SubClass(int aa,int bb,int cc) super(aa,bb);c=cc; voidshow() System.out.println(c=+ c +na=+ a +nb=+ b);class SubSubClass extends SubClass int a; SubSubClass(int aa,int bb,int cc) super(aa,bb,cc);a=aa+bb+cc;void show()System.out.println(a=+ a +nb=+ b +nc=+ c);public class test public static void main(String args) SuperClass p=new SubSubClass(10,20,30);p.show();a=60b=20c

温馨提示

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

评论

0/150

提交评论