java练习题.docx

上传人:b****8 文档编号:27592458 上传时间:2023-07-03 格式:DOCX 页数:16 大小:19.05KB
下载 相关 举报
java练习题.docx_第1页
第1页 / 共16页
java练习题.docx_第2页
第2页 / 共16页
java练习题.docx_第3页
第3页 / 共16页
java练习题.docx_第4页
第4页 / 共16页
java练习题.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

java练习题.docx

《java练习题.docx》由会员分享,可在线阅读,更多相关《java练习题.docx(16页珍藏版)》请在冰豆网上搜索。

java练习题.docx

java练习题

一、程序改错题(每小题5分,共10分)

请说明错误的原因,并标明如何改.

1.publicclassErrorTest{

intmaxElements;

ErrorTest(){}

ErrorTest(inti)

{

maxElements=i;

System.out.println(maxElements);

}

publicstaticvoidmain(String[]args)

{

ErrorTesta=newErrorTest();

ErrorTestb=newErrorTest(999);

}

}

原因:

构造函数ErrorTest()未定义。

2.publicclassDogextendsAnimal{

classAnimal{

publicAnimal(){}

publicAnimal(Stringname){

System.out.println("Animal'sconstructorisinvoked");

}

}

原因:

没有为缺省构造函数定义隐式构造函数Animal(),必须定义显示构造函数

二、程序阅读题(每小题5分,共35分)

1.写出程序运行的结果

publicclassTest{

publicstaticvoidmain(String[]args){

int[]a={1,2};

swap(a[0],a[1]);

System.out.println("a[0]="+a[0]+"a[1]="+a[1]);

}

publicstaticvoidswap(intn1,intn2){

inttemp=n1;

n1=n2;

n2=temp;

}

}

结果:

a[0]=1a[1]=2

2.写出程序运行的结果

 classFatherClass{

publicFatherClass(){

System.out.println("FatherClassCreate");

}

}

publicclassChildClassextendsFatherClass{

publicChildClass(){

System.out.println("ChildClassCreate");

}

publicstaticvoidmain(String[]args){

FatherClassfc=newFatherClass();

ChildClasscc=newChildClass();

}

}

结果:

FatherClassCreate

FatherClassCreate

ChildClassCreate

3.

publicclassChangeStrDemo{

publicstaticvoidchangestr(Stringstr){

str="welcome";

}

publicstaticvoidchangearray(inta[]){

a[0]=10;

}

publicstaticvoidmain(String[]args){

Stringstr="Java";

intb[]=newint[10];

changestr(str);

changearray(b);

System.out.println(str);

System.out.println(b[0]);

}

}

结果:

Java

10

4、写出下列程序运行的结果 ()

publicclassExample{

Stringstr=newString("good");

char[]ch={'a','b','c'};

publicstaticvoidmain(Stringargs[]){

Exampleex=newExample();

ex.change(ex.str,ex.ch);

System.out.print(ex.str+"and");

System.out.print(ex.ch);

}

publicvoidchange(Stringstr,charch[]){

str="testok";

ch[0]='g';

}

}

结果:

goodandgbc

5、

classPeople{

Stringname;

publicPeople(){

System.out.print

(1);

}

publicPeople(Stringname){

System.out.print

(2);

this.name=name;

}

publicstaticvoidmain(String[]args){

newChild();

newChild("chen");

}

}

classChildextendsPeople{

Peoplefather;

publicChild(Stringname){

System.out.print(3);

this.name=name;

father=newPeople(name+":

F");

}

publicChild(){

System.out.print(4);

}

}

结果:

14132

6、

publicclassDerviedextendsBase{

privateStringname="dervied";

publicDervied(){

tellName();

printName();

}

publicvoidtellName(){

System.out.println("Derviedtellname:

"+name);

}

publicvoidprintName(){

System.out.println("Derviedprintname:

"+name);

}

publicstaticvoidmain(String[]args){

newDervied();

}

}

classBase{

privateStringname="base";

publicBase(){

tellName();

printName();

}

publicvoidtellName(){

System.out.println("Basetellname:

"+name);

}

publicvoidprintName(){

System.out.println("Baseprintname:

"+name);

}

}

结果:

Derviedtellname:

null

Derviedprintname:

null

Derviedtellname:

dervied

Derviedprintname:

dervied

7、如果i值为2,函数返回值是()

publicstaticintgetValue(inti){

intresult=0;

switch(i){

case1:

result=result+i;

case2:

result=result+i*2;

case3:

result=result+i*3;

}

returnresult;

}

结果:

10

三、选择题(每小题1分,共25分)

1.以下属于面向对象的特征的是(CD)。

A)重载

B)重写

C)封装

D)继承

a)面向对象的特征有:

多态、封装、继承(extends);

b)多态包括:

重载和重写两种;

2.以下代码运行输出是(C)

publicclassPerson{

privateStringname=”Person”;

intage=0;

}

publicclassChildextendsPerson{

publicStringgrade;

publicstaticvoidmain(String[]args){

Personp=newChild();

System.out.println(p.name);

}

}

A)输出:

Person

B)没有输出

C)编译出错

D)运行出错

3.在使用super和this关键字时,以下描述正确的是(A)

A)在子类构造方法中使用super()显示调用父类的构造方法,super()必须写在子类构造方法的第一行,否则编译不通过

B)super()和this()不一定要放在构造方法内第一行

C)this()和super()可以同时出现在一个构造函数中

D)this()和super()可以在static环境中使用,包括static方法和static语句块

4.以下对封装的描述正确的是(D)

A)只能对一个类中的方法进行封装,不能对属性进行封装

B)如果子类继承了父类,对于父类中进行封装的方法,子类仍然可以直接调用

C)封装的意义不大,因此在编码时尽量不要使用

D)封装的主要作用在于对外隐藏内部实现细节,增强程序的安全性

5.以下对继承的描述错误的是(A)

A) Java中的继承允许一个子类继承多个父类

B)父类更具有通用性,子类更具体

C) Java中的继承存在着传递性

D)当实例化子类时会递归调用父类中的构造方法

6.以下程序的运行结果是(D)

classPerson{

publicPerson(){

System.out.println(“thisisaPerson”);

}

}

publicclassTeacherextendsPerson{

privateStringname=”tom”;

publicTeacher(){

System.out.println(“thisisateacher”);

super();

}

publicstaticvoidmain(String[]args){

Teacherteacher=newTeacher();

System.out.println(this.name);

}

}

A)thisisaPerson

thisisateacher

tom

B)thisisateacher

thisisaPerson

tom

C)运行出错

D)编译有两处错误

7.以下说法错误的是(ABD)

A)super.方法()可以调用父类的所有非私有方法

B)super()可以调用父类的所有非私有构造函数

C)super.属性可以调用父类的所有非私有属性

D)this和super关键字可以出现在同一个构造函数中

8.以下关于final关键字说法错误的是(AC)

A)final是java中的修饰符,可以修饰类、接口、抽象类、方法和属性

B)final修饰的类肯定不能被继承

C)final修饰的方法不能被重载

D)final修饰的变量不允许被再次赋值

final关键字可以用来修饰类、方法、变量、参数,不能修饰抽象类和接口

9.访问修饰符作用范围由大到小是(D)

A)private-default-protected-public

B)public-default-protected-private

C)private-protected-default-public

D)public-protected-default-private

10.以下(D)不是Object类的方法

A)clone()复制对象

B)finalize()

C)toString()

D)hasNext()[Iterator类,判断当前元素是否存在,并指向下一个元素]

11.多态的表现形式有(A)

A)重写

B)抽象

C)继承

D)封装

12.以下对重载描述错误的是(B)

A)方法重载只能发生在一个类的内部

B)构造方法不能重载

C)重载要求方法名相同,参数列表不同

D)方法的返回值类型不是区分方法重载的条件

13.以下(D)添加到ComputerBook中不会出错

classBook{

protectedintgetPrice(){

return30;

}

}

publicclassComputerBookextendsBook{

}

A)protectedfloatgetPrice(){}

B)protectedintgetPrice(intpage){}

C)intgetPrice(){}

D)publicintgetPrice(){return10;}

14.以下对抽象类的描述正确的是(C)

A)抽象类没有构造方法

B)抽象类必须提供抽象方法

C)有抽象方法的类一定是抽象类

D)抽象类可以通过new关键字直接实例化

15.以下对接口描述错误的有(D)

A)接口没有提供构造方法

B)接口中的方法默认使用public、abstract修饰

C)接口中的属性默认使用public、static、final修饰

D)接口不允许多继承

16.以下代码,描述正确的有(A)

interfaceIDemo{

publicstaticfinalStringname;1

voidprint();2

publicvoidgetInfo();3

}

abstractclassPersonimplementsIDemo{4

publicvoidprint(){

}

}

A)第1行错误,没有给变量赋值

B)第2行错误,方法没有修饰符

C)第4行错误,没有实现接口的全部方法

D)第3行错误,没有方法的实现

17.接口和抽象类描述正确的有(BC)(两项)

A)抽象类没有构造函数

B)接口没有构造函数

C)抽象类不允许多继承

D)接口中的方法可以有方法体

18.以下描述错误的有(C)

A)abstract可以修饰类、接口、方法

B)abstract修饰的类主要用于被继承

C)abstract可以修饰变量

D)abstract修饰的类,其子类也可以是abstract修饰的

19.以下描述正确的有(B)

A)方法的重写应用在一个类的内部

B)方法的重载与返回值类型无关

C)构造方法不能重载

D)构造方法可以重写

构造方法不可以进行方法重写,可以重载。

20.以下程序运行结果是(A)

publicclassTestextendsFather{

privateStringname=”test”;

publicstaticvoidmain(String[]args){

Testtest=newTest();

System.out.println(test.getName());

}

}

classFather{

privateStringname=”father”;

publicStringgetName(){

returnname;

}

}

A)father

B)test

C)编译出错

D)运行出错,无输出

21.选项中哪一行代码可以替换题目中//addcodehere而不产生编译错误?

(A)

publicabstractclassMyClass{

publicintconstInt=5;

//addcodehere

publicvoidmethod(){

}

}

Apublicabstractvoidmethod(inta);

BconstInt=constInt+5;

C publicintmethod();

D publicabstractvoidanotherMethod(){}

 

22.System.out.println("5"+2);的输出结果应该是(A)。

A.52                  B.7                   

C.2                  D.5

 

23.在Java中,下面对于构造函数的描述正确的是(D)。

A)类必须显示定义构造函数

B)构造函数的返回类型是void构造函数没有返回类型。

C)构造函数和类有相同的名称,并且不能带任何参数

D)一个类可以定义多个构造函数

 

24.下列选项中关于java中super关键字的说法错误的是(B)

A)super关键字是在子类对象内部指代其父类对象的引用

B)super关键字不仅可以指代子类的直接父类,还可以指代父类的父类

C)子类可以通过super关键字调用父类的方法

D)子类可以通过super关键字调用父类的属性

 

25.分析下面这段Java代码,它的运行结果是(C)。

Importjava.io.*;

PublicclassB{

Publicstaticvoidmain(string[]args){

inti=12;

System.out.println(i+=i-=i*=i);}}

i*i=144;i-144=12-144=-132;i+(-132)=12+(-132)=-120

A)100

B)0

C)-120

D)程序无法编译

 

四、程序题(每小题15分,共30分)

P45613.713.12

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 经济学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1