又有新问题:D?

ghhcld 2003-04-28 11:20:39
QUESTION NO 140
Which two demonstrate encapsulation of data? (Choose Two)
A. Member data have no access modifiers.
B. Member data can be modified directly.
C. The access modifier for methods is protected.
D. The access modifier to member data is private.
E. Methods provide for access and modification of data.
Answer: D, E
其他为什么不对?
...全文
98 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
ghhcld 2003-04-29
  • 打赏
  • 举报
回复
对不起啊,我好苯!
第一题还没理解,
“如果象答案D中所说的只是设置的变量radius的话,那么不会影响diameter”
你是说D也是对的吗?
如果是你,考试时到底会选什么?

还有17题,你的意思是不是如果改成
class Base {}

class Sub extends Base {}

class Sub2 extends Base {}

public class CEx{

public static void main(String argv[]){

Base b=new Sub();

Sub s=(Sub) b;

}

}
就没有异常抛出?
ervinlj 2003-04-29
  • 打赏
  • 举报
回复
17题,你这样来理解是对的!!!
关于变量能不能被修改的问题,我已经讲的很清楚了,自己再动动脑筋!!!
xch28 2003-04-29
  • 打赏
  • 举报
回复
to 17:因为变量radius 和 diameter都是声明为public的,可以直接用xxx.radius = xxx;来赋值而不改变diameter的值。
ervinlj 2003-04-28
  • 打赏
  • 举报
回复
A.没有access modifiers,就是default(friendly)在本包内能够访问,在封装数据的类外能访问,打破封装。
B.数据成员可以被直接修改,这也打破了封装。最好是象E中所说的那样!!!
C.Protected access modifiers导致在包外的这个类的subclass也能访问得到,It also be destroyed encapsulation
ervinlj 2003-04-28
  • 打赏
  • 举报
回复
虽然有return语句,但是已经失去了对真正对象new Float(3.14F)的引用!这时就要Garbage Collect了!!!如果对象没有失去引用,可以在程序结束后,为每个对象调用一次Garbage Collector的。明白了吗?
ervinlj 2003-04-28
  • 打赏
  • 举报
回复
(104-102)(147-74).public class MyCircle{
public double radius;
public double diameter;
public void setRadius(double radious){
6) this.radius=radius;
7) this.diameter=radius*2;}
public double getRadius(){
return radius;}
}
A. The MyCircle class is fullly encapsulated
B. The diameter of a given MyCircle is guaranteed to be twice its radius.
C. Line 6 and 7 should be in a Synchronized block to ensure encapsulation.
D. The radius of a MyCircle object can be set without affecting its diameter
这道题目我觉得应该是这样的,因为运行方法setRadius了后,diameter变量肯定要改变,如果象答案D中所说的只是设置的变量radius的话,那么不会影响diameter。
不知我这样说,你理解了吗?

Question 17:
这是Runtime Polymorphism的问题,因为在程序中对象b指向的是父类Base,如果你将一个对象cast到一个子类的话,而这个对象又不是子类的实例,那么程序会抛出ClassCastException异常。

Question 57:
BitSet可以象Vector of bit(其实就是我们所知道的向量Vector)那样可以被扩展,其中的每个Component都存储boolean类型的值,当然是非on即off的信息拉!

最后一题:
Object o=new float(3.14F);
Line6:o=null;
对象o失去对Wrapper Class Float(先忽略题目中的大小写问题)的引用,这时Garbage Collector就可以考虑开始垃圾收集了,何时真正开始收集由JVM来决定。

ghhcld 2003-04-28
  • 打赏
  • 举报
回复
但是最后有个return语句,仍然要调用o,即使是null,也要等程序结束才能收集!
我觉得我的有道理!
huiyang 2003-04-28
  • 打赏
  • 举报
回复
我觉得选3),line7已经使这个对象失效了
ghhcld 2003-04-28
  • 打赏
  • 举报
回复
对不起啊,还有一题!
Given:

1. public class X {
2. public object m () {
3. object o = new float (3.14F);
4. object [] oa = new object [1];
5. oa[0]= o;
6. o = null;
7. oa[0] = null;
8.return o;
9. }
10. }

When is the float object created in line 3, eligible for garbage collection?

1)Just after line 5
2)Just after line 6
3)Just after line 7
4)Just after line 8(that is, as the method returns)

答案有选3)的,有选4)的,我觉得选4),到底是什么呢?
ghhcld 2003-04-28
  • 打赏
  • 举报
回复
谢谢阳光,还有几个问题望赐教!

(104-102)(147-74).public class MyCircle{
public double radius;
public double diameter;
public void setRadius(double radious){
6) this.radius=radius;
7) this.diameter=radius*2;}
public double getRadius(){
return radius;}
}
A. The MyCircle class is fullly encapsulated
B. The diameter of a given MyCircle is guaranteed to be twice its radius.
C. Line 6 and 7 should be in a Synchronized block to ensure encapsulation.
D. The radius of a MyCircle object can be set without affecting its diameter
Answer:B or D
你的答案是:
“我个人认为答案应该是B,如果设置了radius的话,显然要影响到MyCircle中的实例变量diameter.”

但是刚才你楼上的那个人说是选D,但没说原因,只说他是对的!!
我想知道最后答案!




Question 17)

What will happen if you attempt to compile and run the following code?



1) Compile and run without error

2) Compile time Exception

3) Runtime Exception





class Base {}

class Sub extends Base {}

class Sub2 extends Base {}

public class CEx{

public static void main(String argv[]){

Base b=new Base();

Sub s=(Sub) b;

}

}

Answer to Question 17
Answer:3
为什么会出现这样的问题?我觉得程序是对的啊!


Question 57)

Which of the following most closely describes a bitset collection?



1) A class that contains groups of unique sequences of bits

2) A method for flipping individual bits in instance of a primitive type

3) An array of boolean primitives that indicate zeros or ones

4) A collection for storing bits as on-off information, like a vector of bits



Answer to Question 57
Answer:4
这个知识点我上课时没接触到,能解释一下吗?
谢谢!!

51,407

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧