OCP 804 Exams -10

      在〈OCP 804 Exams -10〉中尚無留言
Q181
Given:
interface Quackable {
   public void quack ( );
}
interface Waddlable {
   public void waddle( );
}
Which compiles?
A) interface Duckable implements Quackable, Waddlable { }
B) interface Duckable extends Quackable extends Waddlable { }
C) interface Duckable extends Quackable, Waddlable { }
D) interface Duckalbe implements Waddlable extends Quackable {
      public void waddle ( );
   }
E) interface Duckable extends Quackable implements Waddlable {
      public void waddle ( ) { }
   }


Answer: C
interface是可以多重繼承的喔
Q182
Given the content of the Messages.properties and Messages_fr_FR.properties file:
Messages.properties:
greet = Welcom!
Messages_fr_FR.properties:
greet = Bienvenue
Given the code fragment:
public class Test {
   public static void main(String [ ] args) {
      // LIne n1
      Locale.setDefault(locale);
      ResourceBundle resource = ResourceBundle.getBundle("Messages");
      System.out.print(resource.getString("greet"));
   }
}
Which two code fragments,when inserted at Line n1 independently,enable the code to print Bienvenue?
A) Locale locale = new Locale("fr_FR");
B) Locale locale = new Locale("FR");
C) Locale locale = Locale.FRENCH;
D) Locale locale = Locale.FRANCE;
E) Locale locale = new Locale(Locale.FRENCH, Locale.FRANCE);


Answer: A D
原答案說E是對的, 但new Locale()不能放入Locale
Q183
Which syntax gets an instance of java.io.Console?
A) Console cons = Console.getInstance( );
B) Console cons = new Console( );
C) Console cons = Console.instance( );
D) Console cons = System.getConsole( );
E) Console cons = System.console( );


Answer: E
Q184
Given:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
class MyClass {
   public void readFile(String file) throws IOException {
      InputStream in = new FileInputStream(file); //line 7
   }
}
class Test {
   public static void main(String[] args) {
      MyClass mc = new MyClass();
      mc.readFile("userguide.txt"); //line 13
      System.out.println("file: userguide.txt opened.");
   }
}
What is the result, if the file userguide.txt exists?
A) An exception is thrown at run time.
B) Compilation fails due to an error on line 7.
C) Compilation fails due to an error on line 13.
D) file: userguide.txt opened.


Answer: C
13行沒有try-catch, 或main()沒有throws
Q185
A programmer wants to create a worker class whose constructor takes an object that implements the java.lang.Runnable interface.
Which definltion of the worker class is correct?
A) public class Worker<T implements Runnable> {
      Worker(T r) {
         Thread t = new Thread(r);
      }
   }
B) public class Worker<T extends Runnable> {
      Worker(T r) {
         Thread t = new Thread(r);
      }
   }
C) public class Worker<Runnable superT> {
      Worker(T r) {
         Thread t = new Thread(r);
      }
   }
D) public class Worker<?superRunnable> {
      Worker(T r) {
         Thread t = new Thread(r);
      }
   }
E) public class Worker<Runnable> {
      Worker(T r) {
         Thread t = new Thread(r);
      }
   }


Answer: A
Q186
Given:
Integer int1 = 1;
Double double1 = new Double("1.1");
Number int2 = 1;
Number double2 = 0;
System.out.print( int1 + double1);
What is the result?
A) Compilation fails.
B) An exception is thrown at runtime.
C) Output is similar to:java.lang.Integer@187aecajava.lang.Double@187aeca.
D) Output is:11.1.
E) Output is:2.1.


Answer: E
Integer, Double是可以相加的, 但Number不可以
Q187
Given:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegEx01 {
   public static void main(String[] args) {
      String str = "Long ago, in a galaxy far far away.";
      Pattern pattern = Pattern.compile("ago.*far");
      Matcher matcher = pattern.matcher(str);
      if (matcher.find()) {
         System.out.print(matcher.group());
      }
   }
}
What is the result?
A) Long ago,in a galaxy far far away
B) Long ago,in a galaxy far far
C) ago,in a galaxy far
D) ago,in a galaxy far far
E) Compilation fails.


Answer: D
Q188
class Bolt implements Runnable {
   public void run() {
      try {Thread.sleep(1000); }
      catch (Exception e) {System.out.print("exc ");}
      System.out.print(Thread.currentThread().getId() + " "); 
   } 
}
public class Dynamic2 {
   public static void main(String[] args) {
      Bolt b = new Bolt();
      Thread t1 = new Thread(b);
      Thread t2 = new Thread(b); // line A
      try {
         t1.start();
         t2.start();
         t1.start(); //line B
      } catch (Exception e) {
         System.out.print("exc ");
      }
   }
}
What is the result?
A) The output could be: 8 9 8.
B) The output could be: 8 9 4.
C) The output could be: exc 8 8.
D) The output could be: exc 8 9.
E) Compilation fails due to an error on line A.
F) Compilation fails due to an error on line B.


Answer: D
Thread重新啟動會丟出IllegalThreadStateException, 所以會被catch抓到, 先印exc 然後再印t1, t2的Id
Q189
Which two are true about an abstract class?
A) An abstract class can provide implemented methods.
B) An abstract class can contain non-abstract methods.
C) An abstract class can be instantiated.
D) An abstract class can contain private abstract methods.
E) A class is implicitly abstract if it contains abstract methods.


Answer: A B
Q190
Given:
public class Test {
   static int inum=10;
   public static boolean verify() {
      if (inum % 2 == 0) {
         return true;
      }else {
         return false;
      }
   }
   public static void increment() {
      System.out.print((inum++) + ", ");
      assert verify();
   }
   public static void decrement() {
      System.out.print(--inum);
      assert verify();
   }
   public static void main(String[] args) {
      increment();
      decrement();
   }
}
And the command:
java -ea Test
What is the result?
A) prints 9 and throws an assertion error
B) prints 10 and throws an assertion error
C) prints 9, 9 and throws an assertion error
D) prints 10,9 and throws an assertion error


Answer:B
Q191
class PutUpWetToo {
   static String s = "";
   static void doIt(long x) { s += "L "; }
   static void doIt(Integer x) { s += "I "; }
   static void doIt(Object x) { s += "O "; }
   public static void main(String[ ] args) {
      int x = 1;
      Integer [] ia = {1, 2};
      doIt(x);
      doIt(ia);
      System.out.println(s);
   }
}
What is the result?
A) L O
B) L I
C) I O
D) I I
E) Compilation fails.
F) An exception is thrown at runtime.


Answer: A
Q192
public class AccessTest {
   static int count = 0;
   public static void main(String[ ] args) {
      Thread t1 = new Thread(new Runnable() {
         public void run() {
            for(int i=0; i<10000;i++) count++; }}) ;
      Thread t2 = new Thread(new Runnable() {
         public void run() {
            for(int i=0; i<10000;i++) count--; } } ) ; 
      t1.start(); 
      t2.start(); 
      try { 
         t1.join( ); 
         t2.join( ); 
      }catch(Exception e){}
      System.out.println(count) ; 
   } 
} 
Which statement is true? 
A. The result of count must always be 0. 
B. The result of count must always be >0.
C. The result of count must always be <0.
D. The result of count is non-deterministic.
E. ConcurrentAccessException is thrown at runtime.
F. InterruptionException is thrown at runtime.


Answer : D
Q193
interface Vehicle {
   public void start();
   public void stop();
}
interface Motorized {
   public void stop ();
   public void slow ();
}
class Car implements Vehicle, Motorized {
   public void start() {System.out.println("Start "); }
   public void stop() {System.out.println("Stop "); }
   public void slow() {System.out.println("Slow "); }
}
public class TestCar {
   public static void main (String[ ] args) {
      Vehicle e = new Car();//line 16
      e.start(); // line 17
      e.slow(); // line 18
      e.stop(); // line 19
   }
}
What is the result?
A) Start Slow Stop
B) Start followed by a runtime exception
C) Compilation fails at line 16.
D) Compilation fails at line 17.
E) Compilation fails at line 18.
F) Compilation fails at line 19.


Answer: E
((Motorized)e).slow();
Q194
Given this code fragment:
Connection conn = DriverManager.getConnection(dbURL, userName, passWord);
String query = "SELECT ID FROM Employee";
try (Statement stmt = conn.createStatement()) {
   ResultSet rs = stmt.executeQuery(query);
   stmt.executeQuery("SELECT ID FROM Customer");
   while (rs.next()) {
      // process the results
      System.out.println("Employee ID: " + rs.getInt("ID"));
   }
}catch (Exception e) {
   System.out.println("Error");
}
Assume that:
The required database driver is configured in the classpath.
The appropriate database is accessible with the dbURL, userName, and passWord exists.
The Employee and Customer tables are available and each table has id coulumn with a few records and the SQL queries are vaild.
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 13.



Answer: A
Q195
Given the code fragment:
interface Information {
   void info( );
}
interface Event extends Information {
   void details ( );
}
abstract class Quiz implements Event {
   abstract void profile ( );
   public void details( ) {
      System.out.print("Quiz");
   }
}
calss PracticeQuiz extends Quiz {
   // method implementation (s)
}
Which option,containing list of method(s), that must be implemetned in the class practiceQuiz, to enable the code to compile?
A) info( ),details( ),and profile( )
B) Only details( ) and profile( )
C) Only info( ) and profile( )
D) Only profile ( )


Answer: C
Q196
Given this code fragment that should copy bytes from one file to another:
public static void main(String[] args) {
   byte[] b = new byte[128];
   try {
      FileInputStream fis = new FileInputStream(args[0]);
      FileOutputStream fos = new FileOutputStream(args[1]);
      int read;
      // while loop line 16
      fos.write(b, 0, read);
   }
   } catch (IOException i) { }
}
What statement should be inserted on line 16 to make this code function?
A) while (read = fis.read(b)) {
B) while ((read = fis.read()) != 0) {
C) while ((read = fis.read()) != -1) {
D) while ((read = fis.read()) != null) {


Answer: C
Q197
Conncetion con = null;
try {
   // line n1
   if(con!=null) {
      System.out.print("Connection Established.");
   }
} catch (Exception e) {
   System.out.print(e);
}
Assume that dbURL, userName, and password are vaild.
Which code fragment can be inserted at line n1 to enable the code to print Connection Established?
A) con = DriverManager.getConnection(userName, password, dbURL);
B) con = DriverManager.getConnection(dbURL);
   con.setClientInfo("user",userName);
   con.setClientInfo("password",password);
C) Properties prop = new Properties( );
   prop.put("userid", userName);
   prop.put("password", password);
   prop.put("url",dbURL);
   con = DriverManager.getConnection(prop);
D) Properties prop = new Properties( );
   prop.put("user", userName);
   prop.put("password", password);
   con = DriverManager.getConnection(dbURL, prop);


Answer: D
Q198
Given:
interface Shape {
   String category = "2D Graphics";
}
class Figure {
   String category = "3D Graphics";
}
class Parallelogram extends Figure implements Shape { // line n1
   String category;
   Parallelogram() {
      this.category = super.category;
   }
   public static void main(String[] args) {
      Shape obj = new Parallelogram(); //line n2
      System.out.print(obj.category);
   }
}
What is the result?
A) 2D Graphics
B) 3D Graphics
C) A compilation error occurs at line n1.
D) A compilation error occurs at line n2.


Answer: A
只保留static 變數, 物件變數會被拿掉

發佈留言

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