两道不明白的SCJP试题,请指教!

charyman 2003-08-21 11:12:50
QUESTION NO: 14
Given:
1. package test1;
2. public class Test1 {
3. static int x = 42;
4. }
1. package test2;
2. public class Test2 extends test1.Test1 {
3. public static void main(String[] args) {
4. System.out.println(“x = “ + x);
5. }
6. }
What is the result?
A. x = 0
B. x = 42
C. Compilation fails because of an error in line 2 of class Test2.
D. Compilation fails because of an error in line 3 of class Test1.
E. Compilation fails because of an error in line 4 of class Test2.
此题选C,但执行结果是D。为何?

QUESTION NO: 23
Which statement is true about assertion in the Java programming language?
A. Assertion expressions should not contain side effects.
B. Assertion expression values can be any primitive type.
C. Assertion should be used for enforcing preconditions on public methods.
D. An AssertionError thrown as a result of a failed assertion should always be handled by
the enclosing method.
此题选A,为何D答案不正确?

以上两题请各们大大指教!
...全文
36 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
luoyuanfan 2003-08-26
  • 打赏
  • 举报
回复
还有楼主你做的是什么题.能不能给我一份?
luo_yuan_fan@163.com
谢了!
luoyuanfan 2003-08-26
  • 打赏
  • 举报
回复
第一题的编译结果(用jdk1.4):
Test2.java:4: x is not public in test1.Test1; cannot be accessed from outside package
System.out.println("x =" + x);
^
定义X为public后运行正确.
这怎么解释,难道是因为你们用的是1.2版吗?

rushfly 2003-08-26
  • 打赏
  • 举报
回复
结果是E!!

diewikwang兄一语中地!!
diewikwang 2003-08-24
  • 打赏
  • 举报
回复
我觉得第一题选e的原因是因为x的限制类型是default,也就是在同一个package里面
才能够访问此变量,Test2 继承了 Test1,但是并不是在同一个包,所以无法直接访问
Test1.x
charyman 2003-08-23
  • 打赏
  • 举报
回复
第一题的执行结果是E啊!
charyman 2003-08-23
  • 打赏
  • 举报
回复
第二题并没说抛出的是exception类型的错误啊?
cshchina 2003-08-23
  • 打赏
  • 举报
回复
e
diewikwang 2003-08-22
  • 打赏
  • 举报
回复
我怎么觉得第一题应该选e?
已经显式的 extends test1.Test1应该就不用import了。
wbuyun 2003-08-22
  • 打赏
  • 举报
回复
帮你翻译一下。以前看的原文忘了,说个大概吧
a:断言不能对程序有副作用
c:断言应该被用来对公共方法的条件进行界定
意思就是这样了
第一题三只眼兄说的很对了
jayqian 2003-08-21
  • 打赏
  • 举报
回复
1.没有import test1包
duke7y 2003-08-21
  • 打赏
  • 举报
回复
顺便问一下,a和c怎样翻译
duke7y 2003-08-21
  • 打赏
  • 举报
回复
1题是两个文件。我编译了没错,是选c
2。assertion是当值为假时抛出一个assertionerro.程序中断,而不是exception
dazui 2003-08-21
  • 打赏
  • 举报
回复
帮你顶一下

50,526

社区成员

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

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