- 关于 java.io.Serializable 接口说法正确的是:()
A. java.io.Serializable 中有一个 serialUID 属性,但是没有方法
B. 类通过实现 java.io.Serializable 接口以启用其对象的序列化功能
C. java.io.Serializable 中有一个 run 方法,但是没属性
D. java.io.Serializable 接口没有方法或属性,仅用于标识可序列化的语义。
答案:BD
- 请看下列代码:
public static void main(String[] args) throws Exception {
FileOutputStream fos=new FileOutputStream("record.dat");
BufferedOutputStream bos=new BufferedOutputStream(fos);
bos.write('a');
}
关于上述代码编译运行的结果的是:()
A. 编译错误
B. 实现向文件 record.dat 追加写入字符 a
C. 实现向文件 record.dat 覆盖写入字符 a
D. 文件 record.dat 无任何内容
答案:D
- 下列代码编译和运行的结果是:()
public static void main(String[] args) throws Exception {
try{
String[] s=null;
s[0]="test";
System.out.println(s[0]);
}catch (Exception ex){
System.out.println(1);
}catch (NullPointerException nep){
System.out.println(2);
}
}
A. 输出:test
B. 输出:Exception
C. 编译失败
D. 输出:NullPointerException
答案:C
13.下列代码编译和运行的结果是:
public class A {
void process() throws Exception{
throw new Exception();
}
public static void main(String[] args) {
new B().process();
}
}
class B extends A{
void process(){
System.out.println("B");
}
}
A. 输出:B
B. 无输出
C. A 类的 process 方法处出现编译错误
D. B 类的 process 方法处出现编译错误
答案:A
17.下列代码编译和运行的结果是:()
public class A {
public void process() {
System.out.println("A,");
}
public static void main(String[] args) {
try{
new B().process();
}catch (IOException e){
System.out.println("Exception");
}
}
}
class B extends A{
public void process() throws IOException{
super.process();
System.out.println("B");
throw new IOException();
}
}
A. 输出 Exception
B. 输出 A,B,Exception
C. 编译失败
D. 在运行时抛出 NullPointerException 异常
答案:C
18.下列代码运行的结果是:()
public class A {
Integer i;
int x;
public A(int y){
x=i+y;
System.out.println(x);
}
public static void main(String[] args) {
new A(new Integer(4));
}
}
A. 输出:4
B. 在运行时抛出 NullPointerException 异常
C. 在运行时抛出 NumberFormatException 异常
D. 在运行时抛出 IllegalStateException 异常
答案:B
26.下列代码编译和运行的结果是:
public class A {
public void methodA(){
B b=new B();
b.getValue();
}
public static void main(String[] args) {
A a=new A();
a.methodA();
}
}
class B{
public C c;
public String getValue(){
return c.getValue();
}
}
class C{
public String value;
public String getValue() {
value="Class C";
return value;
}
}
A. 编译失败
B. 输出 ClassC
C. 代码正确运行,但无输出
D. 在运行时抛出异常
答案:D
31.给定一个 Java 程序的 main 方法的代码片段如下:假如 d 盘下不存在
abc.txt 文件,现运行该程序,下面说法正确的是( )
public class A{
public static void main(String[] args) {
try{
PrintWriter out=
new PrintWriter(
new FileOutputStream("abc.txt")
);
String name="XXXX";
out.print(name);
out.close();
} catch (Exception e) {
System.out.println(1);
}
}
}
A. 将在控制台上打印:“文件没有发现!”
B. 正常运行,但没有生成文件 abc.txt
C. 运行后生成 abc.txt,但该文件中无内容
D. 运行后生成 abc.txt,该文件内容为:bonnie
答案:D
35.下列代码编译和运行的结果是:
public class A{
public static void main(String[] args) {
Object[] myObjects={
new Integer(12),new String("foo"),
new Integer(5),new Boolean(true)};
Arrays.sort(myObjects);
for (int i = 0; i <myObjects.length ; i++) {
System.out.println(myObjects[i].toString());
System.out.println(" ");
}
}
}
A. 代码"Object[] myObjects = { "行,编译出错
B. 代码"Arrays.sort(myObjects); "行,编译出错
C. 代码"Arrays.sort(myObjects); "行,抛出 ClassCastException 异常
D. 代码"System.out.print(myObjects[i].toString());"行,抛出ClassCastException 异常
答案:C
41.下列代码运行输出的结果是:
public class A{
static void test() throws RuntimeException{
try{
System.out.println("test ");
throw new RuntimeException();
}catch (Exception ex){
System.out.println("exception ");
}
}
public static void main(String[] args) {
try{
test();
}catch (RuntimeException ex){
System.out.println("runtime ");
}
System.out.println("end ");
}
}
A. test end
B. test runtime end
C. test exception end
D. test
答案:C
43.请看下列代码:
在<插入代码>处,填入下列代码,运行时能引起
java.lang.ClassCastException 异常的是:
interface Foo{}
public class A implements Foo{}
class B extends A{}
class C extends B{
public static void main(String[] args) {
B x=new B();
//<插入代码>
}
}
A. Alpha a = x;
B. Foo f= (Delta)x;
C. Foo f= (Alpha)x;
D. Beta b = (Beta)(Alpha)x;
答案:B