OCP 804 Exams -03

      在〈OCP 804 Exams -03〉中尚無留言
Q-041
Given the code fragment:
String query = "SELECT ID FROM Employee";
try (Statement stmt = conn.createStatement()) {
   ResultSet rs = stmt.executeQuery(query);
   stmt.executeQuery("SELECT ID FROM Customer"); // Line ***
   while (rs.next()) {
      // process the results
      System.out.println ("Employee ID: " + rs.getInt("ID"));
   }
} catch (Exception e) {
   System.err.println ("Error");
}
Assume that the SQL queries return records. What is the result of compiling and executing this code fragment?
A. The program prints employee IDs
B. The program prints customer IDs
C. The program prints Error
D. Compilation fails on line***


Answer: A
Q-042
Given the following code fragment:
public static void main(String[] args) {
   Connection conn = null;
   Deque myDeque = new ArrayDeque<>();
   myDeque.add("one");
   myDeque.add("two");
   myDeque.add("three");
   System.out.println(myDeque.remove());
}
What is the result?
A. Three
B. One
C. Compilation fails
D. The program runs, but prints no outout


Answer: B
Q-043
Given the code fragment:
1. Thread t1 = new Thread ();
2. t1.start ()
3. t1.join ( );
4. // . . .
Which three are true?
A. On line 3, the current thread stops and waits until the t1 thread finishes.
B. On line 3, the t1 thread stops and waits until the current thread finishes.
C. On line 4, the t1 thread is dead.
D. On line 4, the t1 thread is waiting to run.
E. This code cannot throw a checked exception.
F. This code may throw a checked exception.


Answer: ACF
Q-044
The two methods of code reuse that aggregate the features located in multiple classes are ____________ .
A. Inheritance
B. Copy and Paste
C. Composition
D. Refactoring
E. Virtual Method Invocation


Answer: AC
不同的類別有著相同的程式碼, 不是重新寫一遍, 而是使用繼承或是組合的方式接進來
Q-045
Given the code fragment:
public static void main(String[] args) {
   String source ="d:\\company\\info.txt";
   String dest ="d:\\company\\emp\\info.txt";
   // insert code fragment here. Line ***
   } catch (IOException e) {
      System.err.println("Caught IOException"+ e.getMessage());
   }
}
Which two try statements, when inserted at line ***, enable the code to successfully move the file info.txt to the destination directory, even if a file by the same name already exists in the destination directory?
A. try (FileChannel in = new FileInputStream (source). getChannel(); 
      FileChannel out = new FileOutputStream(dest).getChannel())
   { in.transferTo(0, in.size(), out);
B. try (Files.copy(Paths.get(source),Paths.get(dest));
   Files.delete (Paths.get(source));
C. try (Files.copy(Paths.get(source), Paths.get(dest),StandardCopyOption.REPLACE_Existing); 
   Files.delete(Paths.get(source));
D. try (Files.move(Paths.get(source),Paths.get(dest));
E. try(BufferedReader br = Files.newBufferedReader(Paths.get(source), Charset.forName("UTF- 8"));
   BufferedWriter bw = Files.newBufferedWriter(Paths.get(dest), Charset.forName("UTF-8")); 
   String record = "";
   while ((record = br.readLine()) ! = null) {
      bw.write(record);
      bw.newLine();
   }
   Files.delete(Paths.get(source));


Answer: CE
Q-046
Which two actions can be used in registering a JDBC 3.0 driver?
A. Add the driver class to the META-INF/services folder of the JAR file.
B. Set the driver class name by using the jdbc.drivers system property.
C. Include the JDBC driver class in a jdbcproperties file.
D. Use the java.lang.class.forName method to load the driver class.
E. Use the DriverManager.getDriver method to load the driver class.


Answer: BD
Q-047
Given:
interface Books {
//insert code here
}
Which fragment, inserted in the Books interface, enables the code to compile?
A. public abstract String type;
   public abstract String getType();
B. public static String type;
   public abstract String getType();
C. public String type = "Fiction";
   public static String getType();
D. public String type = "Fiction";
   public abstract String getType();


Correct Answer: D
Q-048
The default file system includes a logFiles directory that contains the following files:
Log-Jan 2009
log_0l_20l0
log_Feb20l0
log_Feb2011
log_10.2012
log-sum-2012
How many files Hoes the matcher in this fragment match?
PathMatcher matcher = FileSystems.getDefault ().getPathMatcher ("glob: *???_*1?" );
A. One
B. Two
C. Three
D. Four
E. Five
F. Six


Answer: D
符合的檔案為
log_0l_20l0
log_Feb20l0
log_Feb2011
log_10.2012
Q-049
Given:
01 interface Event {
02    String type = "Event";
03    public void details();
04 }
05 class Quiz {
06    static String type = "Quiz";
07 }
08 public class PracticeQuiz extends Quiz implements Event {
09    public void details() {
10       System.out.print(type);
11   }
12    public static void main(String[] args) {
13       new PracticeQuiz().details();
14       System.out.print(" " + type);
15    }
16 }
What is the result?
A. Event Quiz
B. Event Event
C. Quiz Quiz
D. Quiz Event
E. Compilation fails


Answer: E
存取變數時, 如果有指定參考, 那就存取那個參考.
但如果是匿名類別的話, 就要明確的指定是那一個類別或介面
所以第10行, 必需指定是 Event.type 還是Quiz.type
如果第6行是 String type="Quiz";那就要指定是super.type或是Event.type
Q-050
Given that myfile.txt contains:
First
Second
Third
Given the following code fragment:
public class ReadFile02 {
   public static void main(String[] args) {
      String fileName1 = "myfile.txt";
      String fileName2 = "newfile.txt";
      try (BufferedReader buffIn =
           new BufferedReader(new FileReader(fileName1));
           BufferedWriter buffOut =
           new BufferedWriter(new FileWriter(fileName2))
      ) {
         String line = ""; int count = 1;
         line = buffIn.readLine();
         while (line != null) {
            buffOut.write(count + ": " + line);
            buffOut.newLine();
            count++;
            line = buffIn.readLine();
         }
      } catch (IOException e) {
        System.out.println("Exception: " + e.getMessage());
      }
   }
}
What is the result?
A. new file.txt contains:
   1: First
   2: Second
   3: Third
B. new file.txt contains:
   1: First 2: Second 3: Third
C. newfile.
D. an exception is thrown at runtime
E. compilation fails


Answer: A
Q-051
Given:
public class MarkOutOfBoundsException extends ArrayIndexOutOfBoundsException {
   public class Test {
      public void verify(int[] arr) throws ArrayIndexOutOfBoundsException {
         for (int i = 1; i <= 3; i++) { if(arr[i] > 100)
               throw new MarkOutOfBoundsException();
            System.out.println(arr[i]);
         }
      }
   }
   public static void main(String[] args) {
      int[] arr = {105,78,56};
      try {
         new Test().verify(arr);
      } catch (ArrayIndexOutOfBoundsException |
         MarkOutOfBoundsException e) {
         System.out.print(e.getClass());
      }
   }
}
What is the result?
A. Compilation fails.
B. 78
   class java.lang.Array.IndexOutOfBoundException
C. class MarkOutOfBoundException
D. class java.lang.arrayIndexOutOfBoundException


Answer: A
需要new MarkOutOfBoundsException(), 而且在main中的catch, 裏面是父子類別, 所以不能用多重catch
Q-052
Given the code fragment:
class Finder extends SimpleFileVisitor {
   private final PathMatcher matcher;
   private static int numMatches = 0;
   Finder () {
      matcher = FileSystems.getDefault().getPathMatcher("glob:*java");
   }
   void find(Path file) {
      Path name = file.getFileName();
      if (name != null && matcher.matches(name)) {
         numMatches++;
      }
   }
   void report()
   {
      System.out.println("Matched: " + numMatches);
   }
   @Override
   public FileVisitResult visitFile(Path file, BasicFileAttributes attrs){
      find(file);
      return CONTINUE;
   }
}
public class Visitor {
   public static void main(String[] args) throws IOException {
      Finder finder = new Finder();
      Files.walkFileTree(Paths.get("D:\\Project"), finder);
      finder.report();
   }
}
What is the result?
A. Compilation fails
B. 6
C. 4
D. 1
E. 3
F. Not possible to answer due to missing exhibit.


Answer: F
這程式是可以執行啦, 但又沒有說明 d:\Project 底下有什麼檔案, 所以我那知道會印出什麼鬼東西出來
Q-053
Which concept allows generic collections to interoperate with java code that defines collections that use raw types?
A. bytocode manipulation
B. casting
C. autoboxing
D. auto-unboxing
E. type erasure


Answer: C
Q-054
Given:
public class Task {
   String title;
   static class Counter {
      int counter = 0;
      void increment() {counter++}
   }
   public static void main(String[] args) {
      // insert code here
   }
}
Which statement, inserted at line 8, enables the code to compile?
A. new Task().new Counter().increment();
B. new Task().Counter().increment();
C. new Task.Counter().increment();
D. Task.Counter().increment();
E. Task.Counter.increment();


Answer: C
Q-055
For which three objects must a vendor provide implementations in its JDBC driver?
A. Time
B. Date
C. Statement
D. ResultSet
E. Connection
F. SQLException
G. DriverManager


Answer: CDE
資料庫廠商要提供四種東西, driver, Connection, Statement, ResultSet
DriverManager是Jave提供的管理介面用來統一所有廠商的總經理, 所以跟廠商無關
Q-056
Given these facts about Java classes in an application:
- Class X is-a Class SuperX.
- Class SuperX has-a public reference to a Class Z.
- Class Y invokes public methods in Class Util.
- Class X uses public variables in Class Util.
Which three statements are true?
A. Class X has-a Class Z.
B. Class Util has weak encapsulation.
C. Class Y demonstrates high cohesion.
D. Class X is loosely coupled to Class Util.
E. Class SuperX's level of cohesion CANNOT be determined


Answer: ABE
SuperX有Z, 所以X就會有Z
X直接使用Util的public vairable, 那Util就表示沒有封裝好
D : Class X is loosely coupled to Class Util=>X對Util是低偶合, 也就是說X對Util的相依性很低. 答案當然是錯的, 因為如果不依賴, 那就不要有Util啊Class X is loosely coupled to Class Util.
Q-057
Given:
01 final class FinalShow {
02    final String location;
03    FinalShow(final String loc) {
04       location = loc;
05    }
06    FinalShow(String loc, String title) {
07       location = loc;
08       loc = "unknown";
09    }
10 }
What is the result?
A. Compilation succeeds.
B. Compilation fails due to an error on line 1.
C. Compilation fails due to an error on line 2.
D. Compilation fails due to an error on line 3.
E. Compilation fails due to an error on line 4.
F. Compilation fails due to an error on line 8.


Answer: A
Q-058
Person class as below
public Person(int id)
public int getId()
public String getContactDetails()
public void setContactDetails(String contactDetails)
public String getName()
public void setName(String name)
public Person getPerson(int id)throws Exception
public void createPerson(Person p)throws Exception
public void deletePerson(int id)throws Exception
public void updatePerson(Person p)throws Exception

Which group of method is moved to a new class when implementing the DAO pattern?
A. public in getId ()
   public String getContractDetails ()
   public Void setContractDetails(String contactDetails)
   public String getName ()
   public void setName (String name)
B. public int getId ()
   public String getContractDetails()
   public String getName()
   public Person getPerson(int id) throws Exception
C. public void setContractDetails(String contractDetails) public void setName(String name)
D. public Person getPerson(int id) throws Exception
   public void createPerson(Person p) throws Exception
   public void deletePerson(int id) throws Exception
   public void updatePerson(Person p) throws Exception


Answer: D
反正就是把新增, 刪除, 修改, 查詢給移除掉就對了
這題好像很喜歡考
Q-059
Given:
01 public class Test {
02    Integer x;
03    public static void main(String[] args) {
04       new Test().go(5);
05    }
06    void go(Integer i) { // line 6
07       System.out.print(x + ++i); // line 7
08    }
09 }
What is the result?
A. 5
B. 6
C. An exception is thrown at runtime
D. Compilation fails due to an error on line 6
E. Compilation fails due to an error on line 7


Answer: C
Integer i 是可以自動解封箱作 ++的動作, 但~~Integer x是 null, 加個鬼, 所以runtime時期會出現 java.lang.NullPointerExceptionis
但如果 Integer x = 0; 那就可以加了, 答案會變成 6
這題的陷井在C, 明明就是錯, 但他沒寫是第幾行throws. 是第7行throws, 不是第7行compile error
Q-060
Given:
interface Car{
   public void start();
}
class BasicCar implements Car{
   public void start(){}
}
public class SuperCar{
   Car c=new BasicCar();
   public void start(){c.start();}
}
Which three are true?
A. BasicCar uses composition.
B. SuperCar uses composition.
C. BasicCar is-a Car.
D. SuperCar is-a Car.
E. SuperCar takes advantage of polymorphism
F. BasicCar has-a Car


Answer: BCE
陷井在E, SuperCar takes advantage of ploymorphism, 這是英文語法=>SuperCar利用了多型, 沒錯,因為SuperCar使用了 car c =new BasicCar();
賽林涼咧,這是考Java還是考英文啊。
改天拎北就叫這些番邦夷族來考中文~~~what is "拎北"!!!

發佈留言

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