码迷,mamicode.com
首页 > 其他好文 > 详细

ocjp 考试题之四

时间:2018-07-05 18:22:59      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:except   考试题   pat   put   对象   void   fail   ring   this   

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:

  1. class ClassA {}
  2. class ClassB extends ClassA {}
  3. class ClassC extends ClassA {} and:
  4. ClassA p0 = new ClassA();
  5. ClassB p1 = new ClassB();
  6. ClassC p2 = new ClassC();
  7. ClassA p3 = new ClassB();
  8. 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. public class MainClass {

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

  4. }

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)

ocjp 考试题之四

标签:except   考试题   pat   put   对象   void   fail   ring   this   

原文地址:http://blog.51cto.com/2096101/2136773

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!