发布时间 : 星期一 文章Java期末复习练习题2014版更新完毕开始阅读
J=100;
Systerm.out.println(“j=”+j); }
Public static void main (String[] args){
Systerm.out.println(“InitDemo.j=”InitDemo.j”); InitDemo aa=new InitDemo(); InitDemo.j=500;
Systerm.out.println(“aa.j=”+aa.j); }}
答:j = 100
InitDemo.j = 100 i = 5 j = 10 j = 1000 aa.j = 500
32.
class Animal{
public Animal(){
System.out.println(“I’m an animal”); } }
class Bird extends Animal{ public Bird(){
System.out.println(“I’m a bird.”); } }
public class AnimalTest{
public static void main(String[]args){ Bird b = new Bird(); } }
运行结果为: I'm an animal I'm a bird.
33. class A{ int x,y;
void setXY(int x,int y){ this.x = x; this.y = y; }
int multiply(){ return x * y; } }
class B extends A{ int x,z;
B(int x,int z){
super(); this.x =x; this.z =z; }
int multiply(){ return x * y * z; } }
public class ABTest{
public static void main(String[]args){ A a = new B(5,10);//x=5,z=10 a.setXY(10,20);//x=10,y=20
System.out.println(\ B b=new B(6,9);//x=6.z=9 b.setXY(7,8);//x=7,y=8 b.z=10;//z=10
System.out.println(\ } }
运行结果为: m=1000 m=480
34.class Parent{ void printMe(){
System.out.println(\ } }
class Child extends Parent{ void printMe(){
System.out.println(\}
void printAll(){ super.printMe(); this.printMe(); printMe(); } }
public class Test{
public static void main(String[]args){ Child myC = new Child(); myC.printAll(); } }
运行结果为: I'm a parent. I'm a child. I'm a child.
Press any key to continue... 34.
abstract class AA{
abstract void callme(); void metoo(){
System.out.println(\ } }
class BB extends AA{ void metoo(){
System.out.println(\ }
void callme(){
System.out.println(\ } }
public class AbstractTest{
public static void main(String[]args){ AA aa = new BB(); aa.callme(); aa.metoo(); } }
运行结果:
Inside BB's callme. Inside BB's metoo.
Press any key to continue... 35.
class Employee{}
class Manager extends Employee{} class Secretary extends Employee{} class Programmer extends Employee{} public class Test24{ public static void method(Employee e){ if (e instanceof Manager) System.out.println(\ else if (e instanceof Secretary) System.out.println(\else if (e instanceof Programmer) System.out.println(\ }
public static void main(String[] args){ Manager m = new Manager(); Secretary s = new Secretary(); Programmer p = new Programmer(); method(m); method(s); method(p); } }
输出结果为:
He is a Manager. He is a Secretary. He is a Programmer.
36.
public class Test{
public static void main(String [] args){ try{
for(int i = 3 ; i >= 0 ; i--){
System.out.println(\System.out.println(6 / i); }
}catch(ArithmeticException ae){ System.out.println(\ } }
}
输出结果为:
The value of i : 3 2
The value of i : 2 3
The value of i : 1 6
The value of i : 0 Divided by zero. 37.
public class Foo{ public static void method(int i){ try{ if(i >0){ return; }else{ System.exit(1); } } finally{ System.out.println(\ }
public static void main(String [] args){ method(5); method(-5); } }
输出结果为:
Finally
38.
public class Test1{
public static String output=\public static void foo(int i){ try {
if(i==1){
throw new Exception(); }
output +=\
}catch (Exception e){ output +=\return ; }finally{
output +=\}
output +=\}
public static void main(String[] args){ foo(0); foo(1);
System.out.println(\