有几道练习题不懂,请教大家,谢谢。

HanDongLin 2004-08-15 02:46:15

大家给我解释一下这几道题。
虽然都有答案,但是答案不一定对。
package com.abe.pkg1;

public class ClassOne{
private char var = 'a';
char getVar(){ return var;}
}

package com.abe.pkg2;

improt com.abc.pkg1.ClassOne;
public class ClassTest extends ClassOne{
public static void main(String[] args){
char a = new ClassOne().getVar();
char a = new ClassTest().getVar();
}
}
what's the result?
a.compilation fails
b.compilation succeeds and no exceptions are

thrown.
c.an exception is thrown at line 5 in

ClassTest.java
答案是A 请解释一下

class TestA{
TestB b;
TestA(){
b = new TestB(this);
}
}
class TestB{
TestA a;
TestB(TestA a){
this.a = a;
}
}

class TestAll{
public static void main(String args[]){
new TestAll().makeThings();
// code on...
}
void makeThings(){
TestA test = new TestA();
}
}
Question:
which two statements are true after line15 ?
a.line 15 causes a stack overflow.
b.an exception is thrown at runtime.
c.the object referenced by a is eligible for

garbage collection.
d.the object referenced by b is eligible for

garbage collection.
答案c d 请解释。

public class Test{
public void foo(){
assert false;
assert false;
}
public void bar(){
while(true){
assert false;
}
assert false;
}
}
what causes compilation to fail?
a.line 13
b. line 14
c. line 18
d. line 20
答案 D 请解释

Given that b and c refer to instances of wrapper

classes, which tow statements are true?
a. b.equals(b) returns true.
b. b.equals(c) returns the same result as b==c
c. b.equals(c) can return false even if

c.equals(b) returns true.
d. b.equals(c) throws an exception if b and c

are different wrpper types.
e. b.equals(c) returns fals if the type of

wrpper objects being compared are different.
答案:b,c 请解释

which statement si true?
a. The Error class is a runtimeException.
b. No exceptions are subclases of Error.
c. Any staement taht may throw an Error must be enclosed in a try block.
d. Any statement that may throw an Exception must be enclosed in a try block.
e. Any statement that may throw a runtimeException must be enclosed in a try block
答案d 我怎么觉得b才对
...全文
247 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
geleisi 2004-08-16
  • 打赏
  • 举报
回复
mark
runmanoo 2004-08-16
  • 打赏
  • 举报
回复
up
睿思BI 2004-08-16
  • 打赏
  • 举报
回复
1,因为两个类在不同的包中,而char getVar(){ return var;} 方法的访问修饰是默认的,只具有包可见性,在别人包中就能访问了,应该改成 public char getVar(){ return var;}

2,当TestAll对象调用makeThings()方法时,开始调用TestA的构造函数,TestA用当前对象做参数构造TestB对象对testA的成员b进行初始化,完成后test引用TestA类的对象,TestA类的对象成员TestA类的对象。
当test引用结束后,TestA类的对象和TestA类的对象变的符合垃圾收集。。

3,不清楚,该段代码会直接抛出断言的异常,因为断言是FALSE

4,b.equals(b) return true; //OK!!
b==c,b.equals(c) returns true;
c. b.equals(c) can return false even if c.equals(b) returns true.//该是false把,
5, Error 和 runtimeException 不需需要捕获,Error 有子类把,



hibernateTest 2004-08-16
  • 打赏
  • 举报
回复
有学习hibernate的人吗?
交个朋友,共同进步!!!!1
QQ:110736012
octopus1 2004-08-15
  • 打赏
  • 举报
回复
第一个,public class ClassOne的char var 是private,不能被他的子类调用.
ransion 2004-08-15
  • 打赏
  • 举报
回复
最后一个,runtimeException 不需要捕捉
ransion 2004-08-15
  • 打赏
  • 举报
回复
第一个,char a 声明两次
HanDongLin 2004-08-15
  • 打赏
  • 举报
回复
顶一下,求高手们帮帮忙吧。
HanDongLin 2004-08-15
  • 打赏
  • 举报
回复
第二题TestA对象和TestB对象之间是什么关系啊。
当运行TestA test = new TestA();的时候,内部情况是怎么样的?
HanDongLin 2004-08-15
  • 打赏
  • 举报
回复
第一题明白了,请各位继续。
ransion 2004-08-15
  • 打赏
  • 举报
回复
问题是char getVar(){ return var;}默认的是friendly只有同一个包才能方位
ema 2004-08-15
  • 打赏
  • 举报
回复
第一题 char getVar(){ return var;}缺省修饰符为friendly,即只有包内才能访问,所以编译不过。不同包内则无法访问了。必须在前面加protected 或者public。
HanDongLin 2004-08-15
  • 打赏
  • 举报
回复
private char var = 'a';
char getVar(){ return var;}

虽然 char var是private的,但是它有一个访问方法getVar()返回var啊。
HanDongLin 2004-08-15
  • 打赏
  • 举报
回复
不好意思,第一题打错了,main里面的声明应该一个是a 一个是b
char a = new ClassOne().getVar();
char b = new ClassTest().getVar();

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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