Q-141
Given:
interface Rideable {
public String ride() {
return "riding ";
}
}
class Horse implements Rideable {
public String ride() {
return "cantering ";
}
}
class Icelandic extends Horse implements Rideable {
public String ride() {
return "tolting ";
}
}
class Test {
public static void main(String[] args) {
Rideable r1 = new Icelandic();
Rideable r2 = new Horse();
Horse h1 = new Icelandic();
System.out.println(r1.ride() + r2.ride() + h1.ride());
}
}
What is the result?
A. riding riding tolting
B. riding riding cantering
C. tolting cantering tolting
D. tolting cantering cantering
E. Compilation fails.
F. An exception is thrown at runtime.
Answer: E
此題原答案為C, 是錯誤的
在interface裏不能有實体方法, 只能具有抽像方法, 所以此題正確答案為 Compliation Fails
如果改成
interface Rideable {
public String ride();
}
答案才是 C : tolting cantering tolting
Q-142
You have been asked to create a ResourceBundle file to localize an application.
Which code example specifies valid keys menu1 and manu2 with values of File Menu and View Menu?
A. File Menu
View Menu
B. menu1File Menu
menu1View Menu
C. menu1m File menu, menu2, view menu
D. menu1 = File Menu
menu2 = View Menu
Answer: D
Q-143
Given:
01 interface Rideable {
02 String ride() ;
03 }
04 class Horse implements Rideable {
05 String ride() {
06 return "cantering ";
07 }
08 }
09 class Icelandic extends Horse {
10 String ride() {
11 return "tolting ";
12 }
13 }
14 class Test {
15 public static void main(String[] args) {
16 Rideable r1 = new Icelandic();
17 Rideable r2 = new Horse();
18 Horse h1 = new Icelandic();
19 System.out.println(r1.ride()+r2.ride()+ h1.ride());
20 }
21 }
What is the result?
A. tolting cantering cantering
B. cantering cantering cantering
C. compilation fails
D. an exception is thrown at runtime
Answer:C
第5行及第10行要加public
Q-144
Given:
public class SleepOtherThread {
public static void main(String[] args) throws InterruptedException {
Runnable r = new Runnable() {
public void run() {
System.out.print(Thread.currentThread().getName());
}
};
Thread t1 = new Thread(r, "One ");
t1.start();
t1.sleep(2000);
Thread t2 = new Thread(r, "Two ");
t2.start();
t2.sleep(1000);
System.out.print("Main ");
}
}
What is the most likely result?
A. Main One Two
B. Main Two One
C. One Two Main
D. One Main Two
E. Two Main One
Answer: C
sleep()是static方法, 應該是Thread.sleep(). 但Java又允許由物件呼叫static方法, 所以t1.sleep()也不會錯.
但就算是t1.sleep(), 還是執行了Thread.sleep(). 那到底是誰要去睡呢, 絕對不是t1去睡, 而是目前的執行緒去睡. 所以在main()方法中呼叫, 當然是main執行緒去睡
Q-145
Which two are valid initialization statements?
A. Map<String, String> m = new SortedMap<String, String>();
B. Collection m = new TreeMap<Object, Object>();
C. HashMap<Object, Object> m = new SortedMap<Object, Object>();
D. SortedMap<Object, Object> m = new TreeMap<Object, Object> ();
E. Hashtable m= new HashMap();
F. Map<List, ArrayList> m = new Hashtable<List, ArrayList>();
Answer: DF
A SortedMap是抽像類別, 不能new
B Map不屬於Collection類別
C SortedMap不是HashMap的子類別, 且SortedMap是抽像類別, 不能new
E HashMap不是Hashtable的子類別
Q-146
Which four are syntactically correct?
A. package abc;
package def;
import Java.util . * ;
public class Test { }
B. package abc;
import Java.util.*;
import Java.util.regex.* ;
public class Test { }
C. package abc;
public class Test {}
import Java.util.* ;
D. import Java.util.*;
package abc;
public class Test {}
E. package abc;
import java.util. *;
publicclassTest{}
F. publicclass Test{}
package abc;
importjava.util.*{}
G. import java.util.*;
public class Test{}
H. package abc;
public class Test{}
Answer: BEGH
Q-147
Given the code fragment:
public class Test {
public static void main(String[] args) {
Path dir = Paths.get("D:\\company");
//insert code here. Line ***
for (Path entry: stream) {
System.out.println(entry.getFileName());
}
} catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
}
}
Which two try statements, when inserted at line ***, enable you to print files with the extensions .java, .htm, and .jar.
A. try (DirectoryStream stream = Files.newDirectoryStream(dir,"*.{java,htm,jar}")){
B. try (DirectoryStream stream = Files.newDirectoryStream(dir,"*.[java,htm,jar]")) {
C. try (DirectoryStream stream = Files.newDirectoryStream(dir,"*.{java*,htm*,jar*}")) {
D. try (DirectoryStream stream = Files.newDirectoryStream(dir,"**.{java,htm,jar}")) {
Answer: AD
Q-148
Given:
import java.io.File;
import java.nio.file.Path;
public class Test12 {
static String displayDetails(String path, int location) {
Path p = new File(path).toPath();
String name = p.getName(location).toString();
return name;
}
public static void main(String[] args) {
String path = "project//doc//index.html";
String result = displayDetails(path, 2);
System.out.print(result);
}
}
What is the result?
A. doc
B. index.html
C. an IllegalArgumentException is thrown at runtime.
D. An InvalidPthException is thrown at runtime.
E. Compilation fails.
Answer: B
Q-149
Given the code fragment:
public void otherMethod() {
printFile("");
}
public void printFile(String file) {
try (FileInputStream fis = new FileInputStream(file)) {
System.out.println (fis.read());
} catch (IOException e) {
printStackTrace();
}
}
Why is there no output when otherMethod is called?
A. An exception other than IOException is thrown.
B. Standard error is not mapped to the console.
C. There is a compilation error.
D. The exception is suppressed.
Answer: A
printFile("")空字串傳給了file, 會出現FileNotFoundException
Q-150
Given the code fragment:
public void ReadFile (String source) {
char[] c = new char [128];
int cLen = c.length;
try (FileReader fr = new FileReader (source)) {
int count = 0;
int read = 0;
while ((read = fr.read(c)) != -1) {
count += read;
}
System.out.println("Read: " + count + " characters.");
} catch (IOException i) { }
}
What change should you make to this code to read and write strings instead of character arrays?
A. Change FileReader to Readers.
B. Change FileReader to DataReader.
C. Change FileReader to File.
D. Change FileReader to BufferReader.
Answer: D
Q-151
Given the class
class Test implements Serializable{
transient int i=2;
static transient int j=4;
int k=6;
}
and the code fragment:
...
Test obj=new Test();
obj.i=3;
obj.j=5;
obj.k=7;
FileOutputStream fos=new FileOutputStream("Deatils.txt");
ObjectOutputStream objo=new ObjectOutputStream(fos);
objo.writeObject(obj);
FileInputStream fis=new FileInputStream("Details.txt");
ObjectInputStream obj1=new ObjectInputStream(fis);
Test obj1=(Test)obj1.readObject();
System.out.print(obj1.i+", "+obj1.j+", "+obj1.k);
.....
What is result?
A. 0, 0, 7
B. 0, 4, 7
C. 0, 5, 7
D. 2, 5, 7
E. 3, 0, 7
F. Compilation fails
G. An exception in thrown at runtime
Answer : C
transient 物件變數不會被儲存, static 類別變數也不會被儲存, 所以加了transient也是白加(雖然不會出錯)
而即然static 變數不會儲存, 那為什麼 j 不是0呢. 因為這程式還沒關閉啊.
如果再重寫一支只抓取Details.txt的程式, j 一定是 0的
Q-152
01 public interface Shrinkable{
02 //int y;
03 //final int x=4;
04 //final void dos();
05 //abstract void do1();
06 //static boolean do2();
07 //protected void do4();
08 //public String do3() throws Exception;
09 }
Which three lines can be un-commented so as to allow the code to compile?
A. Line2
B. Line3
C. Line4
D. Line5
E. Line6
F. Line7
G. Line8
Answer : BDG
Q-153
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 implicity abstract if it contains abstract methods.
Answer : AB
Q-154
Which statement correctly initializes a generic List collection of String objects?
A. List<String> myList=new ArrayList<>();
B. List myList=new ArrayList();
C. List myList=new ArrayList<String>();
D. List myList=(String)new ArrayList();
Answer : A
Q-155
Given the two classes in separate files:
01 package com.util;
02 public abstract class Motor{
03 protected void turnOn(){
04 }
05 protected abstract void setSpeed(int rpm);
06 }
And
11 package com.util.device;
12 public class ElectricFan extends com.util.Motor{
13
14 }
Which method signation must be declared in ElectricFan for this class to compile?
A. void setSpeed(int rpm)
B. private void setSpeed(int rpm)
C. public void setSpeed(int rpm)
D. public void turnOn()
E. protected void turnOn()
F. void turnOn()
Answer : C
interface的方法都是public, 不用手動加abstract, 因為編譯器自己會加
abstract class的方法不一定要public, 如果是protected, 就是保護機制, 只能讓開發者在同package繼承, 且抽像方法一定要加abstract
Q-156
Given the code fragment
11 String s="%2$s %s %3$b";
12 String[] var={"2", "1", "0"};
13 System.out.print(String.format(s, var));
What is the result?
A. 1 1 false
B. 1 2 false
C. 2 1 false
D. 1 1 true
E. 1 2 true
F. 2 1 true
Answer : E
Q-157
...
11 Path source=Path.get("quarter1.txt");
12 Path target=Path.get("consolidate.txt");
13 Files.copy(source, target, REPLACE_EXISTING);
14 DosFileAttributes attr=Files.readAttributes(target,
15 DosFileAttributes.class);
16 System.out.println(attr.isHidden());
.....
And the following file permissions
quarter1.txt is set as hidden.
consolidate.txt is not set as hidden.
What is the result?
A. flase
B. true
C. AccessDeniedException is thrown at runtime
D. NoSuchFileException is throw at runtime
Answer : B
Q-158
class Printer<E>{
private E e;
public Printer(){
this.e=new E();
}
public void print(){
System.out.println(e);
}
public static void main(String args[]){
new Printer<String>().print();
}
}
What is the result?
A. Output is similar to:java.lang.String@18fe7c
B. Output is similar to:java.lang.E@18fe7c
C. Outputis :E
D. Output is blank
E. Compilation fails
Answer : E
this.e=new E()是錯的, 不可以實例化一個不確定的類別
Q-159
Given the code fragment:
...
String title="Java 7, Java 6";
System.out.print(title.indexOf("Java")+" ");
title.replace("Java", "Jawa");
System.out.print(title.indexOf("Java")+" ");
...
What is the result?
A. 0 0
B. 1 1
C. 0 8
D. 1 9
E. 0 1
F. 1 1
Answer : A
Q-160
Given:
class Bolt implements Runnable{
public void run(){
System.out.print(Thread.currentThread().getName());
}
}
public class Dynamic{
public static void main(String[] args){
Blot b=new Bolt();
Thread t1=new Thread(b, "T1");
Thread t2=new Thread(b, "T2"); //line A
try{
t1.start();
t1.join();
t2.start();//line B
t2.join();
t1.start(); //line C
}catch(Exception e){
System.out.print("exec");
}
}
}
What is the result?
A. T1 T1 T1
B. T1 T2 T1
C. T1 T2 exec
D. Compilation fails due to an error on line A
E. Compilation fails due to an error on line B
F. Compilation fails due to an error on linc C
Answer : C