Java复习第二天---JavaSE基础(二)

2014-11-23 19:07:45 · 作者: · 浏览: 67

b = b.replace('a', 'd');

b = b.replace('b', 'c');

//使用b接受replace()方法的返回值

System.out.println("b="+b);

【13】代码:

for (int i =0; i < 4; i +=2) {

System.out.print(i + “”);

}

System.out.println(i);

结果是什么

A. 0 2 4

B. 0 2 4 5

C. 0 1 2 3 4

D. 编译错误.

E. 运行的时候抛出异常

在Java中,变量的生命周期(作用域范围)是{}之内

【14】下列关于类的继承的描述,正确的有( )

A. 一个类可以同时继承多个父类【Java是单继承的】

B. 一个类也只能有一个子类【Java中一个类可以有多个子类】

C. 子类能够直接调用父类所有的方法【权限限制】

D. 一个类继承另一个类需要使用 extends 关键字

【15】

11. class Payload {

12. private int weight;

13. public Payload(int wt) { weight = wt; }

13. public void setWeight(int w) { weight = w; }

15. public String toString(){

return Integer.toString(weight);

}

16. }

18. public class TestPayload {

19. static void changePayload(Payload p) {

20. /* insert code here */

21. }

22.

23. public static void main(String[] args) {

24. Payload p = new Payload(10);

25. p.setWeight(1024);

26. changePayload(p);

27. System.out.println(“The value of p is ”+ p);

28. }

29. }

Which statement, placed at line 20, causes(使得) the code to print “The value of p is 420.”

A. p.setWeight(420);

B. p.changePayload(420);

C. p = new Payload(420);

D. Payload.setWeight(420);

E. p = Payload.setWeight(420);

F. p = new Payload();p.setWeight(420);

【16】

1. abstract class abstractIt {

2. abstract float getFloat ();

3. }

4. public class AbstractTest extends AbstractIt {

5. private float f1= 1.0f;

6. private float getFloat () {return f1;}

7. }

What is the result

A. Compilation is successful.

B. An error on line 6 causes a runtime failure.

C. An error at line 6 causes compilation to fail.

D. An error at line 2 causes compilation to fail.

五、任务

任务一、单项选择题

【1】在Java中,下列语句不能通过编译的有( B )

A. String s= “join”+ 3;

B. int a= “join”+3;

C. int a= ‘a’+5;

D. float f=5+5.5F;

分析原因:因为int是整数型,不能做字符型相关的加减

【2】以下数据类型中,不是基本数据类型的是(B)

A.byte B.String C.boolean D.char

【3】基本数据类型byte的取值范围是:

A.0 ~ 65, 535

B.( 128) ~ 127

C.( 32,768) ~ 32,767

D.( 256) ~ 255

【4】下面哪行代码会在编译的时候出现错误或警告:

A.float f=1.3f;

B.char c =’a’;

C.byte b = 57;

D.boolean b = null;

E.int i = 10;

分析原因:第四个。因为null可以给所有的引用数据类型赋值,不能给基本数据类型赋值

【5】public static void main方法的参数描述是:

A.String[] args

B.Strings args[]

C.String args

D.String[] args[]

分析原因:(说明你没有选择的选项错在哪里)

此题选A,

B错:因为这是个字符串数据,String后面不能加s,

C错:因为它不符合数据形式

D错:因为不需要两个[],理同C

【6】下面代码b的值是多少 ( )

public class A {

public static void main (String args []) {

float a=3.5;

int b=(int)a+2;

System.out.println(b);

}

}

A.编译错误 B.5.5 C.5 D.6

分析原因:因为3.5是double型数据,不能直接用float修饰

【7】Given:

11. public class Test {

12. public static void main(String [] args) {

13. int x =5;

14. boolean b1 = true;

15. boolean b2 = false;

16. if((x==4) && !b2)

17. System.out.print(”1 “);

19. System.out.print(”2 “);

20. if ((b2 = true) && b1)

21. System.out.print(”3 “);

22. }

23. }

What is the result

A. 2 B. 3 C. 1 2 D. 2 3E. 1 2 3

F. Compilation fails

G. 运行时报错

分析原因:

因为第一个if语句返回的是false,所以if语句下面对应的输出就输出不出来了,接着2输出,然后第二个if语句返回的是true,所以3输出,所以选D;

任务二、编程

【1】编写程序,打印99乘法表,效果如图所示

public class test {

public static void main(String[] args) {

for (int i=1; i<10;i++) {

for (int j=1; j<=i;j++) {

if (i == j) {

System.out.println(i + "*" + j + "=" + i * j);

} else {

System.out.print(i + "*" + j + "=" + i * j + " ");

}

}

}

}

}

【2】根据下图实现类。

在 TestCylinder 类中创建 Cylinder 类的对象,利用构造器设置圆柱的底面半径和高,并输出圆柱的表面积和体积。

Circle类:package zuoye;

public class Circle {

private double radius;

public Circle(){}

public Circle(double radius){

this.setRadius(radius);

}

public double getRadius(){

retur