OCA 808 Exams-2

      在〈OCA 808 Exams-2〉中尚無留言

Q-021

Given the following class
class Rectangle{
    private double length;
    private double height;
    private double area;
    public void setLength(double length){
        this.length = length;
    }
    public void setHeight(double height){
        this.height = height;
    }
    public void setArea(){
        area = length * height;
    }
}

Whick two changes would encapsulate this class and ensure that the area field is always equals to length * height whenever the Rectangle class is used?

A. Call the setArea method at the end of the setHeight method.
B. Call the setArea method at the beginning of the setHeight method.
C. Call the setArea method at the end of the setLength method.
D. Call the setArea method at the beginning of the setLength method.
E. Change the setArea method to private.
F. Change the area field to public.

Ans : A C
setArea()可以維持在public不用更變, 讓外部要調用或不調用都可以

Q-022

Given
class C2 {
    public void displayC2() {
        System.out.print("C2");
    }
}
interface I {
    public void displayI ();
}
class C1 extends C2 implements I {
    public void displayI() {
        System.out.print("C1");
    }
}

And given the code fragment:

C2 obj1 = new C1();
I obj2 = new C1();

C2 s=obj2;
I t=obj1;

t.displayI();
s.displayC2();

What is the result?

A) C1C1
B) Compilation fails.
C) C2C2
D) C1C2

Ans : B
obj2是 Interface I, 無法轉成 C2

Q-023

Which three are advantages of the Java exception mechanism?

A.Improves the program structure because the error handling code is separated from the normal program function.
B. Provides a set of standard exceptions that covers all the possible errors.
C. Improves the program structure because the programmer can choose where to handle exceptions.
D. Improves the program structure because exceptions must be handled in the method in which they occurred.
E. Allows the creation of new exceptions that are tailored to the particular program being created.

Answer: A,C,E
解說:
B. 例外處理並不會處理(Cover)所有的錯誤. 比如unchecked Exception 就不需強制寫try-catch
D. 例外可以使用call stack的方式, 丟給上層的方法, 所以不一定要在發生錯誤的方法中處理.
public class Test2 {
    public static void main(String[] args) {
        Pikachu p1=new Pikachu();
        try{
            p1.setArea();
        }
        catch(Exception e){
            System.out.println(e.getMessage());
        }
    }
}
class Pikachu{
    int height, width, area;
    public void setArea()throws Exception{//Call Stack
        if (width==0 || height==0)
            throw new Exception("不能為0");
        area=width*height;
    }
}

在main中, 也可以使用call stack, 如
public static void main(String args)throws Exception{}
此時會造成系統crash閃退, 所以一般程式碼不會這樣寫. 
但考試時, 就是愛考這種五四三的, 要注意這種寫法是正確的.

另外, 若在建構子發生例外, 則無法產生物件, 所得到的結果為null
public class Test2 {
    public static void main(String[] args){
        Pikachu p1=null;
        try{
            p1=new Pikachu();
        }
        catch(Exception e){
            System.out.println(e.getMessage());
        }
        if (p1==null){
            System.out.println("沒有產生皮卡丘");
        }
    }
}
class Pikachu{
    public Pikachu()throws Exception{
        throw new Exception("建構子中發生錯誤");
    }
}
結果為 :
建構子中發生錯誤
沒有產生皮卡丘

Q-024

Given the following classes : 
public class Employee {
    public int salary ;
}
public class Manager extends Employee {
    public int budget ;
}
public class Director extends Manager {
    public int stockOptions ;
}

And given the following main method:

public static void main (String[] args) {
    Employee employee = new Employee () ;
    Manager manager = new Manager () ;
    Director director = new Director () ; 
    //line n1
}

Which two options fail to compile when placed at line n1 of the main method?
A. employee.salary = 50_000;
B. director.salary = 80_000;
C. employee.budget = 200_000;
D. manager.budget = 1_000_000;
E. manager.stockOption = 500;
F. director.stockOptions = 1_000;

Ans : C E

Q-025

class Alpha{
    int ns;
    static int s;
    Alpha(int ns){
        if(s<ns){
            s=ns;
            this.ns=ns;
        }
    }
    void doPrint(){
        System.out.println("ns = "+ns+" s = "+s);
    }
}
public class TestA{
    public static void main(String[]args){
        Alpha ref1=new Alpha(50);
        Alpha ref2=new Alpha(125);
        Alpha ref3=new Alpha(100);
        ref1.doPrint();
        ref2.doPrint();
        ref3.doPrint();
    }
}
what is this result?
A 
    ns = 50 s = 50 
    ns = 125 s = 125 
    ns = 0 s = 125
B 
    ns = 50 s = 125
    ns = 125 s = 125
    ns = 100 s = 125
C 
    ns = 50 s = 125
    ns = 125 s = 125
    ns = 0 s = 125
D 
    ns = 50 s = 50
    ns = 125 s = 125
    ns = 100 s = 100

Ans : C

Q-026

4.  class X{
5.     public void printFileContent(){
6.         /* code goes here */
7.         throw new IOException();
8.     }
9.  }
10. public class Test{
11.    public static void main (String [] args ){
12.        X xobj = new X();
13.        xobj.printFileContent();
14.    }
15. }

Which two modifications should you make so that the code compiles successfully?

A) Replace line 13 with;
    try {
        xobj.printFileContent();
    }
    catch(Exception e){ }
B) Replace line 5 with public void printFileContent() throws IOException{
C) Replace line 11 with public static void main(String[] args )throws Exception{
D) Replace line 7 with throw IOException(“Exception raised”);
E) At line 14,insert throw new IOException();

Ans : A B
解說:
此題有些爭議性
正確答案是AB, 但其實若是選BC, 也是可以的. 只是main會造成runtime error, 
而題目註明是二個答案, 所以就選AB

Q-027

public static void main (String [] args ){
    int[][] arr=new int[2][4];
    arr[0]=new int[]{1,3,5,7};
    arr[1]=new int[]{1,3};
    for (int[] a:arr){
	for (int i:a){
            System.out.print(i+" ");
	}
	System.out.println("");
    }
}

what is the result?
A. Compilation fails.
B. 1 3
   followed by an ArrayIndexOutOfBoundsException
C. 1 3
   1 3 0 0
D. 1 3
   1 3
E. 1 3 5 7
   1 3

Ans : E
解說:
    public static void main(String[] args)throws Exception{
        int [][]arr=new int[][]//等長陣列
        {
            {1,3,5,7}, 
            {2,4,6,8}
        };
        int[][]s=new int[2][];//不等長
        s[0]=new int[4];
        s[1]=new int[10];

Q-028

interface Exportable{
    void export();
}
class Tool implements Exportable{
    protected void export(){ //line n1
        System.out.println("Tool::export");
    }
}
public class Test extends Tool implements Exportable{
    public void export(){ //line n2
        System.out.println("RTool::export");
    }
    public static void main(String[] args) {
        Tool aTool=new Test();
        Tool bTool=new Tool();
        callExport(aTool);
        callExport(bTool);
    }
    public static void callExport(Exportable ex){ //line n2
        ex.export();
    }
}
What is the result?
A. Compilation fails only at line n2.
B. RTool::export Tool::export
C. Tool::export Tool:export
D. Compilation fails only at line n1.
E. Compilation fails at both line n1 and line n2.

Ans : D
解說:
此題在網路上有很多答案都說是B, 其實大錯特錯了. 正確答案是 D. 
因為Line 1 在實作介面的方法, 都是pubic. 不可以為protected.

如果Line改成public , 那答案才是B

Q-029

public class Test{
    int x, y;
    public Test(int x, int y){
        initialize(x, y);
    }
    public void initialize(int x, int y){
        this.x=x*x;
        this.y=y*y;
    }
    public static void main(String[] args){
        int x=3, y=5;
        Test obj=new Test(x, y);
        System.out.println(x + " "+ y);
    }
}

what is the result?
A. 0 0
B. Compilation fails
C. 3 5
D. 9 25

Ans : C

Q-030

interface Downloadable{
    public void download();
}
interface Readable extends Downloadable{ //line n1
    public void readBook();
}
abstract class Book implements Readable{ //line n2
    public void readBook(){
        System.out.println("Read Book");
    }
}
class EBook extends Book{ //line n3
    public void readBook(){
        System.out.println("Read E-Book");
    }
}

And given the code fragment:
Book book1 = new EBook();
book1.readBook();
what is the result?
A. Read Book
B. Compilation fails at line n1
C. Compilation fails at line n2
D. Compilation fails at line n3
E. Read E-Book

Ans : D
line n3 需實作download()方法
解說
如果line n3有實作download的話, 那答案會是E
因子類別方法覆蓋父類別方法時, 父類別方法屍骨無存. 所以就算使用父類別參考, 還是執行
子類別的方法

Q-031

public class Person{
    String name;
    int age=25;
    public Person(String name){
        this(); //line n1
        setName(name);
    }
    public Person(String name, int age){
        Person(name); //line n2
        setAge(age);
    }
    //setter and getter methods go here
    public String show(){
        return name + " "+ age;
    }
    public static void main(String[] args){
        Person p1=new Person("Jeses");
        Person p2= new Person("Walter", 52);
        System.out.println(p1.show());
        System.out.println(p2.show());
    }
}

what is the result?
A. Compilation fails only at line n2.
B. Compilation fails only a line n1.
C. Jeses 5
   Walter 52
D. Compilation fails at both line n1 and line n2

Ans : D
解說
Line 1 : this()需拿掉, 因為沒有預設建構子
Line 2 : 需改為 this(name)

    public Person(){}
    public Person(String name){
        this(); //line n1
        setName(name);
    }
    public Person(String name, int age){
        this(name); //line n2
        setAge(age);
    }

Q-032

public class Triangle{
    static double area;
    int b=2, h=3;
    public static void main(String[] args){
        double p, b, h; //line n1
        if (area ==0) {
            b=3;
            h=4;
            p=0.5;
        }
        area=p * b * h; //line n2
        System.out.println("Area is  "+ area);
    }
}

what is the result?
A. Compilation fails at line n2.
B. Compilation fails at line n1.
C. Area is 6.0
D. Area is 3.0

Ans : A
b, h, p可能沒有初始化

Q-033

 3. public static void main(String[] args){
 4. 	int iVar =100;
 5. 	float fVar =100.100f;
 6. 	double dVar =123;
 7. 	iVar = fVar;
 8. 	fVar = iVar;
 9. 	dVar = fVar;
10. 	fVar = dVar;
11. 	dVar = iVar;
12. 	iVar = dVar;
13. }

Which three lines fail to compile?
A. line 12
B. line 9
C. line 10
D. line 8
E. line 11
F. line 7

Ans : A C F

Q-034

public static void main(String[] args){
    //line n1
    switch(x){
	case 1:
            System.out.println("One");
	    break;
	case 2:
	    System.out.println("Two");
	    break;
    }
}
Which three code fragments can be independently inserted at line n1 to enable the code to print One?
A. long x=1;
B. byte x=1;
C. Integer x=new Integer("1");
D. double x=1;
E. short x=1;
F. String x="1";

Ans : B C E

Q-035

class A{
    public A(){
        System.out.print("A ");
    }
}
class B extends A{
    public B(){ //line n1
        System.out.print("B ");
    }
}
class C extends B{
    public C(){  //line n2
        System.out.println("C ");
    }
    public static void main(String[] args){
        C c=new C();
    }    
}

what is the result?
A. Compilation fails at line n1 and line n2.
B. A B C
C. C
D. C B A

Ans : B
若要嚴格來說, 答案是A, 因為class C 沒有public. 但此題是考super(), 所以是B

Q-036

public class MianTest{
    public static void main(int[] args){
        System.out.println("int main " + args[0]);
    }
    public static void main(Object[] args){
        System.out.println("Object main " + args[0]);
    }
    public static void main(String[] args){
        System.out.println("String main " + args[0]);
    }
}
and commands:
javac MainTest1.java
java MainTest 1 2 3
what is the result?
A. String main 1
B. An exception is thrown at runtime.
C. int main 1
D. Compilation fails.
E. Object main 1

Ans : A

Q-037

public class Test{
    public static final int MIN=1;
    public static void main(String args[])
    {
        int x=args.length;
        System.out.println(args.length);
        if(checkLimit(x)) {     //line n1
            System.out.println("Java SE");
        }
        else{
            System.out.println("Java EE");
        }
    }
    public static boolean checkLimit (int x){
        return (x>=MIN) ? true :false;
    }
}
And given the commands:
javac Test.java
java Test 1 2 3
what is the result?
A. Compilation fails at line n1.
B. 0
   Java EE
C. 3
   Java SE
D. A NullPointerExcpetion is thrown at runtime.

Ans : C

Q-038

public static void main(String args[]){
	short s1 = 200;
	Integer s2 = 400;
	Long s3 = (long) s1 +s2;     //line n1
	String s4 = (String)(s3 * s2);//line n2
	System.out.println("Sum is " + s4);
}
what is the result?
A. Sum is 600
B. A ClassCastException is thrown at line n2.
C. Compilation fails at line n2.
D. A ClassCastException is thrown at line n1.
E. Compilation fails at line n1.

Ans : C
解說 :
Line 2 必需為 String s4=String.valueOf(s3*s2);

Q-039

public class MyException extends RuntimeException{}
public class Test{
    public static void main(String args[]){
        try{
            method1();
        }
        catch(MyException ne){
            System.out.print("A");
        }
    }
    public static void method1(){   //line n1
        try{
            throw Math.random() > 0.5 ? new MyException(): new RuntimeException();
        }
        catch(RuntimeException re){
            System.out.println("B");
        }
    }
}
what is the result?
A. B
B. Either A or B
C. A compile time error occurs at line n1.
D. AB

Ans : A

Q-040

public class Test{
    public static void main(String args[]){
        if (args[0].equals("Hello") ? false : true){
            System.out.println("Success");
        }
        else{
            System.out.println("Failure");
        }
    }
}
And given the commands:

java Test Hello
what is the result?
A. Success
B. Compilation fails.
C. An exception is thrown at runtime.
D. Failure

Ans : D
請注意, 這題有陷井.輸入Hello後, equals是 true, 但 true是返回第一個參數 fasle, 所以會跑 else區塊

發佈留言

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