想过Scjp必看,Scjp中几道经典问题,欢迎讨论!

icerhino 2003-03-17 05:21:58
76. Click the exhibit button.
ClassOne.java
1. package com.abc.pkg1;
2. public class ClassOne {
3. private char var = 'a';
4. char getVar() {return var;}
5. }
ClassTest.java
1. package com.abc.pkg2;
2. import com.abc.pkg1.ClassOne;
3. public class ClassTest extends ClassOne {
4. public static void main(String[]args) {
5. char a = new ClassOne().getVar();
6. char b = new ClassTest().getVar();
7. }
8. }
What is the result?
A. Compilation will fail.
B. Compilation succeeds and no exceptions are thrown.
C. Compilation succeeds but an exception is thrown at line 5 in ClassTest.java.
D. Compilation succeeds but an exception is thrown at line 6 in ClassTest.java.

78. Which two statements are true regarding the creation of a
default constructor? (Choose Two)
A. The default constructor initializes method variables.
B. The compiler always creates a default constructor for every
class.
C. The default constructor invokes the no-parameter
constructor of the superclass.
D. The default constructor initializes the instance variables
declared in the class.
E. When a class has only constructors with parameters, the
compiler does not create a default constructor.

101. Which statement is true?
A. The Error class is a untimeException.
B. No exceptions are subclasses of Error.
C. Any statement that 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 thro a runtimeException must be enclosed in a try block.

133. Given:
1. public class ForBar {
2. public static void main(String []args) {
3. int i = 0, j = 5;
4. tp: for (; {
5. i ++;
6. for(;
7. if(i > --j) break tp;
8. }
9. System.out.println("i = " + i + ", j = "+ j);
10. }
11. }
What is the result?
A.The program runs and prints "i=1, j=0"
B.The program runs and prints "i=1, j=4"
C.The program runs and prints "i=3, j=4"
D.The program runs and prints "i=3, j=0"
E.An error at line 4 causes compilation to fail
F.An error at line 7 causes compilation to fail

142. Which two statements are true? (Choose Two)
A.An anonymous inner class can be declared inside of a method.
B.An anonymous inner class constructor can take arguments in some situation.
C.An anonymous inner class that is a direct subclass that is a
direct subclass of Object can implement multiple interfaces .
D.Even if a class Super does not implement any interfaces, it
is still possible to define an anonymous inner class that is
an immediate subclass of Super that implements a single
interface.
E.Event if a class Super does not implement any interfaces, it
is still possible to define an anonymous inner class that is
an immediate subclass of Super that implements multiple interfaces.
...全文
60 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
AndrewCSDN 2003-05-18
  • 打赏
  • 举报
回复
我来谈谈78, 首先看看这个例子:

class Base
{
Base() { System.out.println("hello from base"); }
}

public class Hello extends Base
{
int i;
Hello(int var)
{
//注意这里,不论你自己写这一行, JVM都会给你加上
//super();
i = var;
}
public static void main(String[] args)
{
Hello hl = new Hello(2);
}
}//程序结束

这个程序的结果是打印出 hello from base. 在这个程序中,因为 Hello类 中有带参数的
Consturctor, 它就不会有Default Constructor. 但是这个程序的输出结果证明, Base类
的 no parameter constructor还是被 invoke 了. 这是因为JVM会在Hello类的Constructor
中的第一行自动加入 super(); 它才是真正的用来 invoke Base类中no parameter constuctor.
所以 C: The default constructor invokes the no-parameter
constructor of the superclass 这种说法是不全面的, 因为在上例中, 根本就没有
default constructor.

请大家指正.
AndrewCSDN 2003-05-18
  • 打赏
  • 举报
回复
76 答案肯定是 A 啦.
AndrewCSDN 2003-05-18
  • 打赏
  • 举报
回复
同意.

虽然在只有 default constructor 的情况下, instance variables 是可以被初始化,
但是带参数的 constructor 也可以做 instance variables 初始化.

所以我认为最准确的应该是只有 E

下面是 JVM 执行过程,有兴趣的可以看一看.
http://java.sun.com/docs/books/jls/second_edition/html/execution.doc.html#44411
井中老男孩 2003-05-18
  • 打赏
  • 举报
回复
D. The default constructor initializes the instance variables
declared in the class.

这句话肯定是错的,大家有不同意见吗?
AndrewCSDN 2003-05-18
  • 打赏
  • 举报
回复
看来这道题是在玩文字游戏了.
正如你说的 "并不是只有确省构造函数才会调用父类的无参数构造函数",
如果 C: 变成 The default constructor CAN invoke the no-parameter
constructor of the superclass 就是对的.

井中老男孩 2003-05-18
  • 打赏
  • 举报
回复
AndrewCSDN(),有没有搞错,你的例子没有确省构造函数,自然不在我们讨论之列

看清题意:
确省构造函数会调用父类的无参数构造函数
并不是只有确省构造函数才会会调用父类的无参数构造函数

ervinlj 2003-05-17
  • 打赏
  • 举报
回复
78题的正确答案是CE,稍微没有脑子的人才会选答案DE,理由如下:
default constructor是编译器为你自动添加的,它不会为你做任何事情,请不要把default constructor和no arguments constructor搞起来!!!!!!
井中老男孩 2003-05-16
  • 打赏
  • 举报
回复
我查了查,78题的标准答案是DE
稍微有脑子的人都看的出来,标准答案是错的
井中老男孩 2003-05-16
  • 打赏
  • 举报
回复
When a class has only constructors with parameters, the
compiler does not create a default constructor

这句话的意思是:当一个类仅仅有带参数构造函数时,编译器不会建立确省构造函数。
而不是:只有当一个类仅仅有带参数构造函数时,编译器才不会建立确省构造函数。

所以78题我认为选C E
井中老男孩 2003-05-14
  • 打赏
  • 举报
回复
对,76选A
讨论78和142题
firerabbit 2003-05-14
  • 打赏
  • 举报
回复
同意菲斯
jackyzhao213 2003-05-13
  • 打赏
  • 举报
回复
我赞成菲斯的,顺便解释一下,
ClassOne.java
1. package com.abc.pkg1;
2. public class ClassOne {
3. private char var = 'a';
4. char getVar() {return var;}
5. }
第四句修饰符是default,缺省的适用范围是同一个包,不同包是不能访问的,
ClassTest.java
1. package com.abc.pkg2;
2. import com.abc.pkg1.ClassOne;
3. public class ClassTest extends ClassOne {
4. public static void main(String[]args) {
5. char a = new ClassOne().getVar();
6. char b = new ClassTest().getVar();
7. }
8. }
第5句就编译错误了。import近来也不能访问getVar()方法。
heface 2003-05-13
  • 打赏
  • 举报
回复
76 a 这个没什么疑问,不在一个包的默认方法是无法访问的
78 c e
101 b
133 题目有问题,请重贴
142 a b
oldmaize 2003-05-13
  • 打赏
  • 举报
回复
为什么大家都说76是选b呢,ClassOne和ClassTest不在同一个package里,getVar()又不是public
的,
5. char a = new ClassOne().getVar();
6. char b = new ClassTest().getVar();
这两行编译都会有错啊
「已注销」 2003-05-12
  • 打赏
  • 举报
回复
修正一下,匿名类的构造函数的确是可以带参数的,因为调用的是父类的带参数构造函数
黑马 2003-05-08
  • 打赏
  • 举报
回复
学习
「已注销」 2003-05-08
  • 打赏
  • 举报
回复
to: idilent
78题的答案是什么?


A. The default constructor initializes method variables.

这我就不明白了,java中是没有函数指针的的,所以就不会有方法变量。
那么这里的方法变量是指什么?

另外我知道反射中得到类的方法指针其实是得到封装这个方法指针的Method类,但和这个答案没有什么关系

谁能解惑
「已注销」 2003-05-08
  • 打赏
  • 举报
回复
76:B 这个好象没什么疑问
78:C,E
101:B
133:E
142:A,D

b肯定是错的,因为匿名类只有确省构造函数(构造函数名是类名,你都不知道类名,怎么自定义构造函数)
D是对的,因为任何类都继承了Object类,所以可以实现一个匿名类继承于Object类而同时实现一个接口,当然除了Object类就不行了
DavidBone 2003-04-20
  • 打赏
  • 举报
回复
up
davidweng 2003-03-28
  • 打赏
  • 举报
回复
142:匿名类怎么创建constructor,又怎么使用参数?
加载更多回复(7)

50,523

社区成员

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

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