c) s3=s1 & s2; d) s3=s1 && s2;
8.以下代码能否编译通过,假如能编译通过,运行时得到什么打印结果? public class Conv{
public static void main(String argv[]){ Conv c=new Conv();
String s=new String(\ c.amethod(s); }
public void amethod(String s){ char c='H'; c+=s;
System.out.println(c); } }
[答案] 编译出错,―c+=s‖这行代码不正确。 9.运行以下代码,得到什么打印结果? System.out.println(6+6+ \ System.out.println(\[答案] 打印:12x x66 第5章 流程控制
1. 运行以下代码,得到什么打印结果? int i = 3; int j = 0;
double k = 3.2; if (i < k) if (i == j)
System.out.println(i); else
System.out.println(j); else
System.out.println(k); [答案] 打印:0
2.以下代码能否编译通过,假如能编译通过,运行时得到什么打印结果? int i = 4; switch (i) { default:
System.out.println(\ case 0:
System.out.println(\ break; case 1:
System.out.println(\ case 2:
System.out.println(\[答案] 打印:default zero
6
3.以下哪段代码是合法的? a) int i;
for (i=5,int j=10; i<10;j--) { } b) int i,j;
for (i=0,j=10;i<10, j>0;i++,j--) { } c) int i,k;
for (i=0,k=9;(i<10 && k>0);i++,k--) { } d) int i,j;
for (i=0;j=10;i<10;i++,j--) { }[答案] c 4.运行以下代码,得到什么打印结果? int i = 1; switch (i) { default:
System.out.println(\ case 0:
System.out.println(\ break; case 1:
System.out.println(\ case 2:
System.out.println(\[答案] 打印: one two 5.以下哪段代码是合法的? a) float x = 1; switch(x) { case 1:
System.out.println(\ b) long y =1; switch(y) { case 1:
System.out.println(\ break; }
c) byte x =1; switch(x) { case 1/1:
System.out.println(\ } d) int x=1; int c =1; switch(c) { case x:
System.out.println(\ break; } e) short x=1; switch(x) {
7
case 3.2 /3:
System.out.println(\ break; }
f) short x=1; switch(x) { case 1,2,3:
System.out.println(\ break; }
6.以下代码能否编译通过,假如能编译通过,运行时得到什么打印结果? public class MySwitch{
public static void main(String argv[]){ MySwitch ms= new MySwitch(); ms.amethod(); }
public void amethod(){
for(int a=0,b=0;a<2; b=++a,System.out.println(\ System.out.println(\ } } } [答案] 打印: a=0 b=1 a=1 b=2
7.以下代码能否编译通过,假如能编译通过,运行时得到什么打印结果? int x = 0; one:
while (x < 10) { two:
System.out.println(++x); if (x > 3) break two;}
[答案] 编译出错。―break two‖语句出错。
8.以下代码能否编译通过,假如能编译通过,运行时得到什么打印结果? public class Hope{
public static void main(String argv[]){ Hope h = new Hope(); }
protected Hope(){ int i=1; do{
System.out.println(i);
}while(++i<3); }}[答案] 打印 1 2 第6章 继承
1.继承有哪些优点和缺点?
[答案] 优点:提高程序代码的可重用性;提高系统的可扩展性
缺点:如果继承树非常复杂、或者随便扩展本来不是专门为继承而设计的类,反而会削
8
弱系统的可扩展性和可维护性。 2.继承与组合有哪些异同?
[答案]相同点:组合与继承都是提高代码可重用性的手段。 不同点:组合关系和继承关系相比,前者的最主要优势是不会破坏封装,当类A与类C之间为组合关系时,类C封装实现,仅向类A提供接口。而当类A与类C之间为继承关系时,类C会向类A暴露部分实现细节。在软件开发阶段,组合关系不能比继承关系减少编码量,但是到了软件维护阶段,由于组合关系使系统具有较好的松耦合性,因此使得系统更加容易维护。组合关系的缺点是比继承关系要创建更多的对象。 3.方法覆盖必须满足哪些规则?
[答案] 以下三个是方法覆盖必须满足的主要规则:
(1)子类方法的名称、参数签名和返回类型必须与父类方法的名称、参数签名和返回类型一致。
(2)子类方法不能缩小父类方法的访问权限。 (3)子类方法不能抛出比父类方法更多的异常。 4.以下哪些代码能够编译通过? a) class Fruit { }
public class Orange extends Fruit { public static void main(String[] args){ Fruit f=new Fruit(); Orange o=f;} } b) class Fruit {}
public class Orange extends Fruit { public static void main(String[] args){ Orange o=new Orange(); Fruit f=o; } }
c) interface Fruit {}
public class Apple implements Fruit { public static void main(String[] args){ Fruit f=new Fruit(); Apple a=f; }}
d) interface Fruit {}
public class Apple implements Fruit { public static void main(String[] args){ Apple a=new Apple(); Fruit f=a; }}
e) interface Fruit {}
class Apple implements Fruit {} class Orange implements Fruit {} public class MyFruit {
public static void main(String[] args){ Orange o=new Orange(); Fruit f=o; Apple a=f; }} 5.对于以下类:
9
interface IFace{}
class CFace implements IFace{} class Base{}
public class ObRef extends Base{ public static void main(String argv[]){ ObRef ob = new ObRef(); Base b = new Base(); Object o1 = new Object();
IFace o2 = new CFace(); } } 下面哪些代码能够编译通过? a) o1=o2; b) b=ob; c) ob=b; d) o1=b;
6.对于以下类: class A {}
class B extends A {} class C extends A {} public class Q3ae4 {
public static void main(String args[]) { A x = new A(); B y = new B(); C z = new C();
// 此处插入一条语句 } }
下面哪些语句放到以上插入行,将导致运行时异常? a) x = y; b) z = x; c) y = (B) x; d) z = (C) y; e) y = (A) y;
7.下面哪些是合法的语句?
a) Object o = new String(\b) Boolean b = true;
c) Panel p = new Frame(); d) Applet a = new Panel(); e) Panel p = new Applet(); 8.以下代码能否编译通过,假如能编译通过,运行时得到什么打印结果? String(\ String s = o;
System.out.println(s); System.out.println(o);
[答案] 编译出错。―String s = o‖语句出错。
9.以下代码能否编译通过,假如能编译通过,运行时得到什么打印结果? class Base{
Object o = new 10