OCP 804 Exams -09

      在〈OCP 804 Exams -09〉中尚無留言
Q161
Given the interface
interface Flowable{
   void flow();
}
Which code snippet successfully prints "Flowless"?
A. new interface Flowable(){
      public void flow(){
         System.out.println(Flowless");
      }
   }.flow();
B. new Flowable(){
      public void flow(){
         System.out.println("Flowless");
      }
   }.flow();
C. new Object() extends Flowable{
      public void flow(){
         System.out.println("Flowless");
      }
   }.flow();
D. new Object() implements Flowable{
      public void flow(){
         System.out.println("Flowless");
      }
   }.flow();


Answer : B
Q162
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegEx02{
   public static void main(String[] args){
      String str="<h1>This is a header</h1>"; 
      Pattern pattern=Pattern.compile("h1"); 
      Matcher matcher=pattern.matcher(str); 
      if(matcher.find()){ 
         str=matcher.replaceFirst("h2"); 
         str=matcher.replaceFirst("h2"); 
         System.out.println(str); 
      } 
   } 
} 
What is the result? 
A. <h1>This is a header</h1>
B. <h1>This is a header</h2>
C. <h2>This is a header</h1>
D. <h2>This is a header</h2>
E. Compilation fails


Answer : C
Q163
Given:
public class Test{
   public static void main(String[] args) throws Excepton{
      Path file=Paths.get("my.txt");
      //insert the code here
      System.out.print(attr.getOwner());
   }
}
Which statement, inserted at line 4, enables the code to compile?
A. BasicFileAttributes attr=Files.readAttributes(file, BasicFileAttributes.class);
B. DosFileAttributes attr=Files.readAttributes(file, DosFileAttributes.class);
C. FileStore attr=Files.getFileStore(file);
D. AclFileAttributeView attr=Files.getFileAttributeView(file, AclFileAttributeView.class);


Answer : D
Q164
Given:
01 Number var1=1;
02 Number var2=2.2;
03 Number var3=var1+var2;
04 if(var3 instanceof Number){System.out.print("Number ");}
05 if(var3 instanceof Integer){System.out.print("Integer ");}
06 if(var3 instanceof Double){System.out.print("Double ");}
What is the result?
A. Number
B. Number Integer
C. Number Double
D. Number Integer Double
E. Compilation fails due to an error on the line 1
F. Compilation fails due to an error on the line 3


Answer : F
Number 不能用 + , 需var3=var1.intValue() + var2.doubleVaule();
Integer, Long, Double卻可以用 + 的喔
Q165
Given:
package c1;
public class Alphatest{
   protected int failed = 10;
   int passed =50;
}
And
package c1.sub;
import c1.Alphatest;
public class Betatest extends Alphatest{
   public void report(){
      Alphatest alpha=new Alphatest();
      int totla=0;
      //add line here
   }
}
Which line, inserted in the report() method, compiles?
A. total += failed;
B. total += passed;
C. total += alpha.failed;
D. total += alpha.passed;


Answer : A
Q166
Given:
import java.io.Serializable;
import java.util.Data;
class Hostelite implements Serializable{
   int roomNo;
}
class Person extends Hostelite implements Serializable{
   String name;
   Date dob;
   transient String address;
}
public class Student extends Person implements Serializable{
   String regNo;
   Hostelite host=new Hostelite();
}
Which of the fields is preserved when an object of the Student class is serialized?
A. only regNo
B. only regNo and roomNo
C. only regNo, roomNo, name, and dob
D. only regNo, roomNo, and name
E. Objects of type Student cannot be serialized


Answer : C
Q167
Given:
abstract class BasePhone{
   private void placeCall(String number){
      System.out.println("Base call: " + number);
   }
}
class SmartPhone extends BasePhone{
   public void placeCall(String number){
      System.out.println("Smart call: " + number);
   }
}
public class TestPhone{
   public static void main(String[] args){
      SmartPhone smart;
      smart=(SmartPhone)new BasePhone();
      smart.placeCall("12305550123");
   }
}
What is the result?
A. Base call : 12305550123
B. Smart call : 1230550123
C. An exception occurs at runtime
D. A compilation error occurs in BasePhone
E. A compilation error occurs in SmartPhone
F. A compilation error occurs in TestPhone 


Answer : F
Q168
Given:
01 public class Murash{
02    private int strict=2;
03    public void show(){
04       protected int strict=3;
05       System.out.print(strict);
06    }
07    public static void main(String[] args){
08       new Murash().show();
09    }
10 }
What is the result?
A. 2
B. 3
C. RuntimeException is thrown at line 4
D. RuntimeException is thrown at line 8
E. Compilation fail


Answer : E
區域變數不能加修飾子
Q169
Given : 
public class Needle extends Thread{
   public static void main(String[] args){
      Thread n1=new Needles();
      Thread n2=new Needles();
      n2.start();
      n1.start();
   }
   void run(){
      System.out.println(Thread.currentThread().getName() + " ");
      System.out.println(Thread.currentThread().getName() + " ");
   }
}
Which is true?
A. Compilation fails
B. The code runs without output
C. The output could be : Thread-7 Thread-7 Thread-7 Thread-7
D. The output could be : Thread-4 Thread-7 Thread-7 Thread-4
E. The output could be : Thread-4 Thread-7 Thread-7 Thread-3


Answer : A
public void run(), 少了public會編譯錯誤
Q170
Given the classes:
01 class Bike{}
02 class RoadBike extends Bike{}
03 class RacingBike extends Bike{}
04 public class BikeShop{
05    static void testDriver(Object o){
06       Bike b=(Bike)o;
07       if(b instanceof RoadBike) System.out.println("RoadBike ");
08       if(b instanceof RacingBike) System.out.println("RacingBike ");
09    }
10    public static void main(String[] args){
11       testDriver(new Bike());
12       testDriver(new RoadBike());
13       testDriver("new RacingBike()");
14    }
15 }
What is the result?
A. Runtime exception
B. RoadBike
C. RoadBike followed by a runtime exception
D. RoadBike RacingBike followed by a runtime exception
E. RoadBike RacingBike RoadBike RacingBike followed by a runtime exception


Answer : C
第13行testDriver("new RacingBike()") 是字串, 所以第6行轉不過去
Q171
Given these classes in independent files:
public class ShopItem{
   public float price;
   public int quantity;
}
public class ShoppingCart{
   public ShopItem[] items;
}
public class Order{
   public float calculateTotal(ShoppingCart cart, float salesTax){
      float total=0;
      for(ShopItem item : cart.items){
         total+=item.price * item.quantity;
      }
      total += total* salesTax;
      return total;
   }
}
This is a example of  ?
A. Strong encapsulation
B. Tight coupling
C. Polymorphism
D. Inheritance
E. Anstraction


Answer : B
緊耦合, ShopItem一修改, Order也要跟著改, 相依性太高
Q172
Which code fragment correctly appends "Java 7" to the end of the file "/tmp/msg.txt"?
A. FileWriter w=new FileWriter("/tmp/msg/txt");
   w.append("Java 7");
   w.close();
B. FileWriter w=new FileWriter("/tmp/msg/txt", true);
   w.append("Java 7");
   w.close();
C. FileWriter w=new FileWriter("/tmp/msg/txt", FileWriter.MODE_APPEND);
   w.append("Java 7");
   w.close();
D. FileWriter w=new FileWriter("/tmp/msg/txt", Writer.MODE_APPEND);
   w.append("Java 7");
   w.close();


Answer : B
Q173
Given:
import java.util.ArrayList;
import java.util.List;
public class Legacy {
   public static void main(String[] args) {
      List myList = new ArrayList(3);
         new Legacy().append(myList);
   }
   void append(List myList) {
      myList.add("2"); //line 10
   }
}
What is the result?
A) Compilation succeeds without warnings.
B) Compilation succeeds with an "unchecked" or "unsafe" warning.
C) Compilation succeeds with an "invalid diamond usage"
D) RuntimeException is thrown at line 10.


Answer:B
Q174
public class Cow extends Thread {
   public void run() {
      System.out.print("1 ");
      try {
         Thread.sleep(2000);
      } catch (Exception e) {
         System.out.print("e1 ");
      }
      System.out.print("2 ");
   }
   public static void main(String[] args) {
      Thread t1 = new Cow();
      t1.start();
      try {
         Thread.sleep(1000);
      } catch (Exception e) {
         System.out.print("e2 ");
      }
      try {
         t1.interrupt();
      } catch (Exception e) {
         System.out.print("e3 ");
      }
   }
}
What is the most likely result?
A) 1 e3
B) 1 e1 2
C) 1 e3 2
D) 1 e2 e1 2
E) Compilation fails.


Answer:B
Q175
Given:
interface Boat { }
class MotorizedBoat implements Boat { }
class RadarSystem { }
interface Galley { }
public class Yacht { }
What changes should you make to the Yacht class so that the resulting class is a MotorizedBoat and has a Galley?
A) Make Yacht extend MotorizedBoat and implement Galley.
B) Make Galley a class.Have Yacht extend MotorizedBoat and Galley.
C) Make MotorizedBoat an interface that extends Boat.Have Yacht implement Galley and MotorizedBoat.
D) Make Yacht extend MotorizedBoat.Add a field that references Galley to Yacht.
E) Add a field reference to MotorizedBoat and Galley to Yacht.


Answer: D
Q176
enum MilesFromBoston {
   NEW_YORK (224.60),
   SAN_FRANCISCO (3097.7),
   DENVER (1969.0);
   private final double distance;
   private MilesFromBoston(double distance) {
      this.distance = distance;
   }
   public double getDistance() {
      return this.distance;
   }
}
class TestMiles {
   public static void main(String[ ] args) {
      for (MilesFromBoston mfb : MilesFromBoston.values()) {
         System.out.println("Boston to " + mfb + " is "
         + mfb.getDistance() + " miles.");
      }
   }
}
What is the result?
A) Boston to DENVER is 1969.0 miles.
   Boston to NEW_YORK is 224.6 miles.
   Boston to SAN_FRANCISCO is 3097.7 miles.
B) Boston to NEW_YORK is 224.6 miles.
   Boston to SAN_FRANCISCO is 3097.7 miles.
   Boston to DENVER is 1969.0 miles.
C) Compilation fails.
D) An exception is thrown at runtime.


Answer: B
Q177
/resources/Messages.properties
/resources/Messages_en.properties
/resources/Messages_FR.properties
/codes/Test.jave

and given the content of the files:

Messages.properties:
msg = Good day!
Messages_en.properties:
msg = Welcome!
Messages_FR.properties:
msg = Bienvenue!

and given the code fragment from the Test.jave file:

Locale locale = new Locale.Builder( ).setLanguage("en").setRegion("FR").build( );
Locale.setDefault(locale);
ResourceBundle resource = ResourceBundle.getBundle("resources/Messages");
System.out.print(resource.getString("msg"));

What is the result?
A) Bienvenue!
B) Welcome!
C) Good day!
D) A MissingResourceException is thrown at runtime.


Answer: D
Q178
Given:
class Employee {
   private Integer getSalary() {
      return 126;//line3
   }
}
public class Manager extends Employee {
   public Byte getSalary() {//line7
      return 127;//line8
   }
}
What is the result?
A) Compilation fails due to an error on line 3.
B) Compilation fails due to an error on line 7.
C) Compilation fails due to an error on line 8.
D) Compilation succeeds.


Answer: D
原答案是B. 但實際去執行, 是沒問題的
Q179
Given the code fragment:
public void readContent() throws IOException {
   String record = "";
   BufferedReader br = null;
   try (br = new BufferedReader(new FileReader("quarter1.txt"))) { //line 8
      while ((record = br.readLine()) != null) { //line 9
         System.out.println(record);
      }
   }
}
What is the result?
A) Compilation fails due to an error on line 8.
B) Compilation fails due to an error on line 9.
C) The content of quarter1.txt is printed.
D) An IOException is thrown.


Answer: A
try-catch-resource裏的變數宣告, 不能宣告在外面
網路版本的原始解答是錯誤的 : 他說因為沒有catch. 其實沒有catch是ok的, 因為方法有throws Exception
Q180
Given the code fragment:
public class Test {
   public static void main(string[] args) throws Exception {
      Path file = Paths.get("my.txt");
      // line 14 insert the code here
      System.out.print(attr.getOwner());
   }
}
Which statement,inserted at line 14,enables the code to compile?
A) BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class);
B) DosFileAttributes attr = Files.readAttributes(file, DosFileAttributes.class);
C) FileStore attr = Files.getFileStore(file);
D) AclFileAttributeView attr = Files.getFileAttributeView(file, AclFileAttributeView.class);


Answer: D
check java.nio.file.attribute的AclFileAttributeView

發佈留言

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