OCP 804 Exams -06

      在〈OCP 804 Exams -06〉中尚無留言
Q-101
Given:
import java.util.*;
public class SearchText {
   public static void main(String[] args) {
      Object[] array1 = new Object[3];
      array1[0] = "foo";
      array1[1] = 1;
      array1[2] = 'a';
      int index = Arrays.binarySearch(array1, "bar");
      System.out.println(index);
   }
}
What is the result?
A. 1
B. 0
C. 2
D. Compilation fails
E. An exception is thrown at runtime


Answer: E
Q-102
Give:
public class Test {
   public static void main(String[] args) {
      String svar= "sports cars";
      svar.replace(svar,"convertibles");
      System.out.printf("There are %3$s %2$s and %d trucks.",5,svar,2+7);
   }
}
What is the result?
A. There are 27 sports cars and 5 trucks
B. There are 27 convertibles and 5 trucks
C. There are 9 sports cars and 5 trucks
D. There are 9 convertibles and 5 trucks
E. IllegalFormatConversionException is thrown at runtime


Answer: C
Q-103
Which two statements are true about RowSet subinterfaces?
A. A JdbcRowSet object provides a JavaBean view of a result set.
B. A CachedRowSet provides a connected view of the database.
C. A FilteredRowSet object filter can be modified at any time.
D. A WebRowSet returns JSON-formatted data.


Answer: AC
WebRowSet支援離線及xml讀寫
Q-104
Given two classes in separate files:
Parent.java
package a.b;
/ / import statement
public class Parent{
   Child c = new Child();
   ...
}

Child.java
package a.b.c;
public class Child{
}
Which two import statements can make the a.b.Parent class compliable?
A. import a.b.c.Parent;
B. import a.b.c.Child;
C. import a.b.c.*;
D. import a.b.*;
E. import a.*;


Answer: BC
Q-105
Given:
public class Customer{
   private int id;
   private String name;
   public int getId(){}
   public String getName(){}
   public boolean add(Customer new){}
   public void delete(int id){}
   public void Customer find(int id){}
   public boolean update(Customer cust){}
}
What two changes should you make to apply the DAO pattern to this class?
A. Make the Customer class abstract.
B. Make the customer class an interface.
C. Move the add, delete, find, and update methods into their own implementation class.
D. Create an interface that defines the signatures of the add, delete, find, and update methods.
E. Make the add, delete, and find, and update methods private for encapsulation.
F. Make the getName and getID methods private for encapsulation.


Answer: CD
Q-106
Which code fragment is required to load a JDBC 3.0 driver?
A. DriverManager.loadDriver("org.xyzdata.jdbc.NetworkDriver");
B. Class.forName("org.xyzdata.jdbc-NetworkDriver");
C. Connection con = Connection.getDriver
   ("jdbc:xyzdata://localhost:3306/EmployeeDB");
D. Connection con = DriverManager.getConnection
   ("jdbc:xyzdata://localhost:3306/EmployeeDB");


Answer: B
Q-107
Given:
public class CowArray extends Thread {
   static List<Integer> myList = new CopyOnWriteArrayList<>();
   public static void main(String[] args) {
      myList.add(11);
      myList.add(22);
      myList.add(33);
      myList.add(44);
      new CowArray().start();
      for(Integer i: myList) {
         try { 
            Thread.sleep(1000); 
         }
         catch (Exception e) { 
            System.out.print("e1 "); 
         }
         System.out.print(" " +i);
      }
   }
   public void run() {
      try { 
         Thread.sleep(500); 
      }
      catch (Exception e) { 
         System.out.print("e2 "); 
      }
      myList.add(77);
      System.out.print("size: " + myList.size() + ", elements:");
   }
}
What is the most likely result?
A. size: 4, elements: 11 22 33 44
B. size: 5, elements: 11 22 33 44
C. size: 4, elements: 11 22 33 44 77
D. size: 5, elements: 11 22 33 44 77
E. a ConcurrentModification Exception is thrown


Answer: B
Q-108
Which two are true about Singletons?
A. A Singleton must implement serializable.
B. A Singleton has only the default constructor.
C. A Singleton implements a factory method.
D. A Singleton improves a class's cohesion.
E. Singletons can be designed to be thread-safe.


Answer: CE
Q-109
Given:
public enum Direction {
   NORTH, EAST, SOUTH, WEST
}
Which statement will iterate through Direction?
A. for (Direction d : Direction.values()){
   //
   }
B. for (Direction d : Direction.asList()){
   //
   }
C. for (Direction d : Direction.iterator()){
   //
   }
D. for (Direction d : Direction.asArray()){
   //
   }


Answer: A
Q-110
What are two differences between Callable and Runnable?
A. A Callable can return a value when executing, but a Runnable cannot.
B. A Callable can be executed by a ExecutorService, but a Runnable cannot.
C. A Callable can be passed to a Thread, but a Runnable cannot.
D. A Callable can throw an Exception when executing, but a Runnable cannot.


Answer: AD
Q-111
Which three are true?
A. A setAutoCommit(false) method invocation starts a transaction context.
B. An instance of savepoint represents a point in the current transaction context.
C. A rollback () method invocation rolls a transaction back to the last savepoint.
D. A rollback () method invocation releases any database locks currently held by this connection object.
E. After calling rollback(mysavepoint), you must close the savepoint object by calling mySavepoint.close() .


Answer: ABC
Q-112
Given:
String s = new String("3");
System.out.print(1 + 2 + s + 4 + 5);
What is the result?
A. 12345
B. 3345
C. 1239
D. 339
E. Compilation fails.


Answer: B
Q-113
Which two properly implement a Singleton pattern?
A. class Singleton {
      private static Singleton instance;
      private Singleton () {}
      public static synchronized Singleton getInstance() {
         if (instance = = null) {
            instance = new Singleton ();
         }
         return instance;
      }
   }
B. class Singleton {
      private static Singleton instance = new Singleton();
      protected Singleton () {}
      public static Singleton getInstance () {
         return Instance;
      }
   }
C. class Singleton {
      Singleton () {}
      private static class SingletonHolder {
         private static final Singleton INSTANCE = new Singleton (); 
      }
      public static Singleton getInstance () {
         return SingletonHolder.INSTANCE;
      }
   }
D. enum Singleton {
      INSTANCE;
   }


Answer: AB
Q-114
Given:
import static A.counter;
class A {
   static int counter = 0;
   public void printCount(){
      System.out.println(counter + " ");
   }
}
class B extends A{
   public int counter = 10;
      public void printCount(){
         System.out.println(counter + " ");
     }
   }
public class Test{
   public static void main(String[] args){
      A foo = new A();
      A bar = new B();
      foo.printCount();
      bar.printCount();
   }
}
What is the result?
A. 0 0
B. 0 10
C. Compilation fails
D. An exception is thrown at runtime


Answer: B
Q-115
Select four examples that initialize a NumberFormat reference using a factory.
A. NumberFormat nf1 = new DecimalFormat();
B. NumberFormat nf2 = new DecimalFormat("0.00") ;
C. NumberFormat nf3 = NumberFormat.getInstance();
D. NumberFormat nf4 = NumberFormat.getIntegerInstance();
E. NumberFormat nf5= DecimalFormat.getNumberInstance ();
F. NumberFormat nf6 = Number Format.getCurrecyInstance () ;


Answer: CDEF
Q-116
Given the code fragment:
class Base{
   public void process() throws IOException{
      FileReader fr=new FileReader("userguide.txt");
      BufferedReader br=new BufferedReader(fr);
      String record;
      while((record=br.readLine())!=null){
         System.out.println(record);
      }
   }
}
public class Derived extends Base{
   public void process() throws Exception{
      super.process();
      System.out.println("Success");
   }
   public static void main(String[] args){
      try{
         new Derived().process();
      } catch(Exception e){
         System.out.print(e.getClass());
      }
   }
}
If the file userguide.txt does not exist, what is the result?
A. An empty file is created and success is printed.
B. class java.io.FileNotFoundException.
C. class java.io.IOException.
D. class java.lang.Exception.
E. Compilation fails.


Answer: E
FileReader在找不到檔案時, 會丟出FileNotFoundException, 不會建新的檔案
Base的process() throws IOException, 所以Derived的process() throws只可以是IOException 或其子類別, 或者不丟
Q-117
Given:
public class Counter {
   public static int getCount(String[] arr) {
      int count = 0;
      for (String var : arr) {
         if (var != null) {
            count++;
         }
      }
      return count;
   }
   public static void main(String[] args) {
      String[] arr = new String[4];
      arr[1] = "C";
      arr[2] = "";
      arr[3] = "Java";
      assert (getCount(arr) < arr.length);
      System.out.print(getCount(arr));
   }
}
And the commands:
javac Counter.java
java -ea Counter
What is the result?
A. 2
B. 3
C. NullPointException is thrown at runtime
D. AssertionError is thrown at runtime
E. Compilation fails


Answer: B
Q-118
Given the code fragment:
String s = "Java 7, Java 6";
Pattern p = Pattern.compile("Java.+\\d");
Matcher m = p.matcher(s);
while (m.find()) {
   System.out.println(m.group());
}
What is the result?
A. Java 7
B. Java 6
C. Java 7, Java 6
D. Java 7
   java 6
E. Java


Answer: C
\d : [0-9]任一個數字
. 任意字元
+ 出現一次或多次
.+ 多個字
Java.+\\d -->JavaXXXXXX6
Q-119
Given:
import java.util.concurrent.atomic.AtomicInteger;
public class AtomicCounter {
   private AtomicInteger c = new AtomicInteger(0);
   public void increment() {
      // insert code here
   }
}
Which line of code, inserted inside the increment () method, will increment the value of c?
A. c.addAndGet();
B. c++;
C. c = c+1;
D. c.getAndIncrement();


Answer: D
Q-120
Given:
public class Runner {
   public static String name = "unknown";
   public void start() {
      System.out.println(name);
   }
   public static void main(String[] args) {
      name = "Daniel";
      start();
   }
}
What is the result?
A. Daniel
B. Unknown
C. It may print"unknown"or"Daniel"depending on the JVM implementation.
D. Compilation fails.
E. An exception is thrown at runtime.


Answer: D
static 方法不可使用非static 的方法及變數

發佈留言

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