java题库 接口_Java题库——Chapter13抽象类和接口

1)What is the output of running class Test?

public classTest {public static voidmain(String[ ] args) {newCircle9();

}

}public abstract classGeometricObject {protectedGeometricObject() {

System.out.print("A");

}protected GeometricObject(String color, booleanfilled) {

System.out.print("B");

}

}public class Circle9 extendsGeometricObject {/**Default constructor*/

publicCircle9() {this(1.0);

System.out.print("C");

}/**Construct circle with a specified radius*/

public Circle9(doubleradius) {this(radius, "white", false);

System.out.print("D");

}/**Construct a circle with specified radius, filled, and color*/

public Circle9(double radius, String color, booleanfilled) {super(color, filled);

System.out.print("E");

}

}

A)CBAE B)BACD C)ABCD D)BEDC E)AEDC

调用过程:先初始化一个Circle9对象时,需要调用Circle9的缺省参数的构造函数Circle9() ,而这个缺省参数的构造函数调用Circle9(double radius) ,而Circle9(double radius) 又调用Circle9(double radius, String color, boolean filled) ,而Circle9(double radius, String color, boolean filled)需要调用其父类构造函数GeometricObject(String color, boolean filled)这样层层调用,在调用栈中就是BEDC

2)Which of the following class definitions defines a legal abstract class? 4) _______

A)abstract class A { abstract void unfinished(); }

B)class A { abstract void unfinished() {     } }

C)public class abstract A { abstract void unfinished(); }

D)class A { abstract void unfinished(); }

abstract要写在 class或void之前

3)Which of the following statements are correct? (Choose all that apply.) 6) _______

A)Number i = 4.5; B) Double i = 4.5; C)Object i = 4.5; D) Integer i = 4.5;

Number类是数值包装类的抽象父类,Object是所有类的父类

4)The printout from the following code is ________.

java.util.ArrayList list = newjava.util.ArrayList();

list.add("New York");

java.util.ArrayList list1=list;

list.add("Atlanta");

list1.add("Dallas");

System.out.println(list1);

A)[New York, Atlanta, Dallas]B) [New York, Atlanta] C)[New York, Dallas] D) [New York]

注意这里的list1不是new出来的,与list指向相同的内存区域

5)Analyze the following code.

public classTest {public static voidmain(String[ ] args) {

Number x= new Integer(3);

System.out.println(x.intValue());

System.out.println((Integer)x.compareTo(new Integer(4)));

}

}

A)The program has a compile error because the member access operator (.) is executed before the casting operator.

B)The program has a compile error because an Integer instance cannot be assigned to a Number variable.

C)The program has a compile error because x cannot be cast into Integer.

D)The program has a compil

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 什么是 Java 异常? Java 异常是指程序执行期间可能发生的错误或异常情况,例如除以零、数组越界、空指针引用等。当这些异常发生时,Java 虚拟机会抛出一个异常对象,并且程序的执行流程将被中断。 2. Java 异常处理机制有哪些关键字和语句? Java 异常处理机制包括以下关键字和语句: - try:用于包含可能会抛出异常的代码块。 - catch:用于捕获指定类型的异常,并在捕获到异常时执行相应的处理代码。 - finally:用于包含无论是否发生异常都需要执行的代码块。 - throw:用于抛出指定的异常对象。 - throws:用于声明可能会抛出指定类型异常的方法。 3. Java 中的异常分为哪几类? Java 中的异常分为两大类:Checked Exception 和 Unchecked Exception。 Checked Exception 是指在编译时就能够检查出来的异常,例如 IOException、ClassNotFoundException 等。程序必须显式地处理这些异常,否则编译不通过。 Unchecked Exception 是指在运行时才能检查出来的异常,例如 NullPointerException、ArrayIndexOutOfBoundsException 等。程序可以选择处理这些异常,但不处理也不会导致编译错误。 4. 请简要说明 try-catch-finally 的执行流程。 当程序执行到 try 块时,Java 会尝试执行其中的代码。如果在 try 块中抛出了异常,则会将异常对象传递给 catch 块进行处理。catch 块会匹配异常类型,如果匹配成功,则执行相应的处理代码。如果 catch 块处理完异常后,程序需要继续执行,则会执行 finally 块中的代码。如果 finally 块中也抛出了异常,则该异常会覆盖 try 或 catch 块中的异常。 如果 try 块中没有抛出异常,则 catch 块不会被执行。如果 finally 块中抛出异常,则该异常会覆盖 try 块中的异常。 5. 什么是异常链? 异常链是指在处理异常时,将一个异常对象作为另一个异常的原因,并将它们组合成一个异常链。这样做的好处是,在抛出异常时可以同时传递多个异常信息,从而更加清晰地表示异常发生的原因。 6. 请简要说明 try-with-resources 的作用和使用方法。 try-with-resources 是 Java 7 中引入的语法,用于自动关闭实现了 AutoCloseable 接口的资源。在 try 块中声明需要使用的资源,Java 会在 try 块执行完毕后自动关闭这些资源,无需手动调用 close 方法。 try-with-resources 的语法如下: ``` try (Resource1 r1 = new Resource1(); Resource2 r2 = new Resource2()) { // 使用资源 } catch (Exception e) { // 处理异常 } ``` 7. 请简要说明 Java 中的文本 IO。 Java 中的文本 IO 主要包括两种类:Reader 和 Writer。Reader 用于读取字符流,而 Writer 用于写入字符流。 Java 中常用的 Reader 类包括 InputStreamReader、FileReader 和 BufferedReader,常用的 Writer 类包括 OutputStreamWriter、FileWriter 和 BufferedWriter。这些类提供了各种方法来读取和写入字符流,并且可以处理多种编码格式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值