Q-021
Given
class X{
int x1, x2, x3;
}
class Y extends X{
int y1;
Y(){
x1=1;
x2=2;
y1=10;
}
}
class Z extends Y{
int z1;
Z(){
x1=3;
y1=20;
z1=100;
}
}
public class Test1 {
public static void main(String[] args) {
Z obj=new Z();
System.out.println(obj.x3+", "+obj.y1+", "+obj.z1);
}
}
Which constructor initializes the variable x3?
A) only the default constructor of class X
B) only the no-argument constructor of class Y
C) only the no-argument constructor of class Z
D) only the default constructor of object class
Answer : A
Q-022
Given
class Patient{
String name;
public Patient(String name){
this.name=name;
}
}
8. public class Test1 {
9. public static void main(String[] args) {
10. List ps=new ArrayList();
11. Patient p2=new Patient("Mike");
12. ps.add(p2);
13. Patient p3=new Patient("Mikee");
14. //insert code here
15. if(f>=0){
16. System.out.print("Mkie Found");
17. }
18. }
20. }
Which code fragment when inserted at line 14, enables the code to print Mike Found?
A) int f=ps.indexOf(new Patient("Mkie");
B) int f=ps.indexOf(Patient("Mike"));
C) Patient p=new Patient("Mike");
int f=ps.indexOf(p)
D) int f=ps.indexOf(p2);
Answer : D
Q-023
Which statement will empty the contents of a StringBuilder variable named sb?
A) sb.deleteAll();
B) sb.delete(0,sb.size());
C) sb.delete(0, sb.length());
D) sb.removeAll();
Answer : C
StringBuilder 沒有deleteAll()及removeAll();
Q-024
Given
public class Access {
private int x=0;
private int y=0;
public static void main(String[] args) {
Access accApp=new Access();
accApp.printThis(1,2);
accApp.printThat(3,4);
}
public void printThis(int x, int y){
x=x;
y=y;
System.out.println("x:"+this.x+" y:"+this.y);
}
public void printThat(int x, int y){
this.x=x;
this.y=y;
System.out.println("x:"+this.x+" y:"+this.y);
}
}
What is the result?
A) x:1 y:2
x:3 y:4
B) x:0 y:0
x:3 y:4
C) x:1 y:2
x:0 y:0
D) x:0 y:0
x:0 y:0
Answer : B
注意 : 要使用Field, 一定要使用this
Q-025
Given
public class Case {
public static void main(String[] args) {
String product="Pen";
product.toLowerCase();
product.concat(" Box".toLowerCase());
System.out.println(product.substring(4,6));
}
}
What is the result?
A) box
B) nbo
C) bo
D) nb
E) An exception is thrown at runtime
Answer : E
toLowerCase(), concat(), 皆是產生新的字串, 不是改變原來的字串(原來的字串不會被更改)
所以到了concat後,product一樣是 "Pen", 所以沒有4及6的Index
上述要改成
String p1=product.toLowerCase();
string p2=p1.concat(" Box".toLowerCase());
system.out.println(p2.substring(4,6)); <=="bo"
Q-026
Which two action will improve the encapsulation of a class?
A) Changing the access modifier of a field from public to private
B) Removing the public modifier from a class declaration
C) Changing the return type of a method to void
D) Returning a copy of the contents of an array or ArrayList instead of a direct reference.
Answer : A, D
Q-027
public class Series {
private boolean flag;
public void displaySeries(){
int num=2;
while(flag){
if(num %7 ==0){
flag=false;
}
System.out.print(num);
num+=2;
}
}
public static void main(String[] args) {
new Series().displaySeries();
}
}
What is the result?
A) 2 4 6 8 10 12
B) 2 4 6 8 10 12 14
C) Compilation fails.
D) The program prints multiples of 2 infinite times.
E) The program prints nothing.
Answer : E
Q-028
Given the code fragment
String[] colors={"red","blue","green","yellow","maroon","cyan";
Which code fragment prints blue, cyan,?
A) for (String c:colors){
if(c.length()!=4){
continue;
}
System.out.print(c+", ");
}
B) for (String c:colors[]){
if(c.length()!=4){
continue;
}
System.out.print(c+", ");
}
C) for (String c:String[] colors){
if(c.length() >=3){
continue;
}
System.out.print(c+", ");
}
D) for (String c:colors){
if(c.length()!=4{
System.out.print(c+", ");
continue;
}
}
Answer : A
Q-029
Given the code fragment
class DBConfiguration{
String user;
Striing password;
}
And
4. public class DBHandler{
5. DBConfiguration configureDB(String uname, String password){
6. //insert code here
7. }
8. public static void main(String[] args){
9. DBHandler r=new DBHandler();
10. DBConfiguration dbConf=r.configureDB("manager","manager");
11. }
12. }
Which code fragment must be inserted at line 6 to enable the code to compile?
A) DBConfiguration f;
return f;
B) return DBConfiguration;
C) reutnr new DBConfiguration();
D) return 0;
Answer : C
Q-030
public class Msg {
public static String doMsg(char x){
return "Good Day!";
}
public static String doMsg(int y){
return "Good Luck!";
}
public static void main(String[] args) {
char x=8;
int z='8';
System.out.println(doMsg(x));
System.out.print(doMsg(z));
}
}
What is the result?
A) Good Day!
Good Luck!
B) Good Day!
Good Day!
C) Good Luck!
Good Day!
D) Good Luck!
Good Luck!
E) Compilation fails.
Answer : A
Q-031
public class Test {
public static void doChange(String sb){
sb.concat("Monday");
}
public static void main(String[] args) {
StringBuilder sb=new StringBuilder("Sunday");
doChange(sb);
System.out.print(sb);
}
}
What is the result?
A) Sunday Monday
B) Sunday
C) Compilation fails.
D) An exception is thrown at runtime.
Answer : C
String 及StringBuilder 是不一樣的,不能相傳
Q-032
public class Test {
public static void main(String[] args) {
int numbers[];
numbers=new int[2];
numbers[0]=10;
numbers[1]=20;
numbers=new int[4];
numbers[2]=30;
numbers[3]=40;
for (int x:numbers){
System.out.print(" "+x);
}
}
}
What is the result?
A) 10 20 30 40
B) 0 0 30 40
C) Compilation fails.
D) An exception is thrown at runtime.
Answer : B
Q-033
Given
public class MyFor3{
public static void main(String[] args){
int[] xx=null;
for(int ii:xx){
System.out.println(ii);
}
}
}
What is the result?
A) null
B) Compilation fails.
C) An exception is thrown at runtime.
D) 0
Answer : C
Q-034
public class X implements Z{
public String toString(){
return "X ";
}
public static void main(String[] args) {
Y myY=new Y();
X myX=myY;
Z myZ=myX;
System.out.print(myX);
System.out.print((Y)myX);
System.out.print(myZ);
}
}
class Y extends X{
public String toString(){
return "Y ";
}
}
interface Z{}
What is the result?
A) X X X
B) X Y X
C) Y Y X
D) Y Y Y
Answer : D
父類別的方法都被子類別給覆蓋了, 再怎麼轉, 都是子類別的方法
Q-035
The protected modifier on a field declaration within a public class means that the field____
A) can not be modified
B) can be read but not writeen from outside the class
C) can be read and writeen from this class and its subclasses only within the same package
D) can be read and written from this class and its subclasses defined in any package
Answer : D
注意 : protected 可以在其他package的子類別裏使用, 但在其他package產生物件後, 不能透過物件使用
Q-036
Given the code fragment
class Test2{
int fvar;
static int cvar;
public static void main(String[] args){
Test2 t=new Test2();
//insert code here to write field variables
}
}
Which two code fragments, inserted independently, enable the code to compile?
A) t.fvar=200;
t.cvar=400;
B) fvar=200;
cvar=400;
C) this.fvar=200;
this.cvar=400;
D) t.fvar=200;
Test2.cvar=400;
E) this.fvar=200;
Test.cvar=400;
Answer : A, D
Q-037
public class Calculator {
public static void main(String[] args) {
int num=5;
int sum;
do{
sum+=num;
}while((num--)>1);
System.out.println("The sum is "+ sum);
}
}
What is the result?
A) The sum is 2.
B) The sum is 14.
C) The sum is 15.
D) The loop executes infinite times.
E) Compilation fails.
Answer : E
若sum 有初始化, 則結果會是15
Q-038
Which two items can legally be contained within a Java class declaration?
A) An import statement
B) A field declaration
C) A package declaration
D) A method declaration
Answer : B, D
Q-039
class Mid{
public int findMid(int n1, int n2){
return (n1+n2)/2;
}
}
public class Calc extends Mid{
public static void main(String[] args) {
int n1=22, n2=2;
//insert code here
System.out.println(n3);
}
}
Which two code fragments, when inserted at //insert code here, enable the code to compile and print 12?
A) Calc c=new Calc();
int n3=c.findMid(n1,n2);
B) int n3=super.findMid(n1,n3);
C) Calc c=new Mid();
int n3=c.findMid(n1, n2);
D) Mid m1=new Calc();
int n3=m1.findMid(n1, n2);
E) int n3=Calc.findMid(n1, n2);
Answer : A, D
Q-40
Which three are advantages of the Java exception mechanism?
A) improves the program structure because the error handling code is separated from the normal program function.
B) provides a set of standard exceptions that covers all the possible errors.
C) improves the program structure because the programmer can choose where to handle exceptions
D) improves the program structure because exceptions must be handle in the method in which they occurred
E) allows the creation of new exceptions that are tailored to the particular program being created
Answer : A, C, E