OCP 804 Exams -02

      在〈OCP 804 Exams -02〉中尚無留言
Q-021
Given these facts about Java types in an application:
- Type x is a template for other types in the application.
- Type x implements dostuff ().
- Type x declares, but does NOT implement doit().
- Type y declares doOther() .
Which three are true?
A. Type y must be an interface.
B. Type x must be an abstract class.
C. Type y must be an abstract class.
D. Type x could implement or extend from Type y.
E. Type x could be an abstract class or an interface.
F. Type y could be an abstract class or an interface.


Answer: BDF
Q-022
Given:
class A {
   int a = 5;
   String doA() { return "a1"; }
   protected static String doA2 () { return "a2"; }
}
class B extends A {
   int a = 7;
   String doA() { return "b1"; }
   public static String doA2() { return "b2"; }
   void go() {
      A myA = new B();
      System.out.print(myA.doA() + myA.doA2() + myA.a);
   }
   public static void main (String[] args) { new B().go(); }
}

Which three values will appear in the output?
A. 5
B. 7
C. a1
D. a2
E. b1
F. b2


Answer: ADE
子類別繼承了父類別, 父類別的物件方法就會被覆蓋而消失無形, 所以就算物件參考為父類別, 也只能執行子類別的方法
但物件方法及類別方法依然存活著, 所以物件參考是子類別就存取子類別, 參考是父類別就存取父類別
Q-023
Which represents part of a DAO design pattern?
A. interface EmployeeDAO {
      int getID();
      Employee findByID (intid);
      void update();
      void delete();
   }
B. class EmployeeDAO {
      int getID() { return 0;}
      Employee findByID (int id) { return null;}
      void update () {}
      void delete () {}
   }
C. class EmployeeDAO {
      void create (Employee e) {}
      void update (Employee e) {}
      void delete (int id) {}
      Employee findByID (int id) {return id}
   }
D. interface EmployeeDAO {
      void create (Employee e);
      void update (Employee e);
      void delete (int id);
      Employee findByID (int id);
   }
E. interface EmployeeDAO {
      void create (Connection c, Employee e);
      void update (Connection c, Employee e);
      void delete (Connection c, int id);
      Employee findByID (Connection c, int id);
   }


Answer: D
Q-024
Assuming the port statements are correct, which two code fragments create a one-byte file?
A. OutputStream fos = new FileOutputStream(new File("/tmp/data.bin")); 
   OutputStream bos = new BufferedOutputStream(fos);
   DataOutputStream dos = new DataOutputStream(bos);
   dos.writeByte(0);
   dos.close();
B. OutputStream fos = new FileOutputStream ("/tmp/data.bin"); 
   dataOutputStream dos = new DataOutputStream(fos);
   dos.writeByte(0);
   dos.close();
C. OutputStream fos = new FileOutputStream (new File ("/tmp/data.bin")); 
   dataOutputStream dos = new DataOutputStream(fos);
   dos.writeByte(0);
   dos.close();
D. OutputStream fos = new FileOutputStream ("/tmp/data.bin"); 
   fos.writeByte(0);
   fos.close();


Answer: BC
Q-025
Given:
public class StringSplit01 {
   public static void main(String[] args) {
      String names = "John-.-George-.-Paul-.-Ringo";
      String[] results = names.split("-..");
      for(String str:results) {
         System.out.println(str);
      }
   }
}
What is the result?
A. John?. ?George?. ?Paul ?. ?Ringo
B. John
   George
   Paul
   Ringo
C. An exception is thrown at runtime
D. Compilation fails


Answer: B
"." 代表任意字元, 所以"-.."就是 "-" 後面再加上2個任意字元
Q-026
Given the integer implements comparable:
import java.util.*;
public class SortAndSearch2 {
   static final Comparator IntegerComparator =
      new Comparator() {
         public int compare (Integer n1, Integer n2) {
            return n2.compareTo(n1);
      }
   };
   public static void main(String args[]) {
      ArrayList<Integer> list = new ArrayList<>();
      list.add (4);
      list.add (1);
      list.add (3);
      list.add (2);
      Collections.sort(list, null);
      System.out.println(Collections.binarySearch(list, 3));
      Collections.sort(list,IntegerComparator);
      System.out.println(Collections.binarySearch(list, 3));
   }
}
What is the result?
A. 21
B. 12
C. 22
D. 23
E. 32


Answer: A
binarySearch(List, object)使用二進位搜索演算找指定的元素, 並傳回該元素的索引值
第一次排序是用null, 所以是自然排序法, {1234}, 3的index為2
第二次排序是由大到小, 所以為{4321}, 3的index 為 1

Q-027

Given:
import java.util.StringTokenizer;
public class StringTokenizer01{
   public static void main(String args[]){
      String names="Jone, George, Paul, Ringo";
      StringTokenizer st=new StringTokenizer(names, ", ");
      for(String str:st){
         System.out.println(str);
      }
   }
}
What is the result?
A. John, George, Paul, Ringo
B. John
   George
   Paul
   Ringo
C. JohnGeorgePaulRingo
D. An exception is thrown at runtime
E. Compilation fails


Answer:E
StringTokenizer 標記符, 並不是把所有分割的字串傳回, 而是要做用nextElement()傳回下一個分割字串
while(st.hasMoreElements()){
   System.out.println(st.nextElement());
}

Q-028

Given the interface:
Public interface Idgenerator {
   int getNextId();
}
Which class implements IdGenerator in a thread-safe manner, so that no threads can get a duplicate id value current access?
A. Public class Generator Implements IdGenerator {
      Private AtomicInteger id = new AtomicInteger (0);
      public int getNextId(){
         return id.incrementAndget();
      }
   }
B. Public class Generator Implements IdGenerator {
      private int id = 0;
      public int getNextId(){
         return ++id;
      }
   }
C. Public class Generator Implements IdGenerator {
      private volatile int Id = 0;
      public int getNextId(){
         return ++id;
      }
   }
D. Public class Generator Implements IdGenerator {
      private int id = 0;
      public int getNextId() {
         synchronized (new Generator()) {
            return + + id;
         }
      }
   }
E. Public class Generator Implements IdGenerator {
      private int id = 0;
      public int getNextId() {
         synchronized (id) {
            return + + id;
         }
      }
   }


Answer: A
Q-029
Given the code fragment:
public class Rank {
   static CopyOnWriteArraySet<String> arr = new CopyOnWriteArraySet<>();
   static void verify() {
      String var ="";
      Iterator e=arr.iterator();
      while (e.hasNext()) {
         var = e.next();
         if(var.equals("A"))
            arr.remove(var);
      }
   }
   public static void main (String[] args) {
      ArrayList<String> list1 = new ArrayList<>();
      list1.add("A"); 
      list1.add("B");
      ArrayList<String> list2 = new ArrayList<>();
      list2.add("A"); 
      list2.add("D");
      arr.addAll(list1);
      arr.addAll(list2);
      verify();
      for(String var : arr)
         System.out.print(var + " ");
   }
}
What is the result?
A. Null B D
B. Null B null D
C. B D
D. D
E. An exception is thrown at runtime


Answer: C
CopyOnWriteArraySet, 是Set喔, 不允許重複值
Q-030
Which two code blocks correctly initialize a Locale variable?
A. Locale loc1 = "UK";
B. Locale loc2 = Locale.getInstance("ru");
C. Locale loc3 = Locale.getLocaleFactory("RU");
D. Locale loc4 = Locale.UK;
E. Locale loc5 = new Locale("ru", "RU");


Answer: DE
Q-031
Given:
class Product {
   private int id;
   public Product (int id) {
      this.id = id;
   }
   public int hashCode() {
      return id + 42;
   }
   public boolean equals (Object obj) {
      return (this == obj) ? true : super.equals(obj);
   }
}
public class WareHouse {
   public static void main(String[] args) {
      Product p1 = new Product(10);
      Product p2 = new Product(10);
      Product p3 = new Product(20);
      System.out.print(p1.equals(p2) + " ");
      System.out.print(p1.equals(p3) );
   }
}
What is the result?
A. false false
B. true false
C. true true
D. Compilation fails
E. An exception is thrown at runtime


Answer: A
Q-032
Given:
class Erupt implements Runnable {
   public void run() {
      System.out.print(Thread.currentThread().getName());
   }
}
public class Yellowstone {
   static Erupt e = new Erupt();
   Yellowstone() { new Thread(e, "const").start(); } // line A
   public static void main(String[] args) {
      new Yellowstone();
      new Faithful().go();
   }
   static class Faithful {
      void go() { new Thread(e, "inner").start(); } // line B
   }
}
What is the result?
A. Both const and inner will be in the output.
B. Only const will be in the output.
C. Compilation fails due to an error on line A.
D. Compilation fails due to an error on line B.
E. Anexceptionis thrown at runtime.


Answer: A
Q-033
Given the code fragment:
try {
   conn.setAutoCommit(false);
   stmt.executeUpdate("insert into employees values(1,'Sam')");
   Savepoint save1 = conn.setSavepoint("point1");
   stmt.executeUpdate("insert into employees values(2,'Jane')");
   conn.rollback();
   stmt.executeUpdate("insert into employees values(3,'John')");
   conn.setAutoCommit(true);
   stmt.executeUpdate("insert into employees values(4,'Jack')");
   ResultSet rs = stmt.executeQuery("select * from employees");
   while (rs.next()) {
      System.out.println(rs.getString(1) + " " + rs.getString(2));
   }
} catch(Exception e) {
   System.out.print(e.getMessage());
}
What is the result of the employees table has no records before the code executed?
A. 1 Sam
B. 4 Jack
C. 3 John
   4 Jack
D. 1 Sam
   3 John
   4 Jack


Answer: C
rollback()如果參數沒有savePoint的名稱, 是捲回全部的東西, 所以上述1跟2都撤消掉了
Q-034
Which statement declares a generic class?
A. public class Example <T>{ }
B. public class { }
C. public class Example <> { }
D. public class Example (Generic){ }
E. public class Example (G) { }
F. public class Example { }


Answer: A
Q-035
Given:
public abstract class Account {
   abstract void deposit (double amt);
   public abstract Boolean withdraw (double amt);
}
public class CheckingAccount extends Account {
}
What two changes, made independently, will enable the code to compile?
A. Change the signature of Account to: public class Account.
B. Change the signature of CheckingAccount to: public abstract class CheckingAccount
C. Implement private methods for deposit and withdraw in CheckingAccount.
D. Implement public methods for deposit and withdraw in CheckingAccount.
E. Change Signature of checkingAccount to: CheckingAccount implements Account.
F. Make Account an interface.


Answer: BD
Q-036
Given:
Deque  myDeque = new ArrayDeque();
myDeque.push("one");
myDeque.push("two");
myDeque.push("three");
System.out.println(myDeque.pop());
What is the result?
A. Three
B. One
C. Compilation fails.
D. The program runs, but prints no output.


Answer: A
Q-037
Given the code fragment:
SimpleDataFormat sdf;
Which code fragment displays the three-character month abbreviation?
A. SimpleDateFormatsdf = new SimpleDateFormat ("mm", Locale.UK); 
   System.out.println ("Result:" + sdf.format(new Date()));
B. SimpleDateFormatsdf = new SimpleDateFormat ("MM", Locale.UK); 
   System.out.println ("Result:" + sdf.format(new Date()));
C. SimpleDateFormatsdf = new SimpleDateFormat ("MMM", Locale.UK); 
   System.out.println ("Result:" + sdf.format(new Date()));
D. SimpleDateFormatsdf = new SimpleDateFormat ("MMMM", Locale.UK); 
   System.out.println ("Result:" + sdf.format(new Date()));


Answer: C
Q-038
Given the code fragment:
1. path file = paths.get(args[0]);
2. try {
3.    // statements here
4. }
5. catch (IOException) i ) {
6. }
And a DOS-based file system:
Which option, containing statement(s), inserted at line 3, creates the file and sets its attributes to hidden and read-only?
A. DOSFileAttributes attrs = Files.setAttribute(file,"dos:hidden","dos: readonly");
   Files.createFile(file, attrs);
B. Files.craeteFile(file);
   Files.setAttribute(file,"dos:hidden","dos:readonly");
C. Files.createFile(file,"dos:hidden","dos:readonly");
D. Files.createFile(file);
   Files.setAttribute(file,"dos:hidden", true);
   Files.setAttribute(file,"dos:readonly", true);


Answer: D
Q-039
When using the default file system provider with a JVM running on a DOS-based file system, which statement is true?
A. DOS file attributes can be read as a set in a single method call.
B. DOS file attributes can be changed as a set in a single method call.
C. DOS file attributes can be modified for symbolic links and regular files.
D. DOS file attributes can be modified in the same method that creates the file.


Answer: A
Q-040
Given:
ConcurrentMap <String, String> partList = new ConcurrentMap<>();
Which fragment puts a key/value pair in partList without the responsibility of overwriting an existing key?
A. partList.out(key,"Blue Shirt");
B. partList.putIfAbsent(key,"Blue Shirt");
C. partList.putIfNotLocked (key,"Blue Shirt");
D. partList.putAtomic(key,"Blue Shirt")
E. if (!partList.containsKey(key)) partList.put (key,"Blue Shirt");


Answer: B

發佈留言

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