OCA 808 Exams-4

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

Q-061

Given the code snippet from a compiled Java source file:
public class MyFile {
    public static void main(String[] args) {
        String arg1= args[1];
        String arg2= args[2];
        String arg3= args[3];
        System.out.println("Arg is " + arg3);
    }
}
Which command line arguments should you pass to the program to obtain the following output?
Arg is 2
A. java MyFile 1 3 2 2
B. java MyFile 1 2 2 3 4
C. java MyFile 0 1 2 3
D. java MyFile 2 2 2

Ans : A

Q-062

Given the code fragment:
public static void main(String[] args) {
    int[] arr={1, 2, 3, 4};
    int i=0;
    do{
	System.out.print(arr[i] +" ");
	i++;
    }while (i < arr.length-1);
}
What is the result?
A. 1 2 3
B. Compilation fails.
C. 1 2 3 4
D. 1 2 3 4
   followed by an ArrayIndexOutOfBoundsException

Ans : A

Q-063

Given : 
public class Test {
    int x;
    int y;
    public void doStuff(int x, int y){
        this.x=x;
        y=this.y;
    }
    public void display(){
        System.out.print(x + " " + y + " : ");
    }
    public static void main(String[] args) {
        Test m1=new Test();
        m1.x=100;
        m1.y=200;
        Test m2=new Test();
        m2.doStuff(m1.x, m1.y);
        m1.display();
        m2.display();
    }
}
What is the result?
A. 100 0 : 100 200 :
B. 100 200 : 100 0 :
C. 100 200 : 100 200 :
D. 100 0 : 100 0 :  

Ans : B

Q-064

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 = "Duke";
B. String str2 = new String(str1);
C. String str2 = str1;
D. String str2= sb1.toString();

Ans : C

Q-065

public class Test {
    public static void main(String[] args) {
        Test ts=new Test();
        System.out.print(isAvailable + " ");
        isAvailable = ts.doStuff();
        System.out.println(isAvailable);
    }
    public static boolean doStuff(){
        return !isAvailable;
    }
    static boolean isAvailable = false;
}
What is the result?
A. false true
B. true false
C. Compilation fails
D. true true
E. false false

Ans : A

Q-066

Given:
package clothing;
class Shirt{
    public static String getColor(){
        return "Green";
    }
}

Given the code fragment:
package clothing.pants;
//line n1
public class Jeans {
    public void matchShirt(){
        //line n2
        if (color.equals("Green")){
            System.out.print("Fit");
        }
    }
    public static void main(String[] args) {
        Jeans trouser = new Jeans();
        trouser.matchShirt();
    }
    public static boolean doStuff(){
        return !isAvailable;
    }
    static boolean isAvailable = false;
}
Which two sets of actions, idenpendently, enable the code fragment to print Fit?
A. At line n1 insert : import clothing;
   At line n2 insert: String color = Shirt.getColor();
B. At line n1 insert: import static clothing.Shirt.getColor;
   At line n2 insert: String color=getColor();
C. At line n1 no change required.
   At line n2 insert: String color=Shirt.getColor();
D. At line n1 insert: imort clothing.Shirt;
   At line n2 insert: String color = getColor();
E. At line n1 insert: import clothing.*;
   At line n2 insert: String color = Shirt.getColor();

Ans : E

Q-067

Given the code fragment
public class Employee{
    String name;
    boolean contract;
    double salary;
    Employee(){
        //line n1
    }
    public String toString(){
        return name + ":" + contract + ":" + salary;
    }
    public static void main(String[] args){
        Employee e=new Employee();
        //line n2
        System.out.print(e);
    }
}
Which two modifications, when made independently, enable the code to print Joe:true:100.0?
A. Replace line n2 with:
   e.name="Joe";
   e.contract = true;
   e.salary = 100;
B. Replace line n1 with:
   this.name=new String("Joe");
   this.contract = new Boolean(true);
   this.salary = new Double(100);
C. Replace line n1 with:
   this("Joe", true, 100);
D. Replace line n1 with:
   name = "Joe";
   contract = TRUE;
   salary = 100.0f;
E. Replace line n2 with:
   this.name = "Joe";
   this.contract = true;
   this.salary =100;

Ans : A B

Q-068

Given:
Acc.java:
package p1;
public class Acc{
   int p;
   private int q;
   protected int r;
   public int s;
}

Test.java:
package p2;
import p1.Acc;
public class Test extends Acc{
   public static void main(String[] args{
       Acc obj=new Test();
   }
}
Which statement is true;
A. p, r, and s are accessible via obj.
B. Both p and s are accessible via obj.
C. Both r and s are accessible via obj.
D. Only s is accessible via obj.

Ans : D

Q-069

public class Test {
    public static void main(String[] args) {
        boolean a=new Boolean(Boolean.valueOf(args[0]));
        boolean b=new Boolean(args[1]);
        System.out.println(a + " " + b);
    }
}
And given the commands:
javac Test.java
java Test TRUE null
What is the result?
A. false false
B. TRUE null
C. true true
D. A ClassCastException is thrown at runtime.
E. true false

Ans : E

Q-070

Which statement is true about the switch statement?
A. The break statement, at the end of each case block, is mandatory.
B. Its expression must evaluate to a single value.
C. Its case label literals can be changed at runtime.
D. It must contain the default section.

Ans : B
mandatory[ˋmændə͵torɪ] : 強制的

Q-071

04 class X{
05     public void printFileContent(){
06         /* code goes here */
07         throw new IOException();
08     }
09 }
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 5 with public void printFileContent() throws IOException{
B. Replace line 11 with public static void main(String[] args) throws Exception {
C. At line 14, insert throw new IOException();
D. Replace line 7 with throw IOException("Excpetion raised");
E. Replace line 13 with
   try{
       xobj.printFileContent();
   }
   catch(Exception e){}
   catch(IOException e){}

Ans : A B

Q-072

Given:
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 n1.
B. Compilation fails at line n2.
C. Area is 6.0
D. Area is 3.0

Ans : B
解說
double p, b, h;
if(area==0){ 
    b=3; 
    h=4; 
    p=0.5; 
}
會造成b, h, p可能沒有初始化的錯誤

Q-073

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. 3 5
B. 0 0
C. Compilation fails.
D. 9 25

Ans : A

Q-074

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 Test {
    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 the result?

A. ns = 50 s= 50
   ns = 125 s= 125
   ns = 100 s= 100
B. ns = 50 s= 125
   ns = 125 s= 125
   ns = 0 s= 125
C. ns = 50 s= 125
   ns = 125 s= 125
   ns = 1000 s= 125
D. ns = 50 s= 50
   ns = 125 s= 125
   ns = 0 s= 125

Ans : B

Q-075

Which two class definitions fail to compile?
A. class A4{
       protected static final int i;
       private void doStuff(){}
   }
B. abstract class A3{
       private static int i;
       public void doStuff(){}
       public A3(){}
   }
C. public class A2{
       private static int i;
       private A2(){}
   }
D. final class A1{
       public A1(){}
   }
E. final abstract class A5{
       protected static int i;
       void doStuff(){}
       abstract void doIt();
   }
Ans : A E
解說:
A. protected final static int i必需要初始化
E. abstract 跟 final不可以同時出現

Q-076

public class Test {
    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. A ClassCastException is thrown at line n1.
B. Compilation fails at line n1.
C. Sum is 600
D. Compilation fails at line n2.
E. A ClassCastException is thrown at line n2.

Ans : D
解說
數字轉字串, 必需使用String.valueOf(數字格式)
int a=10;
String s=String.valueOf(a);

Q-077

public class Jump {
    static String args[]={"Lazy", "Lion", "is", "always"};
    public static void main(String[] args){
        System.out.println(args[1] + " " + args[2] + " " + args[3] + " jumping");
    }
}
And the commands:
javac Jump.java
java Jump Crazy Elephant is always

What is the result?
A. Lazy Lion is jumping
B. Lion is always jumping
C. Crazy elephant is jumping
D. Elephant is always jumping
E.Compilation fails

Ans : D

Q-078

Which tree are valid types for switch?

A. int
B. float
C. double
D. Integer
E. String
F. Float
Ans : A D E
switch裏的條件, 只可以為 byte, short, int, String共四個
Byte, Short, Integer 會自動解封裝成 byte, short, integer, 所以也可以

 

Q-079

class Circle{
    double radius;
    public double area;
    public Circle(double r){radius=r;}
    public double getRadius(){return radius;}
    public void setRadius(double r){radius =r;}
    public double getArea(){return /* ??? */;}
}
public class Test {
    public static void main(String[] args){
        Circle c1=new Circle(17.4);
        c1.area=Math.PI * c1.getRadius() * c1.getRadius();
    }
}

This class is poorly encapsulated. You need to change the circle class to compute and 
return the area instead. 
What three modifications are necessary to ensure that the class is being properly encapsulated?

A. Change the access modifier of the setradius() method to private
B. Change the getArea() method public double getArea(){return area;}
C. When the radius is set in the Circle constructor and the setRadius() method, 
   recomputed the area and store it into the area field.
D. Change the getRadius() method: 
   public double getRadius(){
       area = Math.PI * radius * radius;
       return radius;
   }

Ans : B C D
其實D並不需要, 但題目要求要三個答案, 所以只能捨棄掉絕對是錯誤的A

Q-080

Which two statements are true for a two-dimensional array?

A. It cannot contain elements of different types.
B. Using a row by column convention, each row of a two-dimensional array must be of the same size.
C. At declaration time, the number of elements of the array in each dimension must be specified.
D. All methods of the class Object may be invoked on the two-dimensional array.

Ans : A D
A : 陣列只能包含一種型態, 所以是對的
D : Object類別裏的所有方法, 都可以被二維陣列使用, 因為二維陣列是繼承Object的子類別

底下網站, 有更多的考古題

https://blog.csdn.net/pxg943055021/article/details/80068512

發佈留言

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