SCJP 5.0 习题 第02章
一、重写和重载 ................................................................... 1 二、封装、IS-A HAS-A.............................................................. 5 四、第二章课后习题 ............................................................... 7
一、重写和重载
问题 1)假设有如下类定义,如下哪些方法可以合法放置在“//Here”的注释之后? public class Rid {
public void amethod (int i, String s) {} // Here }
1) public void amethod (String s, int i) {} 2) public int amethod (int i, String s) {}
3) public void amethod (int i, String mystring) {} 4) public void Amethod (int i, String s) {} 答案:1和4<考察方法重载>
Amethod 中的大写字母A 意味着这是不同的方法。
问题 2)假设有如下类定义,哪些代码可以被合法放置在注释“//Here”之后? class Base {
public Base (int i) {} }
public class MyOver extends Base {
public static void main (String arg []) {
MyOver m = new MyOver (10); }
MyOver (int i) {
super (i); }
MyOver (String s, int i) {
this (i); //Here } }
1) MyOver m = new MyOver (): 2) super ();
3) this (“Hello”, 10); 4) Base b = new Base (10); 答案:4
任何this 或super 的调用都必须是构造函数中的第一行。由于方法已经调用了this,不能有 别的调用插入了。
问题 3)假设有如下类定义
class Mammal { Mammal () {
System.out.println (“Mamml”); } }
class Dog extends Mammal { Dog () {
System.out.println (“Dog”); } }
public class Collie extends Dog {
public static void main (String argv []) { Collie c = new Collie (); }
Collie () {
this (“Good Dog”);
System.out.println (“Collie”); }
Collie (String s) {
System.out.println (s); } }
将会输出什么?
1) Compile time error
2) Mammal, Dog, Good Dog, Collie 3) Good Dog, Collie, Dog, Mammal 4) Good Dog, Collie
答案:2) Mammal, Dog, Good Dog, Collie
问题 4)下面哪些论述是正确的? 1) 构造方法不能被继承
2) 构造方法可以被重写;《重写不是重载》. 3) 父类的构造方法可以使用this调用 4) 任何方法都可以调用this和super 答案:1
问题 5)试图编译并运行下面代码会发生什么? class Base {
public void amethod (int i, String s) {
System.out.println (“Base amethod”); }
Base () {
System.out.println (“Base Constructor”); } }
public class Child extends Base {
int i;
String Parm = “Hello”;
public static void main (String argv []) {
Child c = new Child (); c.amethod (); }
void amethod (int i, String Parm) {
super.amethod (i, Parm); }
public void amethod () {}
}
1) 编译错误
2) 错误,super.amethod (i, Parm)语法不正确 3) 输出 “Base Constructor”
4) 错误,super.amethod的参数名字不正确 答案:1
这会导致一个错误,意思是说“你不能重写方法使其访问权限更靠近私有”。基类的amethod 版本被明确的标注为public,但是在子类中没有标识符。好了,所以这不是在考察你的构造 函数重载的知识,但是他们也没在考试中告诉你主题。若这段代码没有省略关键字public, 将会输出“Base constructor”,选项3。
问题 6)试图编译并运行如下代码时将发生什么? class Mammal { Mammal () {
System.out.println (“Four”); }
public void ears () {
System.out.println (“Two”); } }
class Dog extends Mammal { Dog () {
super.ears ();
System.out.println (“Three”); } }
public class Scottie extends Dog {
public static void main (String argv []) {
System.out.println (“One”); Scottie h = new Scottie (); } }
1) One, Three, Two, Four 2) One, Four, Three, Two 3) One, Four, Two, Three 4) Compile time error
答案:3)One, Four, Two, Three
类是从层次的根部往下创建的。因此,首先输出One,因为它在Scottie h 初始化之前创建。
然后,JVM 移动到层次的基类,运行“祖父类”Mammal 的构造函数。这会输出“Four”。 然后,运行Dog 的构造函数。Dog 的构造函数调用Mammal 中的ears 方法,因此输出“Two”。 最后,Dog 的构造函数完成,输出“Three”。
问题7)给定下面的类定义 public class Upton{
public static void main(String argv[]){ }
public void amethod(int i){} //Here }
下面哪一个在替换//Here 后是合法的? 1) public int amethod(int z){}
2) public int amethod(int i,int j){return 99;} 3) protected void amethod(long l){ } 4) private void anothermethod(){} 答案:2、3和4
选项1 由于两个原因不会被编译。第一个相当明显,因为它要求返回一个integer。另一个是 试着直接在类内部重新定义一个方法。把参数的名字从i 换成z 是无效的,并且一个方法不 能在同一个类里重写。
问题8)给定下面的类定义 class Base{
public void amethod(){
System.out.println(\} }
public class Hay extends Base{
public static void main(String argv[]){
Hay h = new Hay(); h.amethod(); } }
下面在类Hay 中的哪一个方法将会编译并使程序打印出字符串\? 1) public int amethod(){ System.out.println(\
2) public void amethod(long l){ System.out.println(\3) public void amethod(){ System.out.println(\4) public void amethod(void){ System.out.println(\答案:3) public void amethod(){ System.out.println(\
选项3 重写<覆盖>了类Base 的方法,因此任何无参数调用都调用这个版本。 选项1 修改方法的返回值类型。
选项2 将会编译,结果是调用父类中的方法
问题9)给定下面的类定义 public class ShrubHill{
public void foregate(String sName){} //Here
}
下面的哪一个方法可以合法的直接替换//Here? 1) public int foregate(String sName){}
2) public void foregate(StringBuffer sName){} 3) public void foreGate(String sName){} 4) private void foregate(String sType){} 答案9)
2) public void foregate(StringBuffer sName){} 3) public void foreGate(String sName){}
选项1 是试着定义一个方法两次,有一个int 返回值并不能帮助将它与存在的foregate 方法 相区分。而像选项4 那样改变方法的参数名,也不能与存在的方法相区分。注意,选项3 里的foreGate 方法有一个大写的G。
二、封装、IS-A HAS-A
问题1)假设你被给予如下设计
“一个人有姓名,年龄,地址和性别。你将要设计一个类来表示一类叫做病人的人。这
种人可以被给予诊断,有配偶并且可能活着”。假设表示人的类已经创建了,当你设计病人 类时如下哪些应该被包含在内? 1) registration date 2) age 3) sex
4) diagnosis 答案1)
1) registration date 2) diagnosis
对于病人来说,注册日期是一个合理的添加域,并且设计明确地指出病人应该有诊断报告。 由于病人是人的一种,它应该有域age 和sex(假设它们没有被声明为私有的)。
问题 2)当你试图编译并运行如下代码时会发生什么? class Base { int i = 99;
public void amethod () {
System.out.println (“Base.amethod ()”); }
Base () {
amethod (); } }
public class RType extends Base { int i = -1;
public static void main (String argv []) {
Base b = new RType (); System.out.println (b.i); b.amethod (); }
public void amethod () {