yuanyao 发表于 2013-2-4 23:00:43

SCJP认证试题(六)

/** * * @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();27a.methor1();}catch(Exception e){29System.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
*BThe code on line 29 will be executed
*CThe code on line 5 of class A will execute
*DThe code on line 5 of class B will execute
*EThe exception will be propagated back to line 27
*/

// Answer : B E



/** * * @author yaoyuan */25int x = 12;26while(x < 10){27x--;28}29System.out.print(x);
/**
*What is the result?
*
*
*A      0
*B10
*C12
*DLine 29 will never be reached
*/

// Answer : C



/** * * @author yaoyuan */1 public class CertKiller2{2Integer I;3int x;45public CertKiller2(int y){6x = I + y;7System.out.println(x);8}910public static void main(String[] args){11new CertKiller2(new Integer(4));12}13 }
/**
*What is the result?
*
*
*A   The value "4" is printed at the command line
*BCompilation fails because of an error in line 6
*CCompilation fails because of an error in line 11
*DA NullPointerException occursatruntime
*EA NumberForm at Exception occursatruntime
*F An IllegalStateException occursatruntime
*/

// Answer : D



/** * * @author yaoyuan */1public static Iterator reverse(List list){2Collections.reverse(list);3return list.iterator();4}5public static void main(String[] args){6List list = new ArrayList();7list.add("1");list.add("2");list.add("3");8for(Object obj; reverse(list))9System.out.print(obj + ", ");10}
/**
*What is the result?
*
*
*A      3,2,1
*B1,2,3
*CCompilation fails
*DThe code runs with no output
*EAn exception is thrown at runtime
*/

// Answer : C



/** * * @author yaoyuan */11public void testIfA(){12if(testIfB("true")){13System.out.println("True");14}else{15System.out.println("Not true");16}17}18public Boolean testIfB(String str){19return Boolean.valueof(str);20
/**
*What is the result when method testIfA is invoked?
*
*
*A      True
*BNot true
*CAn exception is thrown at runtime
*DCompilation fails because of an error at line 12
*ECompilation fails because of an error at line 19
*/

// Answer : A




/** * * @author yaoyuan */23int z = 5;2425public void CertKiller1(int x){26assert(x > 0);27switch(x){28case 2 x=3;29default; assert false; }}3031private void CertKiller2(int y){assert(y < 0);}3233private void CertKiller4(){assert(CertKiller4());}3435private Boolean CertKiller4(){z = 6; return false;}
/**
*Which statement is true?
*
*
*A      All of the assert statements are used appropriately
*BOnly the assert statements line 31 is used appropriately
*CThe assert statements on lines 29 and 31 are used appropriately
*DThe assert statements on lines 26 and 29 are used appropriately
*EThe assert statements on lines 29 and 33 are used appropriately
*FThe assert statements on lines 29 ,31 and 33 are used appropriately
*GThe assert statements on lines 26, 29 and 31 are used appropriately
*/

// Answer : C



/** * * @author yaoyuan */11public static void main(String[] args){12String str = "null";13if(str == null){14System.out.println("null");15}else(str.length() == 0){16System.out.println("zero");17}else{18System.out.println("some");19}20}
/**
*What is the result?
*
*
*A      null
*Bzero
*Csome
*DCompilation fails
*EAn exception is thrown at runtime
*/

// Answer : D


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

/**
*What is the result?
*
*
*A      test
*BException
*CCompilation fails
*DNullPointerException
*/

// Answer : C
/** * * @author yaoyuan */1import java.util.*;23public class LetterASort{4public static void main(String[] args){5ArrayList<String> strings = new ArrayList<String>();6strings.add("aAaA");7strings.add("AaAa");8strings.add("aAa");9strings.add("AAaa");10Collections.sort(strings);11for(String s : strings){System.out.print(s + " ");}12}13}
/**
*What is the result?
*
*
*A      Compilation fails
*BaA aA aA a A A aa A aA
*CAA aa AaA aA aAa aA aA
*DA aA AA aa aA aA aAa
*EaAa AaA aA aA AA aa
*FAn exception is thrown at runtime
*/

// Answer : C




/** * * @author yaoyuan */1import java.util.*;2public class WrappedString{3private String s;4 public WappedString(String s){this.s = s;}5public static void main(String[] args){6HashSet<Object> hs = new HashSet<Object>();7WappedString ws1 = new WappedString("aardvark");8WappedString ws2 = new WappedString("aardvark");9String s1 = new String("aardvark");10String s2 = new String("aardvark");11hs.add(ws1);hs.add(ws2);hs.add(s1);hs.add(s2);12System.out.println(hs.size());}}
/**
*What is the result?
*
*
*A      0
*B1
*C2
*D3
*E4
*FCompilation fails
*GAn exception is thrown at runtime
*/

// Answer : D
页: [1]
查看完整版本: SCJP认证试题(六)