java習題及答案

來源:文萃谷 3.12W

  一、單選擇題(每小題2分,共10分)

java習題及答案

1、編譯Java Application 源程序文件將產生相應的字節碼文件,這些字節碼文件的擴展名為( B )。

A. B. s

C. D.

2、設 x = 1 , y = 2 , z = 3,則表達式 y+=z--/++x 的值是( A )。

A. 3 B. 3. 5

C. 4 D. 5

3、在Java Applet程序用户自定義的Applet子類中,一般需要重載父類的( D )方法來完成一些畫圖操作。

A. start( ) B. stop( )

C. init( ) D. paint( )

4、不允許作為類及類成員的訪問控制符的是( C )。

A. public B. private

C. static D. protected

5、為AB類的一個無形式參數無返回值的方法method書寫方法頭,使得使用類名AB作為前綴就可以調用它,該方法頭的形式為( A )。

A. static void method( ) B. public void method( )

C. final void method( ) D. abstract void method( )

  二、填空題(每空格1分,共20分)

1、開發與運行Java程序需要經過的三個主要步驟為 編輯源程序、編譯生成字節碼 和 解釋運行字節碼 。

2、如果一個Java Applet源程序文件只定義有一個類,該類的類名為MyApplet,則類MyApplet必須是 Applet 類的子類並且存儲該源程序文件的文件名為 MyApplet 。

3、如果一個Java Applet程序文件中定義有3個類,則使用Sun公司的JDK編譯器 編譯該源程序文件將產生 3 個文件名與類名相同而擴展名為 . class 的字節碼文件。

4、在Java的基本數據類型中,char型採用Unicode編碼方案,每個Unicode碼佔用 2 字節內存空間,這樣,無論是中文字符還是英文字符,都是佔用 2 字節內存空間。

5、設 x = 2 ,則表達式 ( x + + )/3 的值是 1 。

6、若x = 5,y = 10,則x < y和x >= y的邏輯值分別為 true 和 false 。

7、 抽象(abstract)方法 方法是一種僅有方法頭,沒有具體方法體和操作實現的方法,該方法必須在抽象類之中定義。 最終(final)方法 方法是不能被當前類的子類重新定義的方法。

8、創建一個名為 MyPackage 的包的語句是 package MyPackag ,該語句應該放在程序的位置為: 應該在程序第一句 。

9、設有數組定義:int MyIntArray[ ] = { 10 , 20 , 30 , 40 , 50 , 60 , 70}; 則執行以下幾個語句後的輸出結果是 120 。

int s = 0 ;

for ( int i = 0 ; i < th ; i + + )

if ( i % 2 = = 1 )

s += MyIntArray[i] ;

tln( s );

10、在Java程序中,通過類的定義只能實現 單 重繼承,但通過接口的定義可以實現 多 重繼承關係。

  三、寫出下列程序完成的功能。(每小題5分,共20分)

1、public class Sum

{ public static void main( String args[ ])

{ double sum = 0.0 ;

for ( int i = 1 ; i <= 100 ; i + + )

sum += 1.0/(double) i ;

tln( "sum="+sum );

}

} 答:計算 1/1+1/2+1/3+...+1/100 的值

2、 import .* ;

public class Reverse

{ public static void main(String args[ ])

{ int i , n =10 ;

int a[ ] = new int[10];

for ( i = 0 ; i < n ; i ++ )

try {

BufferedReader br = new BufferedReader(

new InputStreamReader());

a[i] = eInt(Line( )); // 輸入一個整數

} catch ( IOException e ) { } ;

for ( i = n-1 ; i >= 0 ; i ―― )

t(a[i]+" ");

tln( );

}

} 答:從標準輸入(鍵盤)讀入10個整數存入整型數組a中,然後逆序輸出這10個整數

3、 import .*;

public class abc

{ public static void main(String args[])

{ new FrameOut(); }

}

class FrameOut extends Frame // Frame為系統定

{ Button btn; // 義的窗框類

FrameOut( )

{ super("按鈕");

btn = new Button("按下我");

setLayout(new FlowLayout( ));

add(btn);

setSize(300,200);

show( );

}

}答:創建一個標題為"按鈕"的窗框,窗框中顯示有"按下我"字樣的按鈕。

4、import .*;

public class abc

{ public static void main(String args[])

{ SubClass sb = new SubClass( );

tln(( ));

}

}

class SuperClass

{ int a = 10 , b = 20 ; }

class SubClass extends SuperClass

{ int max( ) { return ((a>b)?a:b); } }答:求兩個數的最大值。

  四、寫出下面程序的運行結果(每小題10分,共30分)

1、 import .*;

public class abc

{ public static void main(String args[ ])

{ AB s = new AB("Hello!","I love JAVA.");

tln(ring( ));

}

}

class AB {

String s1;

String s2;

AB( String str1 , String str2 )

{ s1 = str1; s2 = str2; }

public String toString( )

{ return s1+s2;}

}答:1、Hello! I love JAVA.

2、 import .* ;

public class abc

{

public static void main(String args[ ])

{ int i , s = 0 ;

int a[ ] = { 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 };

for ( i = 0 ; i < th ; i ++ )

if ( a[i]%3 = = 0 ) s += a[i] ;

tln("s="+s);

}

}答:s = 180

3、import .* ;

public class abc

{

public static void main(String args[ ])

)

{ tln("a="+a+"nb="+b); }

}

class SubClass extends SuperClass

{ int c;

SubClass(int aa,int bb,int cc)

{ super(aa,bb);

c=cc;

}

}

class SubSubClass extends SubClass

{ int a;

SubSubClass(int aa,int bb,int cc)

{ super(aa,bb,cc);

a=aa+bb+cc;

}

void show()

{ tln("a="+a+"nb="+b+"nc="+c); }

}答:a=60 b=20 c=30

  五、使用Java語言編寫程序。(每小題10分,共20分)

1、編寫一個字符界面的Java Application 程序,接受用户輸入的10個整數,並輸出這10個整數的最大值和最小值。

答:import .* ;

public class abc

{

public static void main(String args[ ])

{ int i , n = 10 , max = 0 , min = 0 , temp = 0;

try {

BufferedReader br = new BufferedReader(

new InputStreamReader());

max = min = eInt(Line( ));

} catch ( IOException e ) { } ;

for ( i = 2 ; i <= n ; i ++ ) {

try {

BufferedReader br = new BufferedReader(new InputStreamReader());

temp = eInt(Line( ));

if (temp > max ) max=temp;

if (temp < min) min=temp;

} catch ( IOException e ) { } ;

}

tln("max="+max+"nmin="+min);

}

}

2、編寫一個完整的`Java Applet 程序使用複數類Complex驗證兩個複數 1+2i 和3+4i 相加產生一個新的複數 4+6i 。

複數類Complex必須滿足如下要求:

(1) 複數類Complex 的屬性有:

RealPart : int型,代表複數的實數部分

ImaginPart : int型,代表複數的虛數部分

(2) 複數類Complex 的方法有:

Complex( ) : 構造函數,將複數的實部和虛部都置0

Complex( int r , int i ) : 構造函數,形參 r 為實部的初值,i為虛部的初值。

Complex complexAdd(Complex a) : 將當前複數對象與形參複數對象相加,所得的結果仍是一個複數值,返回給此方法的調用者。

String ToString( ) : 把當前複數對象的實部、虛部組合成 a+bi 的字符串形式,其中a 和 b分別為實部和虛部的數據。

答:import et.* ;

import .* ;

public class abc extends Applet

{

Complex a,b,c ;

public void init( )

{

a = new Complex(1,2);

b = new Complex(3,4);

c = new Complex();

}

public void paint(Graphics g)

{

c=lexAdd(b);

String("第一個複數:"+ring(),10,50);

String("第二個複數:"+ring(),10,70);

String("兩複數之和:"+ring(),10,90);

}

}

class Complex

{

int RealPart ; // 複數的實部

int ImaginPart ; // 複數的虛部

Complex() { RealPart = 0 ; ImaginPart = 0 ; }

Complex(int r , int i)

{ RealPart = r ; ImaginPart = i ; }

Complex complexAdd(Complex a)

{

Complex temp = new Complex( ); // 臨時複數對象

Part=RealPart+Part;

inPart=ImaginPart+inPart;

return temp;

}

public String toString( )

{ return ( RealPart+" + "+ImaginPart+" i "); }

}

Java線程 程序題

class sum implements Runnable {

int sum = 0;

int i;

public void run () {

for(i=1;i<=100;i++) {

sum+=i;

}

tln("從1加到100的結果為"+sum);

}

}

class sumpro {

public static void main(String args[]) {

sum sum1 = new sum();

Thread t=new Thread(sum1);

t();

}

}

異常

rt .*;

class A{

void m() throws RuntimeException{}

}

class B extends A{

void m() throws IOException{}

}

rt .*;

class A{

void m() throws RuntimeException{}

}

class B extends A{

void m() throws IOException{}

}

ic class e8{

public static void main(String args[]){

e8 t=new e8();

t();

tln(“Hi");

}

public void first(){second();}

public void second() throws Exception{

int x[]=new int[2];

x[3]=2;

}

}

ic class e10{

public static void main(String args[]) throws Exception{

e10 t=new e10();

t();

tln(“Hi");

}

public void first() throws Exception{second();}

public void second() throws Exception{

int x[]=new int[2];

x[3]=2;

}

}

5使用super調用父類方法

class Fish extends Animal{

public Fish(){super(0);}

public void eat(){

tln("魚吃小魚蟲");

}

public void walk(){

();

tln("魚沒有腿不會走路");

}

}

6.接口類的實現

class Cat extends Animal implements Pet{

String name;

public Cat(String n){

super(4);

name=n;

}

public Cat(){this("");}

public String getName(){return name;}

public void setName(String n){name=n;}

public void play(){

tln("貓玩耍");

}

public void eat(){

tln("貓吃貓糧");

}

}

熱門標籤