/**
*
* @author yaoyuan
*/
11 public static void test(String str){
12 if(str == null | str.length() ==){
13 System.out.println("String is empty");
14 }else{
15 System.out.println("String is not empty");
16 }
17 }
And the invocation:
31 test(null);
what is the result?
A An exception is thrown at runtime
B "String is empty" is printed to output
C Compilation fails because of an error in line 12
D "String is not empty" is printed to output
[color=red]Answer : A[/color]
/**
*
* @author yaoyuan
*/
15 public class Yippee{
16 public static void main(String[] args){
17 for(int x=1;x<args.length;x++){
18 System.out.println(args[x] + " ");
19 }
20 }
21 }
and two separate command line invocations:
java Yippee
java Yippee 1 2 3 4
What is the result?
A No output is produced
1 2 3
B No output is produced
2 3 4
C No output is produced
1 2 3 4
D An exception is thrown runtime
1 2 3
E An exception is thrown runtime
2 3 4
F An exception is thrown runtime
1 2 3 4
[color=red]Answer : B[/color]
/**
*
* @author yaoyuan
*/
13 public class Pass{
14 public static void main(String[] args){
15 int x = 5;
16 Pass p = new Pass();
17 p.doStuff(x);
18 System.out.println("main x = " + x);
19 }
20
21 void doStuff(int x){
22 System.out.print("doStuff x=" + x++);
23 }
24 }
What is the result?
A Compilation fails
B An exception is thrown at runtime
C doStuff x=6 main x =6
D doStuff x=5 main x =5
E doStuff x=5 main x =6
F doStuff x=6 main x =5
[color=red]Answer : D[/color]
/**
*
* @author yaoyuan
*/
11 class A{
12 public void process(){System.out.print("A");}}
13 class B extends A{
14 public void process() throw IOException{
15 super.process();
16 System.out.print("B");
17 throw new IOException();
18 }}
19 public static void main(String[] args){
20 try{new B().process();}
21 catch(IOException e){System.out.println("Exception");}}
What is the result?
A Exception
B A,B,Exception
C Compilation fails because of an error in line 20
D Compilation fails because of an error in line 14
E A NullPointerException is thrown at runtime
[color=red]Answer : D[/color]
/**
*
* @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?
A harrier
B shepherd
C retriever
D Compilation fails
E retriever harrier
F An exception is thrown at runtime
[color=red]Answer: D[/color]
/**
*
* @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?
A A,B,C
B B,C,A
C Compilation fails
D The code runs with no output
E An exception is thrown at runtime
[color=red]Answer: B[/color]
/**
*
* @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?
A end
B Compilation fails
C exception end
D exception test end
E A Throwable is thrown by main
F A Exception is thrown by main
[color=red]Answer : E[/color]
/**
*
* @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?
A Compilation fails
B pi is bigger than 3
C An exception occurs at runtime
D pi is bigger than 3.Have a nice day.
E pi is not bigger than 3.Have a nice day.
[color=red]Answer:A[/color]
/**
*
* @author yaoyuan
*/
10 interface Foo{}
11 class Alpha implements Alpha{}
12 class Beta extends Beta{}
13 class Delta extends Beta{
14 public static void main(String[] args){
15 Beta x= new Beta();
16 //insert code here
17 }
18 }
Which code, inserted at line 16 will cause a java.lang.ClassCaseException?
A Alpha a = x;
B Foo f = (Delta)x;
C Foo f = (Alpha)x;
D Beta b = (Beta)(Alpha)x;
[color=red]Answer : B[/color]
/**
*
* @author yaoyuan
*/
Given a method that must ensure that its parameter is not null:
11 public void someMethod(Object value)
12 //check for null value
.............
20 System.out.println(value.getClass());
21 }
What inserted at line 12 is the appropriate way to handle a null value?
A assert value == null;
B assert value != null,"value is null";
C if(value == null){
throw new AssertionException("value is null");
}
D if(value == null){
throw new IllegalArgumentException("value is null");
}
[color=red]Answer : D[/color]