SCJP认证试题(八)
/** * * @author yaoyuan */11public static void test(String str){12if(str == null | str.length() ==){13System.out.println("String is empty");14}else{15System.out.println("String is not empty");16}17}And the invocation:
31test(null);
what is the result?
AAn exception is thrown at runtime
B"String is empty" is printed to output
CCompilation fails because of an error in line 12
D"String is not empty" is printed to output
Answer: A
/** * * @author yaoyuan */15public class Yippee{16public static void main(String[] args){17for(int x=1;x<args.length;x++){18System.out.println(args + " ");19}20}21}
and two separate command line invocations:
java Yippee
java Yippee 1 2 3 4
What is the result?
ANo output is produced
1 2 3
BNo output is produced
2 3 4
CNo output is produced
1 2 3 4
DAn exception is thrown runtime
1 2 3
EAn exception is thrown runtime
2 3 4
FAn exception is thrown runtime
1 2 3 4
Answer: B
/** * * @author yaoyuan */13public class Pass{14public static void main(String[] args){15int x = 5;16Pass p = new Pass();17p.doStuff(x);18System.out.println("main x = " + x);19}2021void doStuff(int x){22System.out.print("doStuff x=" + x++);23}24}
What is the result?
ACompilation fails
BAn exception is thrown at runtime
CdoStuff x=6 main x =6
DdoStuff x=5 main x =5
EdoStuff x=5 main x =6
FdoStuff x=6 main x =5
Answer : D
/** * * @author yaoyuan */11class A{12public void process(){System.out.print("A");}}13class B extends A{14public void process() throw IOException{15super.process();16System.out.print("B");17throw new IOException();18}}19public static void main(String[] args){20try{new B().process();}21catch(IOException e){System.out.println("Exception");}}
What is the result?
AException
BA,B,Exception
CCompilation fails because of an error in line 20
DCompilation fails because of an error in line 14
EA NullPointerException is thrown at runtime
Answer : D
/** * * @author yaoyuan */public class Test{public enum Dogs{collie,harrier,shepherd};public static void main(String[] args){Dogs myDog = Dogs.shepherd;switch(myDog){case collie:System.out.print("collie");case default:System.out.print("retriever");case harrier:System.out.print("harrier");}}}
What is the result?
Aharrier
Bshepherd
Cretriever
DCompilation fails
Eretriever harrier
FAn exception is thrown at runtime
Answer:D
/** * * @author yaoyuan */public static Collection get(){Collection sorted = new LinkedList();sorted.add("B");sorted.add("C");sorted.add("A");return sorted;}public static void main(String[] args){for(Object obj:get()){System.out.print(obj + ", ");}}
What is the result?
AA,B,C
BB,C,A
CCompilation fails
DThe code runs with no output
EAn exception is thrown at runtime
Answer: B
/** * * @author yaoyuan */static void test() throws Error{if(true) throw new AssertionError();System.out.print("test");}public static void main(String[] args){try{test();}catch(Exception ex){System.out.print("exception");}System.out.println("end");}
What is the result?
Aend
BCompilation fails
Cexception end
Dexception test end
EA Throwable is thrown by main
FA Exception is thrown by main
Answer : E
/** * * @author yaoyuan */Float pi = new Float(3.14f);if(pi > 3){System.out.print("pi is bigger than 3.");}else{System.out.print("pi is not bigger than 3");}finally{System.out.print("Have a nice day.");}
What is the result?
ACompilation fails
Bpi is bigger than 3
CAn exception occurs at runtime
Dpi is bigger than 3.Have a nice day.
Epi is not bigger than 3.Have a nice day.
Answer:A
/** * * @author yaoyuan */10interface Foo{}11class Alpha implements Alpha{}12class Beta extends Beta{}13class Delta extends Beta{14public static void main(String[] args){15Beta x= new Beta();16//insert code here17}18}
Which code, inserted at line 16 will cause a java.lang.ClassCaseException?
AAlpha a = x;
BFoo f = (Delta)x;
CFoo f = (Alpha)x;
DBeta b = (Beta)(Alpha)x;
Answer : B
/**
*
* @author yaoyuan
*/
Given a method that must ensure that its parameter is not null:
11public void someMethod(Object value)12//check for null value.............20System.out.println(value.getClass());21}
What inserted at line 12 is the appropriate way to handle a null value?
Aassert value == null;
Bassert value != null,"value is null";
Cif(value == null){
throw new AssertionException("value is null");
}
Dif(value == null){
throw new IllegalArgumentException("value is null");
}
Answer : D
页:
[1]