}
}
throw new ArithmeticException(\没有\月份!\}
System.out.println(\您输入的月份为\月份\System.out.println(\请输入月份!\
System.out.println(\捕获ArithmeticException异常\System.out.println(e.toString());
}catch(ArrayIndexOutOfBoundsException e){ }catch(ArithmeticException e){
}
已知ArrayIndexOutOfBoundsException和ArithmeticException都是java.lang.*下的异常类,编译TestMonth.java后,用java TestMonth 13的运行结果是什么? 4、仔细阅读下面的程序代码,若经编译和运行后,请写出打印结果。
1) class TestString{
2) public void stringReplace (String text) { 3) text = text.replace('j' , 'i'); 4) text=text+\5) }
6) public void bufferReplace (StringBuffer text) { 7) text.setCharAt(0,'i'); 8) text = text.append(\9) }
10) public void change(char ch[]){ 11) ch[0]= 'Y'; 12) }
13) public static void main(String args[]){ 14) String str1=\
15) StringBuffer str2=new StringBuffer(\16) char ch[]={'j','a','v','a'}; 17) TestString t=new TestString(); 18) t.change(ch);
19) t.stringReplace (str1); 20) t.bufferReplace (str2); 21) System.out.println(str1);
22) System.out.println(str2.toString()); 23) System.out.println (new String(ch)); 24) } 25) }
5、阅读下面的程序代码,判断18~27行(带划线部分)各语句编译是否通过,如果编译通过,直接写出该行的打印结果。
1. class TestString{
2. public static String stringReplace (String text) {
9
3. text = text.replace('j' , 'i'); 4. text=text+\5. return text; 6. }
7. public static StringBuffer bufferReplace (StringBuffer text) { 8. text.setCharAt(0,'i'); 9. text = text.append(\10. return text; 11. }
12. public static void main(String args[]){ 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25.
String str1=\String str2=\
String str3=\String str4=new String(\
StringBuffer str5=new StringBuffer(\
System.out.println(str1= =str2); System.out.println(str2= =str3); System.out.println(str2= =str4); System.out.println(str4= =str5);
System.out.println(str3.equals(str4)); System.out.println(str4.equals(str5)); System.out.println (stringReplace (str1)); System.out.println(bufferReplace (str5));
26. System.out.println (str1); 27. System.out.println(str5); 28. } 29. }
6、阅读下面的程序,修改程序中编译错误的地方(提示:共五处错误)
interface Shape{ double PI;
public double area( ); public double perimeter( ); }
class Cycle extends Shape{ private double r; public Cycle(double r){ this.r=r; }
double area( ){
System.out.println(PI*r*r); } }
10
public class Test{
public static void main(String args[]){ Cycle c=new Cycle(1.5);
System.out.println(\面积为:\ } }
三、简答题
1、对于String对象,可以使用“=”赋值,也可以使用new关键字赋值,两种方式有什么区别?
解析:String类型是实际工作中经常使用到的类型,从数据类型上划分,String是一个引用类型,是API中定义的一个类。所以String类型的对象可以使用new创建,例如String name=new String(“ETC”);为变量name进行赋值,值为“ETC”。然而,String类比起其他类有些特殊,可以使用“=”直接赋值,如String name=“ETC”,也是为变量name进行赋值,值为“ETC”。这两种赋值方式是有差别的,使用new赋值,永远都是创建一个新对象,在新的内存空间初始化了字符串的值;而使用“=”赋值,不会每次都初始化新的字符串,而是从一个“字符串实例池”中去查找有没有要赋值的字符串,如有则直接引用;如不存在,则初始化一个字符串,并放入“字符串实例池”。在实际编程中,往往使用“=”好对String类型变量进行赋值。
参考答案:使用“=”赋值不一定每次都创建一个新的字符串,而是从“字符串实例池”中查找字符串。使用new进行赋值,则每次都创建一个新的字符串。 2、什么叫引用类型,引用类型和基本数据类型有什么区别?
解析:初级程序员常常对float和Float或者double和Double感到混淆。在Java语言中,有八中基本数据类型,即byte、int、long、float、double、char、boolean。对应这八种基本数据类型,API中定义了八个类,能把这些基本类型转换成引用类型,分别是Byte、Short、Int、Long、Double、Character、Boolean。这八个类被统称为包装器类。JDK5之后,包装器类和基本数据类型之间可以直接转换,称为自动的装箱拆箱(boxing/unboxing)。例如integer it=3;it++;虽然写法上可以像使用基本数据类型一样使用包装器类型,但是本质上依**行了类似 it= new Integer(3)的转换,因此,不要轻易使用包装器类的自动装箱拆箱,以优化的性能。能够使用基本类型就使用基本类型。
参考答案:包装类器包括Byte、Short、Integer、Long、Float、Double、Character、Boolean等类,主要用来对byte、short、integer、long、float、double、character、boolean这八种基本数据类型进行包装,使其称为引用类型。 3、String类和StringBuffer类有什么区别?
解析:初级程序员常常对float和Float或者double和Double感到混淆。在Java语言中,有八中基本数据类型,即byte、int、long、float、double、char、boolean。对应这八种基本数据类型,API中定义了八个类,能把这些基本类型转换成引用类型,分别是Byte、Short、Int、Long、Double、Character、Boolean。这八个类被统称为包装器类。JDK5之
11
后,包装器类和基本数据类型之间可以直接转换,称为自动的装箱拆箱(boxing/unboxing)。例如integer it=3;it++;虽然写法上可以像使用基本数据类型一样使用包装器类型,但是本质上依**行了类似 it= new Integer(3)的转换,因此,不要轻易使用包装器类的自动装箱拆箱,以优化的性能。能够使用基本类型就使用基本类型。
参考答案:包装类器包括Byte、Short、Integer、Long、Float、Double、Character、Boolean等类,主要用来对byte、short、integer、long、float、double、character、boolean这八种基本数据类型进行包装,使其称为引用类型。 4、包装器类型包括哪些类,有什么作用?
解析:初级程序员常常对float和Float或者double和Double感到混淆。在Java语言中,有八中基本数据类型,即byte、int、long、float、double、char、boolean。对应这八种基本数据类型,API中定义了八个类,能把这些基本类型转换成引用类型,分别是Byte、Short、Int、Long、Double、Character、Boolean。这八个类被统称为包装器类。JDK5之后,包装器类和基本数据类型之间可以直接转换,称为自动的装箱拆箱(boxing/unboxing)。例如integer it=3;it++;虽然写法上可以像使用基本数据类型一样使用包装器类型,但是本质上依**行了类似 it= new Integer(3)的转换,因此,不要轻易使用包装器类的自动装箱拆箱,以优化的性能。能够使用基本类型就使用基本类型。
参考答案:包装类器包括Byte、Short、Integer、Long、Float、Double、Character、Boolean等类,主要用来对byte、short、integer、long、float、double、character、boolean这八种基本数据类型进行包装,使其称为引用类型。
5、使用代码,创建一个长度为5的String型数组,并使用增强for循环迭代数组打印出数组中的元素。
解析:增强for循环是JDK5增加的特性,可以方便地遍历数组或集合。程序员需要了解的是,如果JDK版本低于5.0,则不支持这个功能。另外,并不是说有了增强for循环后,传统的for循环就不被使用,增强for循环只能用来方便地遍历数组和集合,其他情况下还要使用传统的for循环。
参考答案:
String[] sArray = new String[5];
For(String s:sArray){ System.out.println(s); }
6、Object类有什么特点?
解析:Object类是一个非常重要的类,是所有类的父类,包括数组在内,也就是对一个Java类,不管是API中定义的,还是自定义的类,都直接或间接的继承了Object类。所以,如果
12
相关推荐: