ocjp 考試題之四

OCJP視頻課堂,具體講解:https://edu.csdn.net/course/detail/7811
java


QUESTION 73 Given:app

10: public class Hello {ide

11: String title;this

12: int value;spa

13: public Hello() {.net

14: title += " World";code

15: }視頻

16: public Hello(int value) {對象

17: this.value = value;three

18: title = "Hello";

19: Hello();

20: }

21: } and:

30: Hello c = new Hello(5); 31: System.out.println(c.title);

What is the result?

A.   Hello

B.   Hello World

C.   Compilation fails.

D.   Hello World 5

E.   The code runs with no output.

F.    An exception is thrown atruntime.

Answer: C

19行代碼,不能這樣使用,必須是this.Hello();若是直接使用,則必須是靜態方法。

QUESTION 76
Given:
1. public class Blip {
2. protected int blipvert(int x) { return 0; }
3. }
4. class Vert extends Blip {
5. // insert code here
6. }
Which five methods, inserted independently at line 5, will compile? (Choose five.)
A. public int blipvert(int x) { return 0; }
B. private int blipvert(int x) { return 0; }
C. private int blipvert(long x) { return 0; }
D. protected long blipvert(int x) { return 0; } 重載與返回類型無關
E. protected int blipvert(long x) { return 0; }
F. protected long blipvert(long x) { return 0; }
G. protected long blipvert(int x, int y) { return 0; }
Answer: A,C,E,F,G

重寫和重載。A是重寫,C,E,F,G是重載。

QUESTION 77 
Given: 
1. class Pizza { 
2. java.util.ArrayList toppings; 
3. public final void addTopping(String topping) { 常量方法 
4. toppings.add(topping); 
5. } 
6. } 
7. public class PepperoniPizza extends Pizza { 
8. public void addTopping(String topping) { 
9. System.out.println("Cannot add Toppings"); 
10. } 
11. public static void main(String[] args) { 
12. Pizza pizza = new PepperoniPizza(); 
13. pizza.addTopping("Mushrooms"); 
14. } 
15. } 
What is the result? 
A. Compilation fails. 
B. Cannot add Toppings 
C. The code runs with no output. 
D. A NullPointerException is thrown in Line 4. 
Answer: A 

public final void addTopping(String topping) 不容許重寫。

java中當方法被final修飾就表示它是不可被子類重寫的方法。

QUESTION 78 
Given: 
11. class ClassA {} 
12. class ClassB extends ClassA {} 
13. class ClassC extends ClassA {} and: 
21. ClassA p0 = new ClassA(); 
22. ClassB p1 = new ClassB(); 
23. ClassC p2 = new ClassC(); 
24. ClassA p3 = new ClassB(); 
25. ClassA p4 = new ClassC(); 
Which three are valid? (Choose three.) 
A. p0 = p1; 
B. p1 = p2; 
C. p2 = p4; 
D. p2 = (ClassC)p1; 
E. p1 = (ClassB)p3; 
F. p2 = (ClassC)p4; 
Answer: AEF

p2/p4引用的是C類型的對象,p1/p3引用的是B類型的對象,p0引用的是A類型的對象,而B和C是A的子類,根據語言規範,能夠把子類對象賦給父類,A正確,B錯誤。

p4在編譯器看來是A類型的,不能再沒有類型轉換的狀況下將p2 = p4;,C錯誤。

EF類型轉換正確!


QUESTION 80 Given:

1. package com.company.application;

2.

3. public class MainClass {

4. public static voidmain(String[] args) {}

5. }

And MainClass exists in the/apps/com/company/application directory. Assume the CLASSPATH environmentvariable is set to "." (current directory). Which two java commandsentered at the command line will run MainClass? (Choose two.)

A.   java MainClass if run from the/apps directory

B.   javacom.company.application.MainClass if run from the /apps directory

C.   java -classpath /appscom.company.application.MainClass if run from any directory

D.   java -classpath . MainClass ifrun from the /apps/com/company/application directory

E.   java -classpath/apps/com/company/application:. MainClass if run from the /apps directory

F.    javacom.company.application.MainClass if run from the /apps/com/company/applicationdirectory

Answer: BC

Section: (none)

相關文章
相關標籤/搜索