# 簡答題彙整 ## What is true about the java.sql.Statement interface? A. It provides a session with the database. B. It is used to get an instance of a Connection object by using JDBC drivers. C. It provides a cursor to fetch the resulting data. **D. It provides a class for executing SQL statements and returning the results.** - [x] **Answer: D** :::info **參考文件:** [https://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html](https://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html) > The object used for executing a static SQL statement and returning the results it produces. ::: ## Which statement is true about java.time.Duration? A. It tracks time zones. B. It preserves daylight saving time. **C. It defines time-based values.** D. It defines date-based values. - [x] **Answer: C** :::info 選項A,Duration和時區沒有關係。 選項B,Duration也跟日光節約時間沒有關係。 選項C,正確,Duration是以時間為基礎。 選項D,以日期為基礎的應該為Period。 ::: ## Which two statements are true about the Fork/Join Framework? (Choose two.) **A. The RecursiveTask subclass is used when a task does not need to return a result.** B. The Fork/Join framework can help you take advantage of multicore hardware. **C. The Fork/Join framework implements a work-stealing algorithm.** D. The Fork/Join solution when run on multicore hardware always performs faster than standard sequential solution. - [x] **Answer: AC** :::info Fork/Join 框架是java7中加入的一個並行任務框架, 1.可以將任務分割成足夠小的小任務,然後讓不同的執行緒來做這些分割出來的小事情。 2.然後完成之後再進行join,將小任務的結果組裝成大任務的結果。 --- #### Work-Stealing Algorithm (工作竊取演算法) Fork/Join在實現上,大任務拆分出來的小任務會被分發到不同的佇列裡面,每一個佇列都會用一個執行緒來消費,這是為了獲取任務時的多執行緒競爭,但是某些執行緒會提前消費完自己的佇列。而有些執行緒沒有及時消費完佇列,這個時候,完成了任務的執行緒就會去竊取那些沒有消費完成的執行緒的任務佇列,為了減少執行緒競爭,Fork/Join使用雙端佇列來存取小任務,分配給這個佇列的執行緒會一直從頭取得一個任務然後執行,而竊取執行緒總是從佇列的尾端拉取task。 ::: ## Which three statements are benefits of encapsulation? **A. Allows a class implementation to change without changing t he clients** **B. Protects confidential data from leaking out of the objects** C. Prevents code from causing exceptions **D. Enables the class implementation to protect its invariants** E. Permits classes to be combined into the same package F. Enables multiple instances of the same class to be created safely - [x] **Answer: ABD** :::info 選項A,經過封裝過的類別可以保持API接口的一致,就算內部的程式實作有異動,也不需要去更改到其它引用了這個封裝好的類別的程式。 選項B,封裝可以隱藏程式碼,保護機密的資料外洩。 選項C,封裝無法防止例外的發生。 選項D,封裝可以保護類別的實作程式不被修改。 選項E,封裝觀念和套件並沒有直接的關係。 選項F,封裝與將一個類別實體化出不同物件的安全性並沒有關聯。 ::: ## Which action can be used to load a database driver by using JDBC3.0? A. Add the driver class to the META-INF/services folder of the JAR file. B. Include the JDBC driver class in a jdbc.properties file. **C. Use the java.lang.Class.forName method to load the driver class.** D. Use the DriverManager.getDriver method to load the driver class. - [x] **Answer:C** :::info **JDBC4.0之前,載入Driver的方式就是使用Class類別的forName方法,在參數指定Driver的名稱(類別路徑),程式會自動呼叫DriverManager類別的registerDriver方法來載入(註冊)Driver。** 選項A,這是JDBC4.0的機制,要將Driver的名稱寫進「META-INF/services/java.sql.Driver」檔案內,即可自動加載。 選項B,沒有這樣的用法。 選項C,正確載入Driver的方式。 選項D,DriverManager類別的getDriver方法只是用來取得已經載入的Driver物件。 ::: ## You want to create a singleton class by using the Singleton design pattern. **Which two statements enforce the singleton nature of the design?** A. Make the class static. **B. Make the constructor private.** C. Override equals() and hashCode() methods of the java.lang.Object class. **D. Use a static reference to point to the single instance.** E. Implement the Serializable interface. - [x] **Answer:BD** :::info 單例模式就是確保一個類別只能有一個物件實體。 所以 B.private建構子可以防止其他類別實體化出新的物件實體 D.用static變數來儲存第一次實體化出來的物件參考,接下來都返回這個物件參考即可。 ::: ## Which two are elements of a singleton class? (Choose two.) A. a transient reference to point to the single instance **B. a public method to instantiate the single instance** C. a public static method to return a copy of the singleton reference **D. a private constructor to the class** E. a public reference to point to the single instance - [x] **Answer: BD** :::info 單例模式就是確保一個類別只能有一個物件實體。 所以 B.用公開的方法來回船這個實例化出來的物件 D.private建構子可以防止其他類別實體化出新的物件實體 ::: --- 關於 ResourceBundle [參考網址](https://openhome.cc/Gossip/Encoding/ResourceBundle.html) **Question:** ``` You have been asked to create a ResourceBundle which uses a properties file to localize an application. Which code example specifies valid keys of menu1 and menu2 with values of File Menu and View Menu? ``` **選項** ```xml= A. <key name = 'menu1">File Menu</key> <key name = 'menu2">View Menu</key> B. <key>menu1</key><value>File Menu</value> <key>menu2</key><value>View Menu</value> C. menu1, File Menu, menu2, View Menu D. menu1 = File Menu menu2 = View Menu ``` - [x] **Answer:D** :::info ResourceBundle是讀取.properties檔案 使用「key=value」的結構來存放資料。 ::: --- ## Which two are Java Exception classes? **A. SercurityException** B. DuplicatePathException **C. IllegalArgumentException** D. TooManyArgumentsException - [X] **Answer: AC** :::info SercurityException是一種RuntimeException,為使用SecurityManager類別時會拋出的例外類型,這個在開發Android應用程式的時候可能還蠻常見的,因為Android系統本身有對程式的權限進行細部控制。 IllegalArgumentException也是一種RuntimeException,當傳入方法的參數範圍有誤的時候,常會拋出這種例外。例如: Integer.parseInt(null); 以上程式會拋出NumberFormatException,NumberFormatException是IllegalArgumentException的其中一種。 **[參考網址](https://magiclen.org/ocajp-exception-class/)** ::: --- ## What is the name of the Java concept that uses access modifiers to protect variables and hide them within a class? **A. Encapsulation** 封裝 B. Inheritance 繼承 C. Abstraction 抽象化 D. Instantiation 實例化 E. Polymorphism 多型 - [x] **Answer: A** --- ## Which statement is true about java.util.stream.Stream? A. A stream cannot be consumed more than once. **B. The execution mode of streams can be changed during processing.** C. Streams are intended to modify the source data. D. A parallel stream is always faster than an equivalent sequential stream. - [x] **Answer:B** :::info [參考網站](https://magiclen.org/ocpjp-stream-about/) 選項A,串流是可以被消耗很多次的,例如使用peak方法。 選項B,這個選項應該是指串流的序列處理模式和平行化處理模式。 選項C,串流並不會修改到原始的資料,而是產生新的資料出來。 選項D,平行化的串流並不總是比序列的串流快,因為多執行緒會需要消耗額外的資源。 ::: --- ## A method is declared to take three arguments.A program calls this method and passes only two arguments. What is the results? **A. Compilation fails.** B. The third argument is given the value null. C. The third argument is given the value void. D. The third argument is given the value zero. E. The third argument is given the appropriate falsy value for its declared type. F) An exception occurs when the method attempts to access the third argument. - [x] **Answer: A** --- ## Which two interfaces can you use to create lambda expressions? (Choose two.) ```java= interface P {public void method1();} interface Q extends P {public void method1();} interface R extends P {public void method2();} //有兩個method interface S {public default void method() {};} //已經實作了 interface T {public void method1(); public void method2();} //有兩個method interface U {public void method1(); public abstract void method2();} //有兩個method ``` A. T B. R **C. P** D. S **E. Q** F. U - [x] **Answer: CE** --- ## Which statement is true? ```java= class Canvas implements Drawable { public void draw () { } } abstract class Board extends Canvas { } class Paper extends Canvas { protected void draw (int color) { } } class Frame extends Canvas implements Drawable { public void resize () { } } interface Drawable { public abstract void draw (); } ``` A. Board does not compile. B. Paper does not compile. C. Frame does not compile. D. Drawable does not compile. **E. All classes compile successfully.** - [x] **Answer: E** [參考網站](https://magiclen.org/ocpjp-extend-implement/) --- ## Which two statements are true about synchronization and locks? (Choose two.) **A. A thread automatically acquires the intrinsic lock on a synchronized statement when executed.** **B. The intrinsic lock will be retained by a thread if return from a synchronized method is caused by an uncaught exception.** C. A thread exclusively owns the intrinsic lock of an object between the time it acquires the lock and the time it releases it. D. A thread automatically acquires the intrinsic lock on a synchronized method's object when entering that method. E. Threads cannot acquire intrinsic locks on classes. - [x] **Answer: AB** --- ## Which statement is true about the DriverManager class? **A. It returns an instance of Connection.** B. it executes SQL statements against the database. C. It only queries metadata of the database. D. it is written by different vendors for their specific database. - [x] **Answer: A** [參考網站](https://magiclen.org/ocpjp-drivermanager/) --- ## Which statement best describes encapsulation? **A. Encapsulation ensures that classes can be designed so that only certain fields and methods of an object are accessible from other objects.** B. Encapsulation ensures that classes can be designed so that their methods are inheritable. C. Encapsulation ensures that classes can be designed with some fields and methods declared as abstract. D. Encapsulation ensures that classes can be designed so that if a method has an argument MyType x, any subclass of MyType can be passed to that method. - [x] **Answer: A** --- ## Which code fragment is required to load a JDBC 3.0 driver? A. Connection con = Connection.getDriver ("jdbc:xyzdata://localhost:3306/EmployeeDB"); **B. Class.forName("org.xyzdata.jdbc.NetworkDriver");** C. Connection con = DriverManager.getConnection ("jdbc:xyzdata://localhost:3306/EmployeeDB"); D. DriverManager.loadDriver ("org.xyzdata.jdbc.NetworkDriver"); - [x] **Answer: B** :::info JDBC4.0之前,載入Driver的方式就是使用Class類別的forName方法 ::: --- ###### tags: `ocpjp`