JAVA期末复习和答案(1) 下载本文

super(); x=8;

System.out.println(\+x); }

void doPrint(){ super.doPoint();

System.out.println(\+super.x+\+x); } }

in subClass():x=8 super.x=3 sub.x=8

4、在右边的矩形框中写出该程序运行的结果。

class StaticTest {

static int x=1; int y;

StaticTest() { y++; }

public static void main(String args[ ]){ StaticTest st=new StaticTest(); System.out.println(\

System.out.println(\ st=new StaticTest();

System.out.println(\} static { x++;} }

5、写出下列程序的运行结果

class Time1{

private int hour; private int minute; private int second; public Time1(){ setTime(0,0,0); }

public Time1(int hh){ setTime(hh,0,0); }

public Time1(int hh,int mm){ setTime(hh,mm,0); }

public Time1(int hh,int mm,int ss){ setTime(hh,mm,ss); }

public String toString(){

retunr (hour+\ } }

public class MyTime1{

private static Time1 t0,t1,t2,t3;

public static void main(String[] args){ t0=new Time1(); t1=new Time1(11); t2=new Time1(22,22); t3=new Time1(33,33,33);

System.out.println(\ System.out.println(\ System.out.println(\ System.out.println(\ }

}

答案:t0=0:00:00 t1=11:00:00 t2=22:22:00 t3=0:33:33

6、在右边的矩形框中写出该程序运行的结果。

public class TestArray

{ public static void main(String args[ ]){ int i , j ;

int a[ ] = {1,3,2,5,4};

for ( i = 0 ; i < a.length-1; i ++ ) { int k = i;

for ( j = i ; j < a.length ; j++ ) if ( a[j]>a[k] ) k = j; int temp =a[i]; a[i] = a[k];

a[k] = temp; }

for ( i =0 ; i

答案:5、4、3、2、1

7、写出下列程序的运行结果

public class Exam {

public static void main(String[] args) {

int x=5; int m=0;

x+=x++ +(x++ +m); System.out.println(x);

} }

答案:16

8、在右边的矩形框中写出该程序运行的结果。

class MyException extends Exception{

答案:

private int detail;

MyException( int a ){ detail = a;}

public String toString( ){ return \}

public class ExceptionDemo{

public static void compute(int a) throws MyException { System.out.println(\ if( a>10 ) throw new MyException(a); System.out.println(\} public static void main( String args[] ){

try{ compute(6 ); compute( 12 ); }

catch( MyException e ){ System.out.println(\}

}

normal exit

called compute(12) Caught MyException 12

9、试标识出下面的QQ类中的所有无效语句(用下划线标示出),对每个无效语句说出其无效的理由。

class QQ{

private int alpha; private int beta;

public static void classMethod(){

this.beta=this.alpha*2; //beta和alpha不是静态. }

public QQ(){ //无返回值,应该用void QQ(0,0); } public QQ(int x,int y){ //无返回值,应该用void

alpha=this.x; //应该是this.alpha=x;this.beta=y; beta=this.y; } }

10、在右边的矩形框中写出该程序运行的结果。

class OverloadDemo{

void testOverload( int i ){

System.out.println(“int”); } void testOverload(String s){

System.out.println(“String”); } public static void main(String args[ ]){ OverloadDemo a=new OverloadDemo ( ); char ch=’x’;

a.testOverload(ch); } }

答案:int

11、写出下列程序的运行结果

public class Text { }

public int t=4;

public static void main(String[] args) { }

public void NumberPlay(){ }

int t=2; t=t+5;

this.t=this.t-2; t=t-this.t;

System.out.print(t+\); System.out.println(this.t); new Text().NumberPlay();

答案:5 2

12、在右边的矩形框中写出该程序运行的结果。

class First{

public First(){

aMethod(); } public void aMethod(){

System.out.println(“in First class”);} }

public class Second extends First{ public void aMethod(){

System.out.println(“in Second class”);}

public static void main(String[ ] args){ new Second( ); } }

答案:in Second class

13、设计实现地址概念的类Address。Address具有属性:省、市、街道、门牌号、邮编,具有能设置和获取属性的方法。

14、请将下列程序补充完整

下列程序定义一个表示圆形的类,能够计算园面积和周长

class MyCircle{ float r;

final double PI=3.14159;

public double area(){ //计算面积 return PI*r*r; }

public void setR(float x){ //设置半径 r=x; }