OCJP 1Z0-808考题超详细解析(word文档) 题50--60

我现在边工作,业余时间看看,更新的可能的比较慢,望大家谅解。

题58

Given the code fragment: 

if(aVar++ < 10){

System.out.println(aVar+ " Hello World!");

}else{

System.out.println(aVar+" Hello Universe!");

}

What is the result if the integer aVar is 9?
A. 10 Hello World!
B. Hello Universe!
C. Hello World!
D. Compilation fails.

-------------------------------------------------------------

Answer: A 

解析:1. if(aVar++ < 10) : aVar是先判断后自增,此时aVar是9。

2. System.out.println(aVar+ " Hello World!") aVar已经自增过了,所以是10

题59

Given:

class X {

public void mX() {

System.out.println("Xm1");

}

}

 

class Y extends X{

public void mX() {

System.out.println("Xm2");

}

public void mY() {

System.out.println("Ym");

}

}

 

public class Test{

public static void main(String[] args) {

X xRef = new Y();

Y yRef=(Y)xRef;

yRef.mY();

xRef.mX();

}

}

A. Ym
Xm2
B. Ym
Xm1
C. Compilation fails
D. A ClassCastException is thrown at runtime

-------------------------------------------------------------

Answer: B 

解析:1. 由于Y yRef=(Y)xRef;把父类强转为子类了。所以yRef.mY();没有问题。排除选项C和选项D。

2. xRef.mX(); 多态情况下,子父类存在同名的非静态的成员函数时,访问的是子类的成员函数。1z0-808.236q.pdf文档有误。这题正确的选项是A。

猜你喜欢

转载自blog.csdn.net/aoe15yu/article/details/82808899