OCA 808 Exams-3

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

Q-041

public static void main(String[] args) {
    String[] arr={"Hi", "How", "Are", "Yo"};
    List arrList=new ArrayList <> (Arrays.asList(arr));
    if(arrList.removeIf((String s) -> {return s.length() <= 2;})){
        System.out.println(s + " removed");
    }
}
What is the result?
A. The program compiles, but it prints nothins.
B. Compilation failes.
C. An UnsupportedOperationException is thrown at runtime.
D. Hi removed

Ans : B
String s 為Lambda內的區域變數, 所以無法被Lambda之外使用
Arrays.asList() : 將陣列轉為 List. 
但請注意, 陣列裏不可以為基本資料型態, 如 int[] arr={1, 2, 3}
Integer[] arr={1, 2, 3}才可以放入 asList()裏

Q-042

public static void main(String[] args) {
    int ii=0;
    int jj=7;
    for (ii=0;ii<jj-1;ii=ii+2){
        System.out.print(ii + " ");
    }
}
What is the result?
A. Compilation fails.
B. 0 2 4 6
C. 2 4
D. 0 2 4

Ans : D

Q-043

The following grid shows the state of a 2D array
ocp808_1
This grid is created with the following code:
 char[][] grid=new char[3][3];
 grid[1][1]='X';
 grid[0][0]='O';
 grid[2][1]='X';
 grid[0][1]='O';
 grid[2][2]='X';
 grid[1][2]='O';
 //line n1
Which line of code, when inserted in place of //line n1, adds an x into the grid so that the grid contains three consecutive X's?
A.  grid[2][0]='X';
B.  grid[1][3]='X';
C.  grid[0][2]='X';
D.  grid[1][2]='X';
E.  grid[3][1]='X';

Ans : A

Q-044

public static void main(String[] args) {
    String ta="A ";
    ta=ta.concat("B ");
    String tb="C ";
    ta=ta.concat(tb);
    ta.replace('C', 'D');
    ta=ta.concat(tb);
    System.out.println(ta);
}
What is the result?
A. A C D
B. A B C C
C. A B D
D. A B C D 
E. A B D C

Ans : B

Q-045

Given : 
class Test{
    public static int stVar = 100;
    public int var = 200;
    public String toString(){
        return var + ":" + stVar;
    }
}
And given the code fragment:
Test t1=new Test();
t1.var = 300;
System.out.println(t1);
Test t2=new Test();
t2.stVar = 300;
System.out.println(t2);

What is the result?
A. 300:100
   200:300
B. 200:300
   200:300
C. 300:300
   200:300
D. 300:0
   0:300

Ans : A

Q-046

public static void main(String[] args) {
	//line n1
	array[0]=10;
	array[1]=20;
	System.out.print(array[0] + ":" + array[1]);
}
Which code fragment, when inserted at line n1, enables the code to print 10:20?
A. int[] array=new int[2];
B. int array = new int[2];
C. int array[2];
D. int [] array;
   array = int[2];

Ans : A

Q-047

class Vehicle{
    String type;
    int maxSpeed;
    Vehicle(String tyep, int maxSpeed){
        this.type=type;
        this.maxSpeed=maxSpeed;
    }
}
class Car extends Vehicle{
    String trans;
    Car(String trans){      //line n1
        this.trans=trans;
    }
    Car(String type, int maxSpeed, String trans){
        super(type, maxSpeed);
        this(trans);        //line n2
    }
}
And given the code fragment:
    Car c1=new Car("Auto");
    Car c2=new Car("4W", 150, "Manual");
    System.out.println(c1.type + " " +c1.maxSpeed + " " + c1.trans);
    System.out.println(c2.type + " " +c2.maxSpeed + " " + c2.trans);
What is the result?
A. null 0 Auto
   4W 150 Manual
B. Compilation fails at both line n1 and line n2
C. Compilation fails only at line n2
D. 4W 100 Auto
   4W 150 Manual
E. Compilation fails only at line n1

Ans : B
line n1下一行需寫入 super("",100)
line n2需刪除, super()及this()都必需放在建構子第一行, 所以只能出現一種.

Q-048

You are asked to create a method that accepts an array of integers and returns the highest value from that array.
Given the code fragment:
class Test {
    public static void main(String[] args) {
        int numbers[] ={12,13,42,32,15,156,23,51,12};
        int max=findMax(numbers);
    }
    /* line n1*/{
        int max=0;
        /* code goes here*/
        return max;
    }
}
What method signature do you use at line n1?
A. public int findMax(int[] numbers)
B. final int findMax(int[])
C. static int findMax(int[] numbers)
D. static int[] findMax(int max) 

Ans : C

Q-049

int n[][] = {{1, 3}, {2, 4}};
for (int i=n.length-1;i>=0;i--){
	for (int y: n[i]){
		System.out.print(y);
	}
}
What is the result?
A. 4231
B. 3142
C. 2413
D. 1324

Ans : C

Q-050

13.  List colors=new ArrayList();
14.  colors.add("green");
15.  colors.add("red");
16.  colors.add("blue");
17.  colors.add("yellow");
18.  colors.remove(2);
19.  colors.add(3, "cyan");
20.  System.out.print(colors);
What is the result?
A. An IndexOutOfBoundsException is thrown at runtime.
B. [green, red, yellow, cyan]
C. [green, red, cyan, yellow]
D. [green, blue, yellow, cyan] 

Ans : B

Q-051

class Animal{
    String type = "Canine";
    int maxSpeed=60;
    Animal(){}
    Animal(String type, int maxSpeed){
        this.type=type;
        this.maxSpeed=maxSpeed;
    }
}
class WildAnimal extends Animal{
    String bounds;
    WildAnimal(String bounds){
        //line n1
    }
    WildAnimal(String type, int maxSpeed, String bounds){
        //line n2
    }
}
And given the code fragment:
7.  WildAnimal wolf=new WildAnimal("Long");
8.  WildAnimal tiger=new WildAnimal("Feline", 80, "Short");
9.  System.out.println(wolf.type + " " + wolf.maxSpeed + " " + wolf.bounds);
10. System.out.println(tiger.type + " " + tiger.maxSpeed + " " + tiger.bounds);

Which two modification enable the code to print the following output?
Canine 60 Long
Feline 80 Short

A. Replace line n1 with:
   this("Canine", 60);
B. Replace line n2 with:
   super(type, maxSpeed);
   this(bounds);
C. Replace line n1 with:
   super();
   this.bounds = bounds;
D. Replace line n1 with:
   this.bounds = bounds;
   super();
E. Replace line n2 with:
   super(type, maxSpeed);
   this.bounds = bounds;

Ans : C E

Q-052

public static void main(String[] args) {
    LocalDate date=LocalDate.of(2012, 01, 32);
    date.plusDays(10);
    System.out.println(date);
}
What is the result?
A. Compilation fails.
B. 2020-02-10
C. A DateTimeException is thrown at runtime.
D. 2012-02-11 

Ans : C
日期 2012,01 32 超過31
另外, 要寫成 date=date.plusDays(10);

Q-053

public class Test {
    public static void main(String[] args) {
        String str1="Java";
        String[] str2={"J","a","v","a"};
        String str3="";
        for (String str:str2){
            str3=str3+str;
        }
        boolean b1=(str1==str3);
        boolean b2=(str1.equals(str3));
        System.out.print(b1 + ", "+b2);
    }
}
What is the result?
A. true, true
B. true, false
C. false, true
D. false, false

Ans : C

Q-054

Given the code fragments:
class Student{
    String name;
    int age;
}
And,
4.  public class Test {
5.      public static void main(String[] args) {
6.          Student s1=new Student();
7.          Student s2=new Student();
8.          Student s3=new Student();
9.          s1=s3;
10.         s3=s2;
11.         s2=null;
12.    }
13. }
Which statement is true?
A. After line 11, two objects are eligible for garbage collection.
B. After line 11, none of the objects are eligible for garbage collection.
C. After line 11, three objects are eligible for garbage collection.
D. After line 11, one object is eligible for garbage collection.

Ans : D

Q-055

Given the following segment of code:
ArrayList<Vehicle> myList = new ArrayList<>();
myList.add(new MotorCycle());
Which two statements, if either were true, would make the code compile?
A. MotorCycle is a superclass of Vehicle.
B. Vehicle and MotoryCycle both implement the Transportation interface.
C. Vehicle is an interface that is implemented by the Motorcycle class.
D. Vehicle is a superclass of MotorCycle.
E. Vehicle and MotorCycle both extend the Transportation superclass.
F. MotorCycle is an interface that implements the Vehicle class.

Ans : C D

Q-056

class Student{
    String name;
    public Student(String name){
        this.name=name;
    }
}
public class Test {
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[1]=new Student("Richard");
        students[2]=new Student("Donald");
        for (Student s: students){
            System.out.println(""+s.name);
        }
    }
}
What is the result?
A. A NullPointerException is thrown at runtime.
B. Compilation fails.
C. Richard
   Donald
D. null
   Richard
   Donald
E. An ArrayIndexOutOfBoundsException is thrown at runtime.

Ans : A
student[0] 並沒有產生Student物件

Q-057

Given the code fragment:
public class Test {
    void readCard(int cardNo)throws Exception{
        System.out.println("Reading Card");
    }
    void checkCard(int cardNo)throws RuntimeException { //line n1
        System.out.println("Checking Card");
    }
    public static void main(String[] args) {
        Test ex=new Test();
        int cardNo=1234;
        ex.checkCard(cardNo);   //line n2
        ex.readCard(cardNo);    //line n3
    }
}

What is the result?
A. Compilation fails only at line n2.
B. Reading Card
   Checking Card
C. Compilation fails only at line n1
D. Compilation fails at both line n2 and line n3.
E. Compilation fail only at line n3.

Ans : E

Q-058

int nums1[]=new int[3];
int nums2[]={1, 2, 3, 4, 5};
nums1=nums2;
for (int x:nums1){
    System.out.print(x+":");
}
What is the result?
A. Compilation fails.
B. 1:2:3:4:5:
C. An ArrayOutOfBoundsException is thrown at runtime.
D. 1:2:3:

Ans : B

Q-059

public class Test {
    public static void main(String[] args) {
        int x=1;
        int y=0;
        if(x++ > ++y){
            System.out.print("Hello ");
        }
        else{
            System.out.print("Welcome ");
        }
        System.out.print("log " + x + ":" + y);
    }
}
What is the result?
A. Welcome Log 1:0
B. Hello Log 2:1
C. Hello Log 1:0
D. Welcome Log 2:1

Ans : D

Q-060

public class Test {
    int a1;
    public static void doProduct(int a){
        a=a*a;
    }
    public static void doString(StringBuilder s){
        s.append(" " + s);
    }
    public static void main(String[] args) {
        Test item=new Test();
        item.a1=11;
        StringBuilder sb=new StringBuilder("Hello");
        Integer i=10;
        doProduct(i);
        doString(sb);
        doProduct(item.a1);
        System.out.println(i + " " + sb +" " + item.a1);
    }
}
What is the result?
A. 10 Hello Hello 121
B. 100 Hello 121
C. 10 Hello Hello 11
D. 100 Hello Hello 121
E. 10 Hello 11

Ans : D

發佈留言

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