JAVA复习题Word格式.docx
《JAVA复习题Word格式.docx》由会员分享,可在线阅读,更多相关《JAVA复习题Word格式.docx(42页珍藏版)》请在冰豆网上搜索。
18.下列选项中,定义接口MyInterface的语句正确的是:
(A)
A)interfaceMyInterface{}B)implementsMyInterface{}
C)classMyInterface{}D)implementsinterfaceMy{}
19.如果子类中的方法mymethod()覆盖了父类中的方法mymethod(),假设父类方法头部定义如下:
voidmymethod(inta),则子类方法的定义不合法的是:
(C)
A)publicvoidmymethod(inta)B)protectedvoidmymethod(inta)
C)privatevoidmymethod(inta)D)voidmymethod(inta)
二、填空题
1.如果子类中的某个变量的变量名与它的父类中的某个变量完全一样,则称子类中的这个变量________了父类的同名变量。
(隐藏)
2.属性的隐藏是指子类重新定义从父类继承来的__________。
(同名变量或属性)
3.如果子类中的某个方法的名字、返回值类型和________与它的父类中的某个方法完全一样,则称子类中的这个方法覆盖了父类的同名方法。
(参数列表)
4.Java仅支持类间的____重继承。
(单重)
5.抽象方法只有方法头,没有________。
(方法体)
6.Java语言的接口是特殊的类,其中包含______常量和_______方法。
(static(静态);
abstract(抽象))
7.接口中所有属性均为_________、_________和________的。
(public、static、final)
8.如果接口中定义了一个方法methodA(),一个属性attA,那么一个类ClassA要实现这个接口的话,就必须实现其中的_______方法。
(methodA())
9.一个类如果实现一个接口,那么它就必须实现接口中定义的所有方法,否则该类就必须定义成________的。
(抽象的)
10.如果子类中的某个方法的名字、__________和参数列表与它的父类中的某个方法完全一样,则称子类中的这个方法覆盖了父类的同名方法。
(返回值类型)
11.Java仅支持类间的单重继承,接口可以弥补这个缺陷,支持_____重继承(多重)
12.在方法头用abstract修饰符进行修饰的方法叫做________方法。
(抽象)
13.Java语言中用于表示类间继承的关键字是___________。
(extends)
14.接口中所有方法均为________和________的。
(public、abstract)
15.Java语言中,表示一个类不能再被继承的关键字是________。
(final)
16.Java语言中,表示一个类A继承自父类B,并实现接口C的语句是_________________。
(classAextendsBimplementsC)。
17.如果子类中的方法compute()覆盖了父类中的方法compute(),假设父类的compute()方法头部有可见性修饰符public,则methodS()的可见性修饰符必须是__________。
(public)
三、程序阅读题
1.现有类说明如下,请回答问题:
publicclassA
{
Stringstr1="
Hello!
\t"
;
Stringstr2="
Howareyou?
"
publicStringtoString()
{returnstr1+str2;
}
}
publicclassBextendsA
\b\b,Bill."
{returnsuper.str1+str1;
问题:
1)类A和类B是什么关系?
(继承关系)
2)类A和类B都定义了str1属性和方法toString(),这种现象分别称为什么?
(属性的隐藏;
方法的覆盖)
3)若a是类A的对象,则a.toString()的返回值是什么?
(Hello!
Howareyou?
)
4)若b是类B的对象,则b.toString()的返回值是什么?
(Hello,Bill.)
2.现有一个类定义如下,请回答问题:
classEmployee
{
Stringname;
intage;
doublewage;
staticintNo=0;
Employee(Stringa1,inta2,doublea3)
{
name=a1;
age=a2;
wage=a3;
No++;
}
在使用该类时,已使用下面语句生成了该类的对象:
Employeee1,e2;
e1=newEmployee("
王劲"
26,6300);
e2=newEmployee("
张山"
30,3800);
1)e2.name,e2.age,e2.wage的值各是什么?
(张山;
30;
3800.0)
2)生成对象e1、e2后,e1.No值为多少?
能否通过类名做前缀引用属性No?
(2;
能)
3.阅读程序,回答问题。
publicclassInheritTest1
publicstaticvoidmain(String[]args)
Aaa;
Bbb;
aa=newA();
bb=newB();
aa.show();
bb.show();
classA
inta=1;
doubled=2.0;
voidshow()
{System.out.println("
ClassA:
+"
\ta="
+a+"
\td="
+d);
classBextendsA
floata=3.0f;
Stringd="
Javaprogram."
intb=4;
voidshow()
System.out.println("
+super.a+"
+super.d);
super.show();
ClassB:
+d+"
\tb="
+b);
1)这是哪一类java程序?
(java应用程序)
2)类A和类B是什么关系?
(类B是类A的子类)
3)按程序输出的格式写出程序运行后的结果.
(程序运行结果如下:
a=1d=2.0
a=3.0d=Javaprogram.b=4)
4.现有类说明如下,请回答问题:
intx=10;
intgetA(){returnx;
intx=100;
intgetB(){returnx;
1)类B是否能继承类A的属性x?
(能)
2)若b是类B的对象,则b.getB()的返回值是什么?
(100)
3)若b是类B的对象,则b.getA()的返回值是什么?
(10)
4)类A和类B都定义了x属性,这种现象称为什么?
(属性的隐藏)
5.有如下源程序,请回答问题:
classA
{Strings="
classA"
classBextendsA
classB"
publicclassTypeConvert
publicstaticvoidmain(Stringargs[])
{
Bb1,b2=newB();
Aa1,a2;
a1=(A)b2;
a2=b2;
System.out.println(a1.s);
System.out.println(a2.s);
b1=(B)a1;
System.out.println(b1.s);
System.out.println(b2.s);
}
问题:
该程序的四行输出各是什么?
(classA
classB
classB)
6.现有类说明如下,请回答问题:
intx=888;
Stringstr="
Ilike:
publicStringtoString()
{returnstr+x;
Stringx="
java"
{returnstr+x+"
and"
+super.x;
2)类A和类B都定义了x属性和方法toString(),这种现象分别称为什么?
(属性的隐藏和方法的覆盖)
3)若a是类A的对象,则a.toString()的返回值是什么?
(Ilike:
888)
(Ilike:
javaand888)
7.运行类C的输出结果是什么?
publicA()
System.out.println(“ThedefaultconstructorofAisinvoked”);
publicB()
publicclassC
publicstaticvoidmain(String[]args)
Bb=newB();
8.阅读下列程序写出输出结果:
voidshow()
System.out.println(s);
Bb1;
Bb2=newB();
a1.show();
a2.show();
b1.show();
b2.show();
答案:
四、程序填空题
1.下面是一个类的定义,完成程序填空。
(Youwritethis.x=x;
publicclassYouwrite
{
intx;
_______(){x=0;
Youwrite(intx)
{_____;
2.下面是定义一个接口ITF的程序,完成程序填空。
(interfaceabstract)
public_______ITF
publicstaticfinaldoublePI=Math.PI;
public______doublearea(doublea,doubleb);
3.下面是定义一个接口A的程序,完成程序填空。
(final”;
”)
publicinterfaceA
publicstatic_______doublePI=3.14159;
publicabstractdoublearea(doublea,doubleb)_____
五、编程题
简单类的继承:
1.定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。
编写一个类,继承自矩形类,同时该类描述长方体,具有长、宽、高属性,和计算体积的方法。
编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、宽、高,输出其底面积和体积。
publicclassTestExtends{
publicstaticvoidmain(String[]args){
LiFangTif=newLiFangTi();
f.length=2;
f.width=3;
f.height=7;
System.out.println("
立方体的底面积为:
"
+f.findArea()+"
立方体的体积为:
+f.findVolume());
classJuXing{
intlength;
intwidth;
intfindArea(){
returnlength*width;
classLiFangTiextendsJuXing{
intheight;
intfindVolume(){
returnfindArea()*height;
3.定义一个Person类,它包括的属性有“姓名”和“性别”,为Person类派生出一个子类Student类,为Student子类添加两个属性年龄和成绩等级(用A,B,C,D,E表示),在子类中打印出学生的姓名、性别、年龄及成绩等级。
classPerson{
protectedStringname;
protectedStringsex;
Person(Stringname,Stringsex){
this.name=name;
this.sex=sex;
publicStringtoString(){
Strings=newString();
s=name+"
\t"
+sex;
returns;
classStudentextendsPerson{
protectedintage;
protectedchargrade;
Student(Stringname,Stringsex,intage,chargrade){
super(name,sex);
this.age=age;
this.grade=grade;
voidprint(){
s=super.toString();
s=s+"
+age+"
+grade;
System.out.println(s);
publicclassTestP{
Studentst1=newStudent("
zhangfei"
"
male"
23,'
E'
);
Studentst2=newStudent("
liubei"
25,'
A'
name"
\tset"
\tage"
\tgrade"
st1.print();
st2.print();
4.定义一个类Rectangle代表矩形,其中包括计算面积的方法。
再定义一个它的子类Square代表正方形],其中也包含计算面积的方法。
编写一程序,测试新建子类对象的属性和方法。
classRectangle{
floatlength;
floatwidth;
Rectangle(floatlen,floatwh){
length=len;
width=wh;
floatgetArea(){
classSquareextendsRectangle{
Square(floatlen){
super(len,len);
returnsuper.getArea();
publicclassTestRectangle{
Squaresq=newSquare(5.2f);
sideis"
+sq.length+"
areais"
+sq.getArea());
类与对象习题
一、选择题
1定义类头时,不可能用到的关键字是(B)。
A)classB)privateC)extendsD)public
2.下列类定义中,不正确的是(C)。
A)classx{....}
B)classxextendsy{....}
C)staticclassximplementsy1,y2{....}
D)publicclassxextendsApplet{....}
3.下列类头定义中,错误的是(A)。
A)publicxextendsy{...}
B)publicclassxextendsy{...}
C)classxextendsyimplementsy1{...}
D)classx{...}
4.设A为已定义的类名,下列声明A类的对象a的语句中正确的是(D)。
A)floatAa;
B)publicAa=A();
C)Aa=newint();
D)staticAa=newA();
5.设A为已定义的类名,下列声明A类的对象a的语句中正确的是(A)。
A)publicAa=newA();
C)Aa=newclass();
D)aA;
6.设X、Y均为已定义的类名,下列声明类X的对象x1的语句中正确的是(C)。
A)publicXx1=newY();
B)Xx1=X();
C)Xx1=newX();
D)intXx1;
7.设X、Y为已定义的类名,下列声明X类的对象x1的语句中正确的是(A)。
A)staticXx1;
B)publicXx1=newX(int123);
C)Yx1;
D)Xx1=X();
8.通过类MyCla