您的位置:首页 > 其它

ocjp 考试题之四

2021-01-11 22:36 302 查看

OCJP视频课堂,具体讲解:https://edu.csdn.net/course/detail/7811


QUESTION 73 Given:

10: public class Hello {

11: String title;

12: int value;

13: public Hello() {

14: title += " World";

15: }

16: public Hello(int value) {

17: this.value = value;

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)


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: