SCJP认证试题(六)

本文探讨了Java中异常处理的实践案例,包括NullPointerException的处理及不同情况下的执行流程。同时,介绍了使用Collections.sort()对字符串列表进行排序的方法,并展示了HashSet在存储自定义对象时的行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 /**
*
* @author yaoyuan
*/
public class A{
public void method1(){
B b = new B();
b.method2();
5 //more code here
}
}

public class B{
public void method2(){
C c = new C();
c.method3();
5 //more code here
}
}

public class C{
public void method3(){
//more code here
}

}

try{
A a = new A();
27 a.methor1();
}
catch(Exception e){
29 System.out.print("an error occurred");
}


/**
*Which two statements are true if a NullPointerException is thrown on line 13 of class C?(choose two)
*
*
*A The application will crash
*B The code on line 29 will be executed
*C The code on line 5 of class A will execute
*D The code on line 5 of class B will execute
*E The exception will be propagated back to line 27
*/

[color=red]// Answer : B E[/color]


 /**
*
* @author yaoyuan
*/
25 int x = 12;
26 while(x < 10){
27 x--;
28 }
29 System.out.print(x);

/**
*What is the result?
*
*
*A 0
*B 10
*C 12
*D Line 29 will never be reached
*/

[color=red]// Answer : C[/color]


 /**
*
* @author yaoyuan
*/

1 public class CertKiller2{
2 Integer I;
3 int x;
4
5 public CertKiller2(int y){
6 x = I + y;
7 System.out.println(x);
8 }
9
10 public static void main(String[] args){
11 new CertKiller2(new Integer(4));
12 }
13 }

/**
*What is the result?
*
*
*A The value "4" is printed at the command line
*B Compilation fails because of an error in line 6
*C Compilation fails because of an error in line 11
*D A NullPointerException occursatruntime
*E A NumberForm at Exception occursatruntime
*F An IllegalStateException occursatruntime
*/

[color=red]// Answer : D[/color]



/**
*
* @author yaoyuan
*/
1 public static Iterator reverse(List list){
2 Collections.reverse(list);
3 return list.iterator();
4 }
5 public static void main(String[] args){
6 List list = new ArrayList();
7 list.add("1");list.add("2");list.add("3");
8 for(Object obj; reverse(list))
9 System.out.print(obj + ", ");
10 }

/**
*What is the result?
*
*
*A 3,2,1
*B 1,2,3
*C Compilation fails
*D The code runs with no output
*E An exception is thrown at runtime
*/

[color=red]// Answer : C[/color]



/**
*
* @author yaoyuan
*/

11 public void testIfA(){
12 if(testIfB("true")){
13 System.out.println("True");
14 }else{
15 System.out.println("Not true");
16 }
17 }
18 public Boolean testIfB(String str){
19 return Boolean.valueof(str);
20

/**
*What is the result when method testIfA is invoked?
*
*
*A True
*B Not true
*C An exception is thrown at runtime
*D Compilation fails because of an error at line 12
*E Compilation fails because of an error at line 19
*/

[color=red]// Answer : A[/color]


 /**
*
* @author yaoyuan
*/

23 int z = 5;
24
25 public void CertKiller1(int x){
26 assert(x > 0);
27 switch(x){
28 case 2 x=3;
29 default; assert false; }}
30
31 private void CertKiller2(int y){assert(y < 0);}
32
33 private void CertKiller4(){assert(CertKiller4());}
34
35 private Boolean CertKiller4(){z = 6; return false;}

/**
*Which statement is true?
*
*
*A All of the assert statements are used appropriately
*B Only the assert statements line 31 is used appropriately
*C The assert statements on lines 29 and 31 are used appropriately
*D The assert statements on lines 26 and 29 are used appropriately
*E The assert statements on lines 29 and 33 are used appropriately
*F The assert statements on lines 29 ,31 and 33 are used appropriately
*G The assert statements on lines 26, 29 and 31 are used appropriately
*/

[color=red]// Answer : C[/color]


 /**
*
* @author yaoyuan
*/

11 public static void main(String[] args){
12 String str = "null";
13 if(str == null){
14 System.out.println("null");
15 }else(str.length() == 0){
16 System.out.println("zero");
17 }else{
18 System.out.println("some");
19 }
20 }

/**
*What is the result?
*
*
*A null
*B zero
*C some
*D Compilation fails
*E An exception is thrown at runtime
*/

[color=red]// Answer : D[/color]



/**
*
* @author yaoyuan
*/

11 public static void main(String[] args){
12 try{
13 args = null;
14 aegs[0] = "test";
15 System.out.println(args[0]);
16 }catch(Exception ex){
17 System.out.println("Exception");
18 }catch(NullPointerException npe){
19 System.out.println("NullPointerException");
20 }
21 }


/**
*What is the result?
*
*
*A test
*B Exception
*C Compilation fails
*D NullPointerException
*/

[color=red]// Answer : C[/color]



/**
*
* @author yaoyuan
*/
1 import java.util.*;
2
3 public class LetterASort{
4 public static void main(String[] args){
5 ArrayList<String> strings = new ArrayList<String>();
6 strings.add("aAaA");
7 strings.add("AaAa");
8 strings.add("aAa");
9 strings.add("AAaa");
10 Collections.sort(strings);
11 for(String s : strings) {System.out.print(s + " ");}
12 }
13 }

/**
*What is the result?
*
*
*A Compilation fails
*B aA aA aA a A A aa A aA
*C AA aa AaA aA aAa aA aA
*D A aA AA aa aA aA aAa
*E aAa AaA aA aA AA aa
*F An exception is thrown at runtime
*/
[color=red]
// Answer : C
[/color]


 /**
*
* @author yaoyuan
*/

1 import java.util.*;
2 public class WrappedString{
3 private String s;
4 public WappedString(String s){this.s = s;}
5 public static void main(String[] args){
6 HashSet<Object> hs = new HashSet<Object>();
7 WappedString ws1 = new WappedString("aardvark");
8 WappedString ws2 = new WappedString("aardvark");
9 String s1 = new String("aardvark");
10 String s2 = new String("aardvark");
11 hs.add(ws1);hs.add(ws2);hs.add(s1);hs.add(s2);
12 System.out.println(hs.size());}}

/**
*What is the result?
*
*
*A 0
*B 1
*C 2
*D 3
*E 4
*F Compilation fails
*G An exception is thrown at runtime
*/

[color=red]// Answer : D[/color]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值