切換
舊版
前往
大廳
主題

OCA習題(一)

我也太廢了吧 | 2020-07-26 08:45:09 | 巴幣 0 | 人氣 264

SELF TEST

前言:
在其他人網誌或部落格上也有看過類似的文章
就單純想把練習OCA的習題與自己寫的中解答放上去紀錄而已。
習題分六個部分,所以會發個六篇吧?
出現中文全都是自己的理解翻作中文
或註解出來的
所以感覺會很多錯誤
暑假結束後才可能會找到人問
如果有熱心的網友發現我的諸多錯誤
到時候請不吝指教,謝謝


Which are true? (Choose all that apply.)

A. “X extends Y” is correct if and only if X is a class and Y is an interface
B. “X extends Y” is correct if and only if X is an interface and Y is a class
C. “X extends Y” is correct if X and Y are either both classes or both interfaces
D. “X extends Y” is correct for all combinations of X and Y being classes and/or interfaces

C is correct.
“X extends Y”他們彼此都介面或者彼此都是類別,不能一邊是介面,一邊是類別。
A錯在他們不能擴充類別實作介面,B錯在這些介面只能"繼承自"其他的介面,D同上述
A is incorrect because classes implement interfaces, they don’t extend them. B is incorrect
because interfaces only “inherit from” other interfaces. D is incorrect based on the preceding rules.
(OCA Objective 7.5)

2. Given:

01: class Rocket{
02: private void blastOff() { System.out.print("bang "); }
03: }
04: public class Shuttle extends Rocket {
05: public static void main(String[] args) {
06: new Shuttle().go();
07: }
08: void go() {
09: blasstOff();
10: // Rocket.blastOff(); //line A
11: }
12: private void blastOff() { System.out.print("sh-bang "); }
13: }


Which are true? (Choose all that apply.)
A. As the code stands, the output is bang
B. As the code stands, the output is sh-bang
C. As the code stands, compilation fails
D. If line A is uncommented, the output is bang bang
E. If line A is uncommented, the output is sh-bang bang
F. If line A is uncommented, compilation fails

B和F是對的,自從Rocket.blastOff()是private,他不能複寫,他也不能存取class Shuttle
B and F are correct. Since Rocket.blastOff() is private, it can’t be overridden, and it
is invisible to class Shuttle.
A, C, D, and E are incorrect based on the above. (OCA Objective 6.4)

3. Given that the for loop’s syntax is correct, and given:

import static java.lang.System.*;
class _ {
static public void main(String[] __A_V_){
String $ = "";
for(int x=0; ++x < __A_V_).length;) //for loop
$ += __A_V_[x];
out.println($);
}
}
What is the result?
A. -A
B. A.
C. -A.
D. _A.
E. _-A.
F. Compilation fails
G. An exception is thrown at runtime

And the command line:
java _ - A.

B是正確的,雖然符號很奇怪,但就能跑
B is correct. This question is using valid (but inappropriate and weird) identifiers, static
imports, main(), and pre-incrementing logic. (Note: You might get a compiler warning when
compiling this code.)
A, C, D, E, F, and G are incorrect based on the above. (OCA Objective 1.2)

4. Given:

01: enum Animals {
02: DOG("woof"), CAT("meow"), FISH("burble");
03: String sound;
04: Animals(String s) { sound = s;}
05: }
06: class TestEnum {
07: staatic Animals a;
08: public static void main(String[] args) {
09: System.out.println(a.DOG.sound + "" +a.FISH.sound);
10: }
11: }


What is the result?
A. woof burble
B. Multiple compilation errors
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 9


eunms(列舉)可以有建構子跟變數,因此選A
A is correct; enums can have constructors and variables.
B, C, D, E, and F are incorrect; these lines all use correct syntax. (OCA Objective 1.2)

5. Given two files:

01: package pkgA;
02: public class Foo {
03: int a = 5;
04: protected int b =6
05: public int c= 7;
06: }

03: package pkgB;
04: import pkgA.*;
05: public class Baz {
06: public statiic void main(String[] args) {
07: Foo f = new Foo();
08: System.out.print(" " + f.a);
09: System.out.print(" " + f.b);
10: System.out.println(" " + f.c);
11: }
12: }


What is the result? (Choose all that apply.)
A. 5 6 7
B. 5 followed by an exception
C. Compilation fails with an error on line 7
D. Compilation fails with an error on line 8
E. Compilation fails with an error on line 9
F. Compilation fails with an error on line 10

a已經默認存取了,他不能被外面的套件存取,變數b已經被設為protected,所以他只能在
pkgA內存取,所以答案是DE。
D and E are correct. Variable a has default access, so it cannot be accessed from outside
the package. Variable b has protected access in pkgA.
A, B, C, and F are incorrect based on the above information. (OCA Objectives 1.4 and 6.5)

6. Given:

1: public class Electronic implements Device
2: { public void doIt() { } }
3: abstract class Phonel extends Electronic { }
4:
5: abstract class Phonel2 extends Electronic
6: { pubic  void doI(int x) { } }
7: class Phone3 extends Electronic implements Device
8: {public void doSuff() { } }
9: interface Device { public  void doIt(); }

What is the result? (Choose all that apply.)
A. Compilation succeeds
B. Compilation fails with an error on line 1
C. Compilation fails with an error on line 3
D. Compilation fails with an error on line 5
E. Compilation fails with an error on line 7
F. Compilation fails with an error on line 9

上面這些全都可以正常編譯喔
A is correct; all of these are legal declarations.
B, C, D, E, and F are incorrect based on the above information. (OCA Objective 7.5)

7. Given:

04: class Announce {
05: public static void main(String[] args) {
06: for(int __x =0; __X<3; __x++) ;
07: int #1b = 7;
08: long [] x [5];
09: Boolean [] ba[];
10: }
11: }

What is the result? (Choose all that apply.)
A. Compilation succeeds
B. Compilation fails with an error on line 6
C. Compilation fails with an error on line 7
D. Compilation fails with an error on line 8
E. Compilation fails with an error on line 9

變數名稱不能用#開頭,陣列宣告不能包括沒有實例化的大小,其他的都對
C and D are correct. Variable names cannot begin with a #, and an array declaration can’t
include a size without an instantiation. The rest of the code is valid.
A, B, and E are incorrect based on the above. (OCA Objective 2.1)

8. Given:

03: public class TestDays {
04: public enum Days { MON, TUE, WED };
05: public static void main(String[] args) {
06: for(Days d : Days.values() )
07: ;
08: Days [] d2 = Days.values();
09: System.out.println(d2[2]);
10: }
11: }

What is the result? (Choose all that apply.)
A. TUE
B. WED
C. The output is unpredictable
D. Compilation fails due to an error on line 4
E. Compilation fails due to an error on line 6
F. Compilation fails due to an error on line 8
G. Compilation fails due to an error on line 9

在enum(列舉)中每個值都帶有values()方法,會回傳enum裡面的值
B is correct. Every enum comes with a static values() method that returns an array of
the enum’s values in the order in which they are declared in the enum.
A, C, D, E, F, and G are incorrect based on the above information. (OCP Objective 1.2)

9. Given:

04: public class Frodo extends Hobbit {
05: public static void main(String[] args) {
06: int myGold =7;
07: System.out.println(countGold(myGold, 6));
08: }
09: }
10: class Hobbit {
11: int countGold(int x,int y) { return x+y; }
12: }

What is the result?
A. 13
B. Compilation fails due to multiple errors
C. Compilation fails due to an error on line 6
D. Compilation fails due to an error on line 7
E. Compilation fails due to an error on line 11

第七行不能被調用,第11行要加static然後用Hobbit.countGold()才會對
D is correct. The countGold() method cannot be invoked from a static context.
A, B, C, and E are incorrect based on the above information. (OCA Objective 6.2)

10. Given:

interface Gadget {
void dostuff();
}
abstract class Electrinic {
void getPower() { System.out.print("plug in "); }
}
public class Tablet extends Electrinic implements Gadget{
void dostuff() { System.out.print("show book "); }
public static void main(String[] args) {
new Tablet().getPower();
new Tablet().dostuff();
}
}

Which are true? (Choose all that apply.)
A. The class Tablet will NOT compile
B. The interface Gadget will NOT compile
C. The output will be plug in show book
D. The abstract class Electronic will NOT compile
E. The class Tablet CANNOT both extend and implement

介面方法是在預設情況下是設public的,所以Tablet.doStuff也要設public
其他的code有效
A is correct. By default, an interface’s methods are public so the Tablet.doStuff
method must be public, too. The rest of the code is valid.
B, C, D, and E are incorrect based on the above. (OCA Objective 7.5)

11. Given that the Integer class is in the java.lang package and given:

1: // insert code here
2: class StatTest {
3: public static void main(String[] args) {
4: System.out.println(Integer.MAX_VALUE);
5: }
6: }

Which, inserted independently at line 1, compiles? (Choose all that apply.)
A. import static java.lang;
B. import static java.lang.Integer;
C. import static java.lang.Integer.*;
D. static import java.lang.Integer.*;
E. import static java.lang.Integer.MAX_VALUE;
F. None of the above statements are valid import syntax

C跟E是使用static import正確的語法,第四行沒有使用static import
所以code雖然有經過編譯但沒有import進去
C and E are correct syntax for static imports. Line 4 isn’t making use of static imports,
so the code will also compile with none of the imports.
A, B, D, and F are incorrect based on the above. (OCA Objective 1.4)

12. Given:
interface MyInterface {
// insert code here
}
Which lines of code—inserted independently at insert code here—will compile? (Choose all
that apply.)
A. public static m1() {;}
B. default void m2() {;}
C. abstract int m3();
D. final short m4() {return 5;}
E. default long m5();
F. static void m6() {;}

B,C,F是正確的,在java8介面可以有default和static方法,A沒有回傳型態,D不能建立一個方法架構,E需要一個方法架構

B, C, and F are correct. As of Java 8, interfaces can have default and static methods.
A, D, and E are incorrect. A has no return type; D cannot have a method body; and E needs a
method body. (OCA Objective 7.5)

13:
Which are true? (Choose all that apply.)
A. Java is a dynamically typed programming language
B. Java provides fine-grained control of memory through the use of pointers
C. Java provides programmers the ability to create objects that are well encapsulated
D. Java provides programmers the ability to send Java objects from one machine to another
E. Java is an implementation of the ECMA standard
F. Java’s encapsulation capabilities provide its primary security mechanism

Java是靜態的程式語言,他沒有提供指標,ECMA是JavaScript的標準,java主要的安全機制是由bytecode與JVM提供,並非封裝
C and D are correct.
A is incorrect because Java is a statically typed language. B is incorrect because it does not
provide pointers. E is incorrect because JavaScript is an implementation of the ECMA standard,
not Java. F is incorrect because the use of bytecode and the JVM provide Java’s primary security
mechanisms.

創作回應

更多創作