OCP 803 Exams – 03

      在〈OCP 803 Exams – 03〉中尚無留言
Q-041
public class Test{
    static void dispResult(int[] num){
        try{
            System.out.println(num[1]/(num[1]-num[2]));
        }
        catch(ArithmeticException e){
            System.err.println("first exception");
        }
        System.out.println("Done");
    }
    public static void main(String[] args) {
        try{
            int[] arr={100,100};
            dispResult(arr);
        }
        catch (IllegalArgumentException e){
            System.err.println("second exception");
        }
        catch(Exception e){
            System.err.println("third exception");
        }
    }
}
What is the result?
A) 0
   Done
B) first exception
   Done
C) second exception
D) Done
   third exception
E) third exception

Answer : E
num[2]產生ArrayIndexOutOfBoundsException, 所以是main的third exception
若是num[0]-num[1], 則除0 後, 會產生ArithmeticException, 因此會產生first exception
Q-042
public class Test{
    public static void main(String[] args) {
        String theString="Hello World";
        System.out.println(theString.charAt(11));
    }
}
What is the result?
A) The program prints nothing.
B) d
C) A StringIndexOutOfBoundsException is thrown at runtime.
D) An ArrayIndexOutOfBoundsException is thrown at runtime.
E) A NullPointerException is thrown at runtime.

Answer : C
Q-043
class X{
    public void  nX(){
        System.out.println("Xn1");
    }
}
class Y extends X{
    public void nX(){
        System.out.println("Xn2");
    }
    public void nY(){
        System.out.println("Yn");
    }
}
public class Test{
    public static void main(String[] args) {
        X xRef=new Y();
        Y yRef=(Y)xRef;
        yRef.nY();
        xRef.nX();
    }
}
What is the result?
A) Yn
   Xn2
B) Yn
   Xn1
C) Compliation fails.
D) A classCastException is thrown at runtime.

Answer : A
Q-044
class SpecialException extends Exception{
    public SpecialException(String message){
        super(message);
        System.out.println(message);
    }
}
public class ExceptionTest{
    public static void main(String[] args) {
        try{
            doSomething();
        }
        catch(SpecialException e){
            System.out.println(e);
        }
    }
    static void doSomething() throws SpecialException{
        int[] ages=new int[4];
        ages[4]=17;
        doSomethingElse();
    }
    static void doSomethingElse() throws SpecialException{
        throw new SpecialException("Thrown at end of doSomething() method");
    }
}
What will be the output?
A)SpecialException : Thrown at end of doSomething() method
B)Error in thread "main" java.lang.ArrayIndexOutOfBoundsError
C) Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
   at ExceptionTest.doSomething(ExceptionTest.java:13)
   at ExceptionTest.main(ExceptionTest.java:4)
D) SpecialException: Thrown at end of doSomething() method
   at ExceptionTest.doSomethingElse(ExceptionTest.java:16)
   at ExceptionTest.doSomething(ExceptionTest.java:13)
   at ExceptionTest.main(ExceptionTest.java:4)

Answer : C
ArrayIndexOutOfBounds不需自己撰寫Exception
若ages[4]改為ages[3]時, 會產生
Thrown at end of doSomething() method
exceptiontest.SpecialException: Thrown at end of doSomething() method
Q-045
public class Test{
    public static void main(String[] args) {
        Test t=new Test();
        try{
            t.doPrint();
            t.doList();
        }
        catch(Exception eZ){
            System.out.println("Caught "+eZ);
        }
    }
    public void doList() throws Exception{
        throw new Exception("Error");
    }
    public void doPrint() throws Exception{
        throw new RuntimeException("Exception");
    }
}
結果是
Caught java.lang.RuntimeException: Exception
Q-046
class Alpha{
    public String doStuff(String msg){
        return msg;
    }
}
class Beta extends Alpha{
    public String doStuff(String msg){
        return msg.replace('a','e');
    }
}
class Gamma extends Beta{
    public String doStuff(String msg){
        return msg.substring(2);
    }
}
public class Test{
    public static void main(String[] args) {
        List strs=new ArrayList();
        strs.add(new Alpha());
        strs.add(new Beta());
        strs.add(new Gamma());
        for (Alpha t: strs){
            System.out.println(t.doStuff("Java"));
        }
    }
}
What is the result?
A) Java
   Java
   Java
B) Java
   Jeve
   va
C) Java
   Java
   ve
E) Compilation fails.

Answer : B
Q-047
Which two are valid declarations of a two-dimension array?
A) int[][] array20;
B) int[2][2] array20;
C) int array20[];
D) int[] array20[];
E)int [][] array20[];

Answer : A, D
Q-048
class Student{
    String name;
    int age;
}
public class Test{
    public static void main(String[] args) {
        Student s1=new Student();
        Student s2=new Student();
        Student s3=new Student();
        s1=s3;
        s3=s2;
        s2=null;
    }
}
Which statement is true?
A) After line8, three objects are eligible for garbage collection.
B) After line8, two objects are eligible for garbage collection.
C) After line8, one object are eligible for garbage collection.
D) After line8, none of the object are eligible for garbage collection.

Answer : C
文件的答案說是A, 但似乎是錯的答案,應該是C
Q-049
public class Test{
    public static void main(String[] args) {
        int arr[]={1,2,3};
        for (int var:arr){
            int i=1;
            while(i<=var);
                System.out.print(i++);
        }
    }
}
What is the result?
A) 1 1 1
B) 1 2 3
C) 1 1 2 1 2 3
D) Compilation fails.
E) The loop executes infinite times.

Answer : E
就死在while(i<=var); 若沒有 ";" 則結果是E
Q-050
Given the code fragment
7.  StringBuilder sb1=new StringBuilder("Duke");
8.  String str1=sb1.toString();
9.  //insert code here
10. System.out.print(str1==str2);
Which code fragment, when inserted at line 9, enables the code to print true?
A) String str2=str1;
B) String str2=new String(str1);
C) String str2=sb1.toString();
D) String str2="Duke";

Answer : A
Q-051
interface Contract{}
class Super implements Contract{}
class Sub extends Super{}
public class Test{
    public static void main(String[] args) {
        List objs=new ArrayList();
        Contract c1=new Super(); //line n1
        Contract c2=new Sub();
        Super s1=new Sub();      //line n2
        objs.add(c1);objs.add(c2);objs.add(s1);
        for (Object item: objs){
            System.out.println(item.getClass().getName());
        }
    }
}
What is the result?
A) Super
   Sub
   Sub
B) Contract
   Contract
   Super
C) Compilation fails at line n1.
D) Compilation fails at line n2.

Answer : A
Q-052
Given the code fragments
public class Test extends Root{
    public static void main(String[] args) {
        Root r=new Test();
        System.out.println(r.method1());  //line n1
        System.out.println(r.method2());  //line n2
    }
}
class Root{
    private static final int MAX = 20000;
    private int method1(){
        int a=100+MAX;  //line n3
        return a;
    }
    protected int method2(){
        int a=200 + MAX; //line n4
        return a;
    }
}
Which line causes a compilation error?
A) line n1
B) line n2
C) line n3
D) line n4

Answer : A
Q-053
Given the code fragment
System.out.println(25+5<=4+29);
System.out.println((25+5) <= (4+29));
What is the result?
A) 25 false 29
   true
B) 285 < 429
   true
C) true
   true
D) Compilation fails.

Answer : C
Q-054
Given
public class Falindrome{
    public static int main(String[] args){
        System.out.print(args [1]);
        return 0;
    }
}
And the commands:
javac Falindrome.java
java Falindrome Wow Mon

What is the result?
A) Compilation fails.
B) The code compiles, but does not execute.
C) Faildrome
D) Wow
E) Mon

Answer : B
Q-055
Given
1. public class Test{
2.     public static void main(String[] args) {
3.         float myarray[]={10.20f, 20.30f, 30.40f, 50.60f};
4.         int index=0;
5.         boolean isFound=false;
6.         float key=30.40f;
7.         //insert code here
8.         System.out.println(isFound);
9.     }
10. }
Which code fragment, when inserted at line 7, enables the code to print true?
A) while(key==myarray[index++]){
      isFound=true;
   }
B) while(index<=4){
      if(key==myarray[index]){
          index++;
          isFound=true;
          break;
      }
   }
C) while(index++<5){
       if(key==myarray[index]){
           isFound=true;
       }
   }
D) while(index < 5){
       if(key==myarray[index]){  
           isFound=true;
           break;
       }
       index++;
   }

Answer : D
Q-056
public class Test{
    public static void main(String[] args) {
        String names[]=new String[3];
        names[0]="Mary Brown";
        names[1]="Nancy Red";
        names[2]="Jessy Orange";
        try{
            for (String n:names){
                try{
                    String pwd=n.substring(0,3)+n.substring(6,10);
                    System.out.println(pwd);
                }
                catch(StringIndexOutOfBoundsException sie){
                    System.out.println("string out of limits");
                }
            }
        }
        catch(ArrayIndexOutOfBoundsException e){
            System.out.println("array out of limits");
        }
    }
}
What is the result?
A) Marrown
   string out of limits
   JesOran
B) Marrown
   string out of limits
   array out of limits
C) Marrown
   string out of limits
D) Marrown
   NanRed
   JesOran

Answer : A
Q-057
class Caller{
    private void init(){
        System.out.println("Initialized");
    }
    public void start(){
        init();
        System.out.println("Started");
    }
}
public class Test{
    public static void main(String[] args) {
        Caller c=new Caller();
        c.start();
        c.init();
    }
}
What is the result?
A) Initialized
   Started
B) Initialized
   Started
   Initialized
C) Compilation fails.
D) An exception is thrown at runtime.

Answer : C
Q-058
class Tours{
    public static void main(String[] args){
        System.out.print("Happy Journey! "+args[1]);
    }
}
public class Traveler{
    public static void main(String[] args) {
        Tours.main(args);
    }
}
And the commands
javac Traveler.java
java Traveler Java Duke
What is the result?
A) Happy Journey! Duke
B) Happy Journey! Java
C) An exception is thrown at runtime.
D) The program fails to execute due to a runtime error.

Answer : A
Q-059
Given the code fragment
24. float var1=(12_345.01>= 123_45.00)?12_456:124_56.02f;
25. float var2=var1+1024;
26. System.out.print(var2);
What is the result?
A) 13480.0
B) 13480.02
C) Compilation fails.
D) An exception is thrown at runtime.

Answer : A
pdf文件答案是錯的
Q-060
class Dog{
    Dog(){
        try{
            throw new Exception();
        }
        catch(Exception e){}
    }
}
public class Test{
    public static void main(String[] args) {
        Dog d1=new Dog();
        Dog d2=new Dog();
        Dog d3=d2;
        //do complex stuff
    }
}
How many objects have been create when the line //do complex stuff is reached?
A) two
B) three
C) four
D)six

Answer : A

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *