OCA 808 Exams-1

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

Q-001

Given the code fragment
public class Test {
    static int count=0;
    int i=0;
    public void changeCount(){
        while(i<5){
            i++;
            count++;
        }
    }
    public static void main(String[] args) {
        Test check1=new Test();
        Test check2=new Test();
        check1.changeCount();
        check2.changeCount();
        System.out.println(check1.count+":"+check2.count);
    }
}
what is the result?
A. 10 : 10
B. 5:5
C. 5 : 10
D. Compilation fails

Ans : A
解說 :
i 為物件變數, check1的i, 與check2 的i 是不一樣的變數
count 為類別變數, check1的count 與check2的count是同一個變數
check1.count是錯誤的, 應該寫為 Test.count. 
雖Java可以使用check1.count, 但c#是完全禁止的

底下的程式碼中, t1.sleep(2000), 結果是main主執行緒去睡覺, 而不是t1去睡覺, 
所以為了防止誤會, 藍色的地方需要改成 Thread.sleep(2000);

    public static void main(String[] args) {
        Thread t1=new Thread(new Job());
        t1.start();
        try {
            Thread.sleep(2000);
            t1.sleep(2000);
            
        } catch (InterruptedException ex) {}
        System.out.println("主執行死掉了");
    }

Q-002

public class Case {
    public static void main(String[] args) {
        String product = "Pen";
        product.toLowerCase();
        product.concat(" BOX".toLowerCase());
        System.out.println(product.substring(4,6));
    }
}
What is the result?
A. box
B. nbo
C. bo
D. nb
E. An exception is thrown at runtime

Ans. E
解說:
product.concat(" BOX".toLowerCase()); product的結果還是 "Pen"
但如果是 product=product.concat(" BOX".toLowerCase()), 結果是 "bo"

Q-003

public static void main(String[] args) {
    String[] arr={"A", "B", "C", "D"};
    for (int i=0;i<arr.length;i++){
	System.out.print(arr[i]+" ");
	if(arr[i].equals("C")){
            continue;
 	}
	System.out.println("Work done");
	break;
    }
}
What is the result?
A) A B C D Work done
B) A B C Work done
C) A Work done
D) Complation fails.

Ans. C
解說:
i=0時, 列印 A, 然後不會continue, 再印work done, 然後break整個中段

Q-004

public static void main(String[] args) {
    int numbers[];
    numbers= new int[2];
    numbers[0] = 10;
    numbers[1] = 20;
    numbers=new int[4];
    numbers[2] = 30;
    numbers[3] = 40;
    for (int x: numbers){
	System.out.print(" "+x);
    }
}
What is the result?
A) 10 20 30 40
B) An exception is thrown at runtime.
C) Compilation fails.
D) 0 0 30 40

Ans : D

Q-005

Which three statements describe the object-oriented features of the Java language?
A) A main method must be declared in every class.
B) Object can share behaviors with other objects.
C) Objects cannot be reused.
D) A subclass can inherit from a superclass.
E) A package must contain more than one class.
F) Object is the root class of all other objects.

Ans : B D F

Q-006

public static void main(String[] args) {
	String date=LocalDate.parse("2014/05/04").format(DateTimeFormatter.ISO_DATE_TIME);
	System.out.println(date);
}
What is the result?
A) May 04, 2014T00:00:00.000
B) 2014-05-04T00:00:00.000
C) 5/4/14T00:00:00.000
D) An exception is thrown at runtime.

Ans : D
解說 : 
Date d=new Date();可取得目前的時間系統, 
印出d.toString()為 Wed Aug 12 12:13:24 CST 2020
但若要列印出人類常用的格式, 可使用SimpleDateFormat, 如下
String str=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());
System.out.println(str);

到了Java8開始, 建議不要再使用Date了, 而要改用LocalDate. 因為LocalDate 是thread-safe.
LocalDate沒有時區及時間資訊, 僅有日期資訊. 使用ISO-8601系統, 格式為 yyyy-mm-dd.
請注意, 是 "-", 而不是 "/". 使用 "/" 會發生runtime error
所以正確要改為 LocalDate date=LocalDate.parse("2014-05-04");

LocalDate.format()是要將LocalDate物件轉成字串, 轉換格式依()的參數而定.
因為LocalDate沒有時間格式, 所以不能使用DateTimeFormatter.ISO_DATE_TIME, 
只能使用 DateTimeFormatter.ISO_DATE
所以若改成String date=LocalDate.parse("2014-05-04").format(DateTimeFormatter.ISO_DATE);
則輸出結果為:2014-05-04

Q-007

public static void main(String[] args) {
    int i = 10;
    int j = 20;
    int k = j += i/5;
    System.out.print(i + " : " + j + " : " + k);
}
What is the result?
A) 10 : 22 : 22
B) 10 : 22 : 20
C) 10 : 22 : 6
D) 10 : 30 : 6

Ans : A

Q-008

String shirts [][] =new String[2][2];
shirts[0][0] = "red";
shirts[0][1] = "blue";
shirts[1][0] = "small";
shirts[1][1] = "medium";
Which code fragment prints red:blue:small:medium:
A) 
    for (int index=0 ; index<2 ;){
        for (int idx=0 ; idx<2 ;){
            System.out.print(shirts[index][idx] + ":");
            idx++;
        }
        index++;
    }
B) 
    for (int index=0 ; index<2 ; ++index){
        for (int idx=0 ; idx<2 ; ++idx){
            System.out.print(shirts[index][idx] + ":");
        }
    }
C) 
    for (String c : colors){
        for (String s: size){
            System.out.println(s + ":");
        }
    }
D) 
    for (int index=1 ; index<2 ; index++){
        for (int idx=1 ; idx<2 ; idx++){
            System.out.print(shirts[index][idx] + ":");
        }
    }
Ans : A B
解說:
for (;;){} 為一無窮迴圈

Q-009

Given the following code for a Planet object:

public class Planet{
    public String name;
    public int moons;
    public Planet(String name, int moons){
        this.name = name;
        this.moons = moons;
    }
}

And the following main method:
public static void main(String[] args) {
    Planet[] planets = {
        new Planet("Mercury", 0),
        new Planet("Venus", 0),
        new Planet("Earth", 1), 
        new Planet("Mars", 2)
    };
    System.out.println(planets);
    System.out.println(planets[2]);
    System.out.println(planets[2].moons);
}
What is the output?
A) 
    [LPlanets.Planet;@15db9742
    Earth
    1
B)
    [LPlanets.Planet;@15db9742
    Venus
    0
C)
    [LPlanets.Planet;@15db9742
    Planets.Planet@6d06d69c
    [LPlanets.Moon;@15db97421
D) 
    [LPlanets.Planet;@15db9742
    Planets.Planet@6d06d69c
    1
Ans : D

Q-010

public static void main(String[] args) {
    String s = " Java Duke ";
    int len = s.trim().length();
    System.out.print(len);
}
What is the result?
A) 9
B) 8
C) 10
D) 11
E) Compilation fails.

Ans : A
解說:
trim()只會刪除字串前後的空格, 中間的空格不理會

Q-011

Given the code fragment:
int wd = 0;
String days[] = {"sun", "mon", "wed", "sat"};
for (String s:days){
    switch(s){
        case "sat":
        case "sun":
            wd -= 1;
            break;
        case "mon":
            wd++;
        case "wed": 
            wd +=2;
    }
}
System.out.println(wd);
What is the result?
A) 1
B) 3 
C) -1
D) 4
E) Compilation fails.
Ans : B
wd -=1  如同wd = wd - 1;, 右邊的值放入左邊

Q-012

Given the code fragment
public static void main(String[] args) {
    ArrayList myList = new ArrayList();
    String[] myArray;
    try{
        while (true){
            myList.add("My String");
        }
    }
    catch(RuntimeException re){
        System.out.println("Caught a RuntimeException");
    }
    catch(Exception e){
        System.out.println("Caught an Exception");
    }
    System.out.println("Ready to use");
}
What is the result?
A) Execution terminates in the first catch statement, 
   and caught a RuntimeException is printed to the console.
B) Execution terminates in the second catch statement, 
   and caught an Exception is printed to the console.
C) A runtime error is thrown in the thread “main”.
D) Execution completes normally, 
   and Ready to use is printed to the console.
E) The code fails to compile because a throws keyword is required.

Ans : C
此題會進入無窮迴圈, 導致記憶体不足, 最後產生 Exception in thread “main” java.lang.OutOfMemoryError: Java heap space

Q-013

Given the following array:
int[] intArr = {8, 16, 32, 64, 128};
Which two code fragments, Independently, print each element in this array?
A) 
    for (int i=0; i<intArr.length;i++){
        System.out.print(i + " ");
    }
B) 
    for (int i :intArr){
        System.out.print(intArr[i] + " ");
    }
C) 
    for (int i ; i < intArr.length ; i++){
        System.out.print(intArr[i] + " ");
    }
D) 
    for (int i=0 ; i < intArr.length ; i++){
        System.out.print(intArr[i] + " ");
    }
E) 
    for (int i=0 : intArr){
        System.out.print(intArr[i] + " ");
        i++;
    }
F)
    for (int i : intArr){
        System.out.print(i + " ");
    }
Ans : D F

Q-014

Given the code fragment : 
float var1 = (12_345.01 >= 123_45.00)? 12_456 : 124_56.02f;
float var2 = var1 + 1024;
System.out.println(var2);

What is the result?
A) 13480.0
B) Compilation fails.
C) An exception is thrown at runtime.
D) 13480.02

Ans : A

Q-015

Given the code fragment:
public static void main(String[] args) {
    StringBuilder sb=new StringBuilder(5);
    String s="";
    if(sb.equals(s)){
        System.out.println("Match 1");
    }
    else if (sb.toString().equals(s.toString())){
        System.out.println("Match 2");
    }
    else{
        System.out.println("No Match");
    }
}
What is the result?
A) No Match
B) A NullPointerException is thrown at runtime.
C) Match 2
D) Match 1

Ans : C

Q-016

Which two class definitions faile to compile?
A) 
    public class A2{
        private static int i;
        private A2(){}
    }
B) 
    abstract class A3{
        private static int i;
        public void doStuff(){}
        public A3(){}
    }
C) 
    class A4{
        protected static final int i;
        private void doStuff(){}
    }
D)
    final class A1{
        public A1(){}
    }
E)
    final abstract class A5{
        protected static int i;
        void doStuff(){}
        abstract void doIt();
    }
Ans : C E
解說:
final static int count 一定要有初始值
final int price也一定要有初始值, 亦可於建構子內, 且只能在建構子內給值, 並只能設定一次

class Toy{
    final int price;
    final static int count=10;
    public Toy(){
        int a=100;     
        price=10;
    }
}

Q-017

Given the code fragment : 
abstract class Toy{
    int price;
    //line n1
}
Which three code fragments are valid at line n1?
A) 
    public abstract Toy getToy(){
        return new Toy();
    }
B) 
    public void printToy();
C) 
    public int calculatePrice(){
        return price;
    }
D)
    public static void insertToy(){
        /* code goes here */
    }
E)
    public abstract int computeDiscount();
Ans : C D E
解說 :
抽像類別裏的抽像方法, 一定要加abstract
abstract class Pokemon{
    abstract public void setLevel();
}

介面裏的抽像方法, 不一定要加abstract及public, 因系統會自動加入public abstract
interface Water{
    void setSwinning();
}

Q-018

Which statement is true about Java byte code?

A. It can run on any platform.
B. It can run on any platform only if it was compiled for that platform.
C. It can run on any platform that has the Java Runtime Environment.
D. It can run on any platform that has a Java compiler.
E. It can run on any platform only if that platform has both the Java Runtime Environment and a Java compiler.

Ans : C

Q-019

Given the code fragment:
public static void main(String[] args){
    double discount = 0;
    int qty = Integer.parseInt(args[0]);
    // line n1;
}

And given the requirements:

If the value of the qty variable is greater than or equal to 90, discount = 0.5
If the value of the qty variable is between 80 and 90, discount = 0.2

Which two code fragments can be independently placed at line n1 to meet the requirements?
What is the result?
A) 
    if (qty >= 90) {discount = 0.5; }
    if (qty > 80 && qty < 90) {discount = 0.2; }
B) 
    discount = (qty >= 90) ? 0.5 : 0;
    discount = (qty > 80) ? 0.2 : 0;
C) 
    discount = (qty >= 90) ? 0.5 : (qty > 80) ? 0.2 : 0;
D) 
    if (qty > 80 && qty < 90) {
        discount = 0.2;
    } else {
        discount = 0;
   }
   if (qty >= 90) {
        discount = 0.5;
   } else {
       discount = 0;
   }
E)
    discount = (qty > 80) ? 0.2 : (qty >= 90) ? 0.5 : 0;

Ans : A C

Q-020

class Vehicle {
    int x;
    Vehicle() {
        this(10); // line n1
    }
    Vehicle(int x) {
        this.x = x;
    }
}
class Car extends Vehicle {
    int y;
    Car() {
        super();
        this(20); // line n2
    }
    Car(int y) {
        this.y = y;
    }
    public String toString() {
        return super.x + ":" + this.y;
    }
}
And given the code fragment:
    Vehicle y=new Car();
    System.out.println(y);
What is the result?
A) 0:20
B) 10:20
C) Compilation fails at line n1
D. Compilation fails at line n2

Ans : D
解說
super及this都必需放在建構子的第一行, 所以二個不能同時出現

發佈留言

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