Java語言程序設計考試題及答案(四)
本文關鍵詞:Java語言程序設計,由筆耕文化傳播整理發(fā)布。
Java語言程序設計考試題及答案(四)
2015-11-11 10:13:57 來源:大學生聯(lián)盟
Java語言程序設計考試題及答案(四)
一、單項選擇題(本大題共10小題,每小題1分,共10分) 在每小題列出的四個備選項中只有一個是符合題目要求的,請將其代碼填寫在題后的括號內。錯選、多選或未選均無分。
1. 下列正確的句子是() (1分)
A:float 3.14;
B:byte i=225;
C:int k=33L;
D:int p[][];
2. 當使用FileInputStream類中的read()方法時,如果沒有讀入一個字節(jié)數(shù)據(jù)時,返回值為()
A:0
B:-1
C:true
D:false
3. 能夠支持 javadoc 命令的文檔注釋形式是() (1分)
A:/**...//
B:/*...*/
C://
D:/**...*/
4. 以下程序代碼的輸出結果是()
int x=100;
System.out.println(5.5+x/8); (1分)
A:18
B:17.5
C:9.5
D:5.512
5. 當用戶在文件對話框中選擇了文件并按下了“打開/保存”按鈕后,程序可利用一個方法取得文件對象,并針對這個對象使用getName()取得文件名,使用getPath()取得文件的路徑,則這個方法是() (1分)
A:getFile()
B:getSelectFile()
C:getSelectedFile()
D:getDescription()
6. 在 Java 中,表示換行符的轉義字符是() (1分)
A:A
B:B
C:C
D:D
7. 一個實現(xiàn)Runnabel接口的類必須要實現(xiàn)的方法是() (1分)
A:start()
B:run()
C:sleep()
D:init()
8. 下列有關Java的敘述錯誤的是() (1分)
A:強類型語言
B:與平臺無關
C:可以使用漢字變量名
D:不檢查數(shù)組下標越界
9. 下列有關數(shù)組的敘述錯誤的是() (1分)
A:數(shù)組是將一組相同類型的數(shù)據(jù)順序存儲,組成一種復合數(shù)據(jù)類型
B:在Java語言中,數(shù)組是一種引用類型,數(shù)組名如同對象名一樣,是一種引用
C:對于多維數(shù)組,從最高維開始,可以對每一維分配不等長的空間
D:在Java語言中,數(shù)組元素可以互相賦值,而數(shù)組名不能直接賦值
10. 當線程A使用某個對象,而此對象又需要線程B修改后才能符合A線程的需要,這時線程A就要等待線程B完成修改工作,這種現(xiàn)象稱為() (1分)
A:線程的同步
B:線程的互斥
C:線程的調度
D:線程的就緒
二、填空題(本大題共10小題,每小題2分,共20分)請在每小題的空格中填上正確答案。錯填、不填均無分。
1. 在switch 語句中,通過語句退出 switch 語句,使程序從整個 switch 語句后面的第一條語句處開始執(zhí)行。 (2分)break
2. 在 Java 中所有實現(xiàn)的多維數(shù)組,實際上是由一維數(shù)組構成的。 (2分)
3. 為實現(xiàn)線程同步,Java語言提供了notify()、notifyAll()和Sycgised()三個方法供線程在臨界段中使用。 (2分)
4. Java語言使用Unicode字符集,共有65536個字符。 (2分)
5. 閏年的條件是:每4年一個閏年,但每100年少一個閏年,每400年又增加一個閏年。如果年份用整數(shù)類型變量y表示,則y年是閏年的條件用邏輯表達式可描述為。 (2分)
y%4&&y!%100||y%400
6. Java語言中,邏輯類型用boolean關鍵字標識,也稱布爾類型。 (2分)
7. Java程序能實現(xiàn)多線程并行執(zhí)行,為了讓一個線程暫停執(zhí)行,其他線程有機會被執(zhí)行,可對要暫停的線程執(zhí)行wait()方法。 (2分)
8. Java應用程序和小應用程序都由一個或多個擴展名為的class文件組成。 (2分)
9. 將顯示區(qū)域按行、列劃分成一個個大小相等的格子,組件依次放入這些格子中。GridLayout (2分)
10. 鍵盤事件的接口是。keyListener (2分)
三、程序填空題(本大題共5小題,每空2分,共20分)
1. 下面小應用程序啟動后自動播放一個聲音文件“1.wav”。
import java.applet.*;
public class Class3001 extends Applet
{
public void start()
{
=getAudioClip(getCodeBase(),"1.wav");
clip.;
}
} (2分)
2. 下列程序的功能是每隔一秒鐘輸出一個字符串“Hello!”。
public class C3101 implements Runnable
{
public static void main(String args[])
{
C3101 t=new C3101();
Thread tt=new Thread(t);
tt.start();
}
public void run()
{
for(int i=0;i<10;i++)
{
try{sleep(1000);
}catch(Exception e){e.printStackTrace();}
System.out.println("Hello");
}
}
} (2分)
3. 設計一個JFrame窗口,窗口中心有一個文本區(qū)。另有4個菜單,每個菜單都有菜單項,每個菜單項都對應有快捷鍵,選擇某個菜單項時,窗口中心的文本區(qū)顯示相應信息。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class C2902 implements ActionListener
{
JTextField text=new JTextField(20);
String t[]={"文件","編輯","格式","幫助"};
Menu menu[]=new Menu[4];
String s[]={"打開","保存","復制","粘貼","字體","段落","小助手","關于"};
MenuItem jmi[]=new MenuItem[8];
C2902()
{
JFrame myWin=new JFrame("Ex0606");
myWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con=myWin.getContentPane();
con.add(text,"Center");
text.setEditable(false);
myWin.setMenuBar(jmb);
int i;
for(i=0;i<4;i++)//生成四個菜單
{
menu[i]=new Menu(t[i]);
jmb.add(menu[i]);
}
for(i=0;i<8;i++)//每個菜單中放兩個菜單項
{
jmi[i]=new MenuItem(s[i]);
menu[i/2].add(jmi[i]);
jmi[i].addActionListener(this);
jmi[i].(new MenuShortcut(65+i));//設快捷鍵
}
myWin.setBounds(300,200,400,200);
myWin.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
text.setText("你選擇了:"+e.getActionCommand()+"菜單項.");
}
public static void main(String[] args)
{
new C2902();
}
} (2分)
4. 下面應用程序,繪制一個紅色五角星。請完成程序。
import java.awt.*;
import javax.swing.*;
class MyPanel extends JPanel
{
public void
{
int r=100;
int x1=100,
x2=(int)(r*(1-Math.cos((18*Math.PI)/180))),
x3=(int)(r*(1+Math.cos((18*Math.PI)/180))),
x4=(int)(r*(1-Math.cos((54*Math.PI)/180))),
x5=(int)(r*(1+Math.cos((54*Math.PI)/180))),
y1=0,
y2=(int)(r*(1-Math.sin((18*Math.PI)/180))),
y3=(int)(r*(1-Math.sin((18*Math.PI)/180))),
y4=(int)(r*(1+Math.sin((54*Math.PI)/180))),
y5=(int)(r*(1+Math.sin((54*Math.PI)/180)));
g.drawOval(0,0,200,200);
g.drawLine(x4,y4,x1,y1);g.drawLine(x1,y1,x5,y5);
g.drawLine(x5,y5,x2,y2);g.drawLine(x2,y2,x3,y3);
g.drawLine(x3,y3,x4,y4);
}
}
public class C3002 extends JFrame
{
C3002()
{
super("C3002");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(new MyPanel());
setSize(210,242);
setVisible(true);
}
public static void main(String[] args)
{
new C3002();
}
} (2分)
5. 下面程序運行后,連續(xù)輸出20個相同的單詞。請完成程序。
public class C3102
{
public static void main(String args[])
{
t.start();
}
}
class Hello implements Runnable
{
int i=0;
public void run()
{
while()
{
System.out.print(i+":Hello");
}
}
} (2分)
四、程序分析題(本大題共5小題,每小題4分,共20分)
1. 寫出下面程序的運行結果。
public class Test extends TT
{
public static void main(String args[])
{
Test t=new Test("Tom.");
}
public Test(String s)
{
super(s);
System.out.print("How are you?");
}
public Test()
{
this("I am Jack.");
}
}
class TT
{
public TT()
{
System.out.print("Hi!");
}
public TT(String s)
{
this();
System.out.print("I am "+s);
}
} (4分)
2. 閱讀下面程序,寫出程序功能。
import java.applet.*;import java.awt.*;
public class Class3602 extends Applet implements Runnable
{
Thread redBall,blueBall;Graphics redPen,bluePen;
int blueSeta=0,redSeta=0;
public void init()
{
setSize(250,200);
redBall=new Thread(this);blueBall=new Thread(this);
redPen=getGraphics();bluePen=getGraphics();
redPen.setColor(Color.red);bluePen.setColor(Color.blue);
setBackground(Color.gray);
}
public void start()
{
redBall.start();blueBall.start();
}
public void run()
{
int x,y;
while(true)
{
if(Thread.currentThread()==redBall)
{
x=(int)(80.0*Math.cos(3.1415926/180.0*redSeta));
y=(int)(80.0*Math.sin(3.1415926/180.0*redSeta));
redPen.setColor(Color.gray);
redPen.fillOval(100+x,100+y,10,10);
redSeta +=3;
if(redSeta>=360) redSeta=0;
x=(int)(80.0*Math.cos(3.1415926/180.0*redSeta));
y=(int)(80.0*Math.sin(3.1415926/180.0*redSeta));
redPen.setColor(Color.red);
redPen.fillOval(100+x,100+y,10,10);
try {redBall.sleep(20);}
catch(InterruptedException e){}
}
else if (Thread.currentThread()==blueBall)
{
x=(int)(80.0*Math.cos(3.1415926/180.0*blueSeta));
y=(int)(80.0*Math.sin(3.1415926/180.0*blueSeta));
bluePen.setColor(Color.gray);
bluePen.fillOval(150+x,100+y,10,10);
blueSeta-=3;
if(blueSeta<=-360) blueSeta=0;
x=(int)(80.0*Math.cos(3.1415926/180.0*blueSeta));
y=(int)(80.0*Math.sin(3.1415926/180.0*blueSeta));
bluePen.setColor(Color.blue);
bluePen.fillOval(150+x,100+y,10,10);
try {blueBall.sleep(40);}
catch(InterruptedException e){}
}
}
}
} (4分)
3. 閱讀下列程序,寫出運行結果。
public class Class3205
{
public static void main(String[] args)
{
String str1="abc";
String str2="Abc";
String str3="abc";
String str4=new String("abc");
System.out.println(str1==str2);
System.out.println(str1.equals(str2));
System.out.println(str1==str3);
System.out.println(str1.equals(str3));
System.out.println(str1==str4);
System.out.println(str1.equals(str4));
}
} (4分)
4. 閱讀下列程序,寫出程序功能。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Class3505 implements AdjustmentListener
{
JScrollBar js;
JLabel label;
int size=20;
public Class3505()
{
JFrame myWin=new JFrame("Class3505");
myWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myWin.setBounds(200,200,400,400);
Container con=myWin.getContentPane();
js=new JScrollBar(JScrollBar.HORIZONTAL,20,10,1,210);
js.setSize(new Dimension(395,50));
js.addAdjustmentListener(this);
con.add(js);
label=new JLabel("Class3505");
con.add(label);
myWin.setVisible(true);
}
public void adjustmentValueChanged(AdjustmentEvent e)
{
size=e.getValue();
Font f=new Font(label.getFont().getName(),
label.getFont().getStyle(),size);
label.setFont(f);
}
public static void main(String[]args)
{
new Class3505();
}
} (4分)
5. 閱讀下面程序,寫出程序運行結果。
class printer
{
synchronized void printchar(char ch)
{
for (int i=1;i<=3;i++)System.out.print(ch);
System.out.println();
}
}
class printerThread extends Thread
{
printer ptr;
char ch;
printerThread(printer ptr,char ch)
{
this.ptr=ptr;
this.ch=ch;
}
public void run()
{
for (int i=1; i<=3;i++)ptr.printchar(ch);
}
}
public class Class3605
{
public static void main(String[] args)
{
printer ptr=new printer();
printerThread pt1=new printerThread(ptr,?A?);
printerThread pt2=new printerThread(ptr,?B?);
pt1.start();
pt2.start();
}
} (4分)
五、程序設計題(本大題共2小題,每小題6分,共12分)
1. 編寫一個小應用程序,在小應用程序中有兩個線程,一個負責模仿圓轉運動,另一個模仿橢圓運動。已給出部分代碼,請完成程序。
import java.awt.*;
import java.applet.*;
public class Class3804 extends Applet implements Runnable
{
Thread thread1,thread2;
Graphics g;
public void init()
{
setSize(500,500);
setBackground(Color.gray);
g=getGraphics();
g.setColor(Color.red);
g.setXORMode(Color.blue);
thread1=new Thread(this);
thread2=new Thread(this);
}
public void start()
{
thread1.start();
thread2.start();
}
public void run()
{
int x=0,y=0;
while(true)
{
//這里是你要編寫的代碼
}
}
} (6分)
2. 編寫一個方法hex3705(),要求將一個以字符串(如有字母用大寫)形式表示的十六進制數(shù)轉換十進制正整數(shù),其中字符串為方法參數(shù),正整數(shù)為返回值。 (6分)
六、簡答題(本大題共6小題,每小題3分,共18分)
1. 請寫出:提供緩沖式輸出的類、采用緩沖輸出時如果想立即寫入文件就調用的方法。 (3分)
2. 在以下類的聲明中,指出哪些是錯誤的以及錯誤的原因?
①class Class22 extends Class2201,Class2202{......}
②public abstract class Class22{......}
③abstract final class Class22{......}
④abstract public class Class22{......}
⑤public abstract class String{......}
⑥public final class Class22 extends implement interface1{......} (3分)
3. 文件隨機訪問應該利用什么類來實現(xiàn)?創(chuàng)建此類對象的代碼應該能捕獲什么類型的異常? (3分)
4. 用一個Java語句完成下面的要求:
先將整型變量a的值加1,再把它加到變量b中。 (3分)
5. 用一個Java語句完成下面的要求:
先把變量a與b的差賦值給變量c,再把變量b的值減1。 (3分)
6. 請寫一條語句,實現(xiàn)用預定的顏色填充一個突出顯示的矩形色塊,其中矩形左上角坐標為(200,100),右下角坐標為(400,500),調用的是類型為Graphics的g對象。 (3分)
參考答案
一、單項選擇題 (本大題共10小題,每小題1分,共10分) 在每小題列出的四個備選項中只有一個是符合題目要求的,請將其代碼填寫在題后的括號內。錯選、多選或未選均無分。
1:參考答案:D
參考解析:(P71)聲明二維數(shù)組的一般形式有以下3種:
①類型 數(shù)組名 [][]
②類型 [][] 數(shù)組名
③類型 [] 數(shù)組名 []
考試題內容:
下列正確的句子是()
A:float 3.14;
B:byte i=225;【-128,127】
C:int k=33L;
D:int p[][];
2:參考答案:B
參考解析:(P176)read()方法的返回值類型為int型,用-1表示流在讀之前已結束。
考試題內容:
當使用FileInputStream類中的read()方法時,如果沒有讀入一個字節(jié)數(shù)據(jù)時,返回值為()
A:0
B:-1
C:true
D:false
3:參考答案:D
參考解析:(P6)/**……*/這種注釋是文檔注釋,文檔注釋能被實用程序javadoc接受,由它能自動生成程序的HTML文檔。
考試題內容:
能夠支持 javadoc 命令的文檔注釋形式是()
A:/**...//
B:/*...*/
C://
D:/**...*/
4:參考答案:B
參考解析:(P14)Java程序中的類只能繼承一個類,這種繼承稱為單繼承。Java語言雖不允許一個類繼承多個類,但允許一個類實現(xiàn)多個接口。接口有與類相似的基本形式。
考試題內容:
以下程序代碼的輸出結果是()
int x=100;
System.out.println(5.5+x/8);
A:18
B:17.5
C:9.5
D:5.512
5:參考答案:C
參考解析:(P182)
考試題內容:
當用戶在文件對話框中選擇了文件并按下了“打開/保存”按鈕后,程序可利用一個方法取得文件對象,并針對這個對象使用getName()取得文件名,使用getPath()取得文件的路徑,則這個方法是()
A:getFile()
B:getSelectFile()
C:getSelectedFile()
D:getDescription()
6:參考答案:A
參考解析:(P9)
考試題內容:
在 Java 中,表示換行符的轉義字符是()
A:A
B:B
C:C
D:D
7:參考答案:B
參考解析:(P163)本題考核知識點是Runnable接口的使用。實現(xiàn)一個接口必須實現(xiàn)接口中的所有方法,而Runnable接口只有一個方法run()。
考試題內容:
一個實現(xiàn)Runnabel接口的類必須要實現(xiàn)的方法是()
A:start()
B:run()
C:sleep()
D:init()
8:參考答案:D
參考解析:(P3)
考試題內容:
下列有關Java的敘述錯誤的是()
A:強類型語言
B:與平臺無關
C:可以使用漢字變量名
D:不檢查數(shù)組下標越界
9:參考答案:D
參考解析:(P70)數(shù)組是一種引用類型,一個數(shù)組名在不同時間可引用不同的數(shù)組,數(shù)組名之間可以賦值。
考試題內容:
下列有關數(shù)組的敘述錯誤的是()
A:數(shù)組是將一組相同類型的數(shù)據(jù)順序存儲,組成一種復合數(shù)據(jù)類型
B:在Java語言中,數(shù)組是一種引用類型,數(shù)組名如同對象名一樣,是一種引用
C:對于多維數(shù)組,從最高維開始,可以對每一維分配不等長的空間
D:在Java語言中,數(shù)組元素可以互相賦值,而數(shù)組名不能直接賦值
10:參考答案:A
參考解析:(P168)
考試題內容:
當線程A使用某個對象,而此對象又需要線程B修改后才能符合A線程的需要,這時線程A就要等待線程B完成修改工作,這種現(xiàn)象稱為()
A:線程的同步
B:線程的互斥
C:線程的調度
D:線程的就緒
二、填空題 (本大題共10小題,每小題2分,共20分)請在每小題的空格中填上正確答案。錯填、不填均無分。
1:參考答案:(P23)break
考試題內容:
在switch 語句中,通過_____語句退出 switch 語句,使程序從整個 switch 語句后面的第一條語句處開始執(zhí)行。
2:參考答案:(P71)數(shù)組的數(shù)組
考試題內容:
在 Java 中所有實現(xiàn)的多維數(shù)組,實際上是由一維數(shù)組構成的_____。
3:參考答案:(P168)wait()
考試題內容:
為實現(xiàn)線程同步,Java語言提供了notify()、notifyAll()和_____三個方法供線程在臨界段中使用。
4:參考答案:(P7)65536
考試題內容:
Java語言使用Unicode字符集,共有_____個字符。
5:參考答案:(P16)(y%4==0&&y%100!=0)||y%400==0
考試題內容:
閏年的條件是:每4年一個閏年,但每100年少一個閏年,每400年又增加一個閏年。如果年份用整數(shù)類型變量y表示,則y年是閏年的條件用邏輯表達式可描述為_____。
6:參考答案:(P8)Boolean
考試題內容:
Java語言中,邏輯類型用關鍵字_____標識,也稱布爾類型。
7:參考答案:(P161)sleep()
考試題內容:
Java程序能實現(xiàn)多線程并行執(zhí)行,為了讓一個線程暫停執(zhí)行,其他線程有機會被執(zhí)行,可對要暫停的線程執(zhí)行_____方法。
8:參考答案:(P11).class
[解析]Java程序的擴展名為“.class”,源程序的擴展名為“.java”。
考試題內容:
Java應用程序和小應用程序都由一個或多個擴展名為_____的文件組成。
9:參考答案:(P98)GridLayout
[解析]常用的布局類:FlowLayout為依次放置組件;BorderLayout將組件放置在邊界上; CardLayout將組件像撲克牌一樣疊放,而每次只能顯示其中的一個組件;GridLayout將顯示區(qū)域按行、列劃分成一個個大小相等的格子,組件依次放入這些格子中;GridBagLayout能進行更精細的位置控制。
考試題內容:
_____將顯示區(qū)域按行、列劃分成一個個大小相等的格子,組件依次放入這些格子中。
10:參考答案:(P132)KeyListener
[解析]鍵盤事件的接口是KeyListener,注冊鍵盤事件監(jiān)視器的方法是addKeyListener(監(jiān)視器)。實現(xiàn)KeyListener接口的方法有3個:
(1)keyPressed(KeyEvent e),鍵盤上某個鍵被按下。
(2)keyReleased(KeyEvent e),鍵盤上某下鍵被按下后,又釋放。
(3)keyTyped(KeyEvent e),keyPressed和keyReleased兩方法的組合
考試題內容:
鍵盤事件的接口是_____。
三、程序填空題 (本大題共5小題,每空2分,共20分)
1:參考答案:AudioClip clip
play()
,
考試題內容:
下面小應用程序啟動后自動播放一個聲音文件“1.wav”。
import java.applet.*;
public class Class3001 extends Applet
{
public void start()
{
_____=getAudioClip(getCodeBase(),"1.wav");
clip._____;
}
}
2:參考答案:sleep(1000)
InterruptedException e
,
考試題內容:
下列程序的功能是每隔一秒鐘輸出一個字符串“Hello!”。
public class C3101 implements Runnable
{
public static void main(String args[])
{
C3101 t=new C3101();
Thread tt=new Thread(t);
tt.start();
}
public void run()
{
for(;;)
{
try{
_____ ;
}catch(_____){}
System.out.println("Hello");
}
}
}
3:參考答案:MenuBar jmb=new MenuBar();
setShortcut
,
考試題內容:
設計一個JFrame窗口,窗口中心有一個文本區(qū)。另有4個菜單,每個菜單都有菜單項,每個菜單項都對應有快捷鍵,選擇某個菜單項時,窗口中心的文本區(qū)顯示相應信息。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class C2902 implements ActionListener
{
_____
JTextField text=new JTextField(20);
String t[]={"文件","編輯","格式","幫助"};
Menu menu[]=new Menu[4];
String s[]={"打開","保存","復制","粘貼","字體","段落","小助手","關于"};
MenuItem jmi[]=new MenuItem[8];
C2902()
{
JFrame myWin=new JFrame("Ex0606");
myWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con=myWin.getContentPane();
con.add(text,"Center");
text.setEditable(false);
myWin.setMenuBar(jmb);
int i;
for(i=0;i<4;i++)//生成四個菜單
{
menu[i]=new Menu(t[i]);
jmb.add(menu[i]);
}
for(i=0;i<8;i++)//每個菜單中放兩個菜單項
{
jmi[i]=new MenuItem(s[i]);
menu[i/2].add(jmi[i]);
jmi[i].addActionListener(this);
jmi[i]._____(new MenuShortcut(65+i));//設快捷鍵
}
myWin.setBounds(300,200,400,200);
myWin.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
text.setText("你選擇了:"+e.getActionCommand()+"菜單項.");
}
public static void main(String[] args)
{
new C2902();
}
}
4:參考答案:paintComponent(Graphics g);
g.setColor(Color.RED);
,
考試題內容:
下面應用程序,繪制一個紅色五角星。請完成程序。
import java.awt.*;
import javax.swing.*;
class MyPanel extends JPanel
{
public void_____
{
int r=100;
int x1=100,
x2=(int)(r*(1-Math.cos((18*Math.PI)/180))),
x3=(int)(r*(1+Math.cos((18*Math.PI)/180))),
x4=(int)(r*(1-Math.cos((54*Math.PI)/180))),
x5=(int)(r*(1+Math.cos((54*Math.PI)/180))),
y1=0,
y2=(int)(r*(1-Math.sin((18*Math.PI)/180))),
y3=(int)(r*(1-Math.sin((18*Math.PI)/180))),
y4=(int)(r*(1+Math.sin((54*Math.PI)/180))),
y5=(int)(r*(1+Math.sin((54*Math.PI)/180)));
_____
g.drawOval(0,0,200,200);
g.drawLine(x4,y4,x1,y1);g.drawLine(x1,y1,x5,y5);
g.drawLine(x5,y5,x2,y2);g.drawLine(x2,y2,x3,y3);
g.drawLine(x3,y3,x4,y4);
}
}
public class C3002 extends JFrame
{
C3002()
{
super("C3002");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(new MyPanel());
setSize(210,242);
setVisible(true);
}
public static void main(String[] args)
{
new C3002();
}
}
5:參考答案:Thread t=new Thread(new Hello());
i++<20
,
考試題內容:
下面程序運行后,連續(xù)輸出20個相同的單詞。請完成程序。
public class C3102
{
public static void main(String args[])
{
_____
t.start();
}
}
class Hello implements Runnable
{
int i=0;
public void run()
{
while(_____)
{
System.out.print(i+":Hello");
}
}
}
四、程序分析題 (本大題共5小題,每小題4分,共20分)
1:參考答案:輸出結果為:
Hi!I am Tom.How are you?
考試題內容:
寫出下面程序的運行結果。
public class Test extends TT
{
public static void main(String args[])
{
Test t=new Test("Tom.");
}
public Test(String s)
{
super(s);
System.out.print("How are you?");
}
public Test()
{
this("I am Jack.");
}
}
class TT
{
public TT()
{
System.out.print("Hi!");
}
public TT(String s)
{
this();
System.out.print("I am "+s);
}
}
2:參考答案:程序功能:小應程序創(chuàng)建兩個線程,一個順時針畫圓,一個逆時針畫圓。
考試題內容:
閱讀下面程序,寫出程序功能。
import java.applet.*;import java.awt.*;
public class Class3602 extends Applet implements Runnable
{
Thread redBall,blueBall;Graphics redPen,bluePen;
int blueSeta=0,redSeta=0;
public void init()
{
setSize(250,200);
redBall=new Thread(this);blueBall=new Thread(this);
redPen=getGraphics();bluePen=getGraphics();
redPen.setColor(Color.red);bluePen.setColor(Color.blue);
setBackground(Color.gray);
}
public void start()
{
redBall.start();blueBall.start();
}
public void run()
{
int x,y;
while(true)
{
if(Thread.currentThread()==redBall)
{
x=(int)(80.0*Math.cos(3.1415926/180.0*redSeta));
y=(int)(80.0*Math.sin(3.1415926/180.0*redSeta));
redPen.setColor(Color.gray);
redPen.fillOval(100+x,100+y,10,10);
redSeta +=3;
if(redSeta>=360) redSeta=0;
x=(int)(80.0*Math.cos(3.1415926/180.0*redSeta));
y=(int)(80.0*Math.sin(3.1415926/180.0*redSeta));
redPen.setColor(Color.red);
redPen.fillOval(100+x,100+y,10,10);
try {redBall.sleep(20);}
catch(InterruptedException e){}
}
else if (Thread.currentThread()==blueBall)
{
x=(int)(80.0*Math.cos(3.1415926/180.0*blueSeta));
y=(int)(80.0*Math.sin(3.1415926/180.0*blueSeta));
bluePen.setColor(Color.gray);
bluePen.fillOval(150+x,100+y,10,10);
blueSeta-=3;
if(blueSeta<=-360) blueSeta=0;
x=(int)(80.0*Math.cos(3.1415926/180.0*blueSeta));
y=(int)(80.0*Math.sin(3.1415926/180.0*blueSeta));
bluePen.setColor(Color.blue);
bluePen.fillOval(150+x,100+y,10,10);
try {blueBall.sleep(40);}
catch(InterruptedException e){}
}
}
}
}
3:參考答案:(P75)運行結果:
false
false
true
true
false
true
[解析]str1.equals(str2)是str1t和str2兩個字符串的內容比較是否相等,str1==str2表示str1和str2兩個字符串是
否引用同一個對象。為了比較基本數(shù)據(jù)類型變量的值是否相等是使用“==”,但這個辦法并不適用于String對象(或者
其他任何對象)。對于str1和str3是引用同一個字符串常量,其內容當然相同。而str1和str4內容雖然相同但引用的不
是同一對象,這是因為str1引用的是編譯時就確定的字符串常量,str4引用的是程序運行時才創(chuàng)建的對象。
考試題內容:
閱讀下列程序,寫出運行結果。
public class Class3205
{
public static void main(String[] args)
{
String str1="abc";
String str2="Abc";
String str3="abc";
String str4=new String("abc");
System.out.println(str1==str2);
System.out.println(str1.equals(str2));
System.out.println(str1==str3);
System.out.println(str1.equals(str3));
System.out.println(str1==str4);
System.out.println(str1.equals(str4));
}
}
4:參考答案:程序功能:應用程序窗口中產(chǎn)生一個水平滾動條,下面是一個標簽,當拖動滾動條滑塊時,標簽里的文字的大小隨著
滑塊的移動而變化。文字字號的范圍是1~200。
考試題內容:
閱讀下列程序,寫出程序功能。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Class3505 implements AdjustmentListener
{
JScrollBar js;
JLabel label;
int size=20;
public Class3505()
{
JFrame myWin=new JFrame("Class3505");
myWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myWin.setBounds(200,200,400,400);
Container con=myWin.getContentPane();
js=new JScrollBar(JScrollBar.HORIZONTAL,20,10,1,210);
js.setSize(new Dimension(395,50));
js.addAdjustmentListener(this);
con.add(js);
label=new JLabel("Class3505");
con.add(label);
myWin.setVisible(true);
}
public void adjustmentValueChanged(AdjustmentEvent e)
{
size=e.getValue();
Font f=new Font(label.getFont().getName(),
label.getFont().getStyle(),size);
label.setFont(f);
}
public static void main(String[]args)
{
new Class3505();
}
}
5:參考答案:程序運行結果:
AAA
AAA
AAA
BBB
BBB
BBB
[解析]本程序當兩個循環(huán)次數(shù)改為30時,輸出結果將不再是先所有行都是A,然后所有行都是B,會出現(xiàn)若干行A,若干
行B,再若干行A,若干行B的情況,那是因為兩個線程在同時運行且運行次數(shù)比較多,CPU在分時為它們服務,循環(huán)次數(shù)
少分時體現(xiàn)不出來,CPU的一個時間單位一個線程就已經(jīng)完成了。即使是循環(huán)30次,但也不會出現(xiàn)一行中既有A又有B的情
況,這是因為輸出字符的方法printchar()被synchronized修飾,任何時刻都只能有一個線程使用printchar(),在一行
中不會輸出一部分A一部分B,如果把synchronized去掉,則會出現(xiàn)這種情況。
考試題內容:
閱讀下面程序,寫出程序運行結果。
class printer
{
synchronized void printchar(char ch)
{
for (int i=1;i<=3;i++)System.out.print(ch);
System.out.println();
}
}
class printerThread extends Thread
{
printer ptr;
char ch;
printerThread(printer ptr,char ch)
{
this.ptr=ptr;
this.ch=ch;
}
public void run()
{
for (int i=1; i<=3;i++)ptr.printchar(ch);
}
}
public class Class3605
{
public static void main(String[] args)
{
printer ptr=new printer();
printerThread pt1=new printerThread(ptr,?A?);
printerThread pt2=new printerThread(ptr,?B?);
pt1.start();
pt2.start();
}
}
五、程序設計題 (本大題共2小題,每小題6分,,共12分)
1:參考答案:if(Thread.currentThread()==thread1)
{
g.setColor(Color.gray);
g.drawArc(50,50,200,200,x,1);
x=(x+1);
try
{Thread.sleep(10);}
catch(InterruptedException e){}
}
else if (Thread.currentThread()==thread2)
{
g.setColor(Color.gray);
g.drawArc(150,350,300,100,y,1);
y=(y+1);
try
{Thread.sleep(5);}
catch(InterruptedException e){}
}
[解析]本程序利用了異或繪圖模式,第一圈是畫出圓,再重復畫時,相當于擦除,所以第二圈畫完也就擦完了,第三
圈再畫,第四圈再擦,反復循環(huán),形成一種漂亮的動畫效果。
考試題內容:
編寫一個小應用程序,在小應用程序中有兩個線程,一個負責模仿圓轉運動,另一個模仿橢圓運動。已給出部分代碼,請完成程序。
import java.awt.*;
import java.applet.*;
public class Class3804 extends Applet implements Runnable
{
Thread thread1,thread2;
Graphics g;
public void init()
{
setSize(500,500);
setBackground(Color.gray);
g=getGraphics();
g.setColor(Color.red);
g.setXORMode(Color.blue);
thread1=new Thread(this);
thread2=new Thread(this);
}
public void start()
{
thread1.start();
thread2.start();
}
public void run()
{
int x=0,y=0;
while(true)
{
//這里是你要編寫的代碼
}
}
}
2:參考答案:int hex3705(String s)
{
int n=0;
byte b[]=s.getBytes();
int i=b.length,k=1;
while(--i>=0)
{
int t=b[i]-48;
if(t>10)t=t-7;
n=n+t*k;
k*=16;
}
return n;
}
[解析]完整的程序如下:
public class Class3705
{
static int hex3705(String s)
{
int n=0;
byte b[]=s.getBytes();//字符串存入字節(jié)數(shù)組,轉化為字符
int i=b.length,k=1;
while(--i>=0)//對字節(jié)數(shù)組每個元素進行分析
{
int t=b[i]-48;//字符‘0’的ASCII碼值是48
if(t>10)t=t-7;//若是字母,需要減55,‘A’的ASCII碼值是65
n=n+t*k;
k*=16;//k是16的冪
}
return n;
}
public static void main(String[] args)
{
System.out.println(hex3705("10D"));
}
}
運行結果為:269
考試題內容:
編寫一個方法hex3705(),要求將一個以字符串(如有字母用大寫)形式表示的十六進制數(shù)轉換十進制正整數(shù),其中字符串為方法參數(shù),正整數(shù)為返回值。
六、簡答題 (本大題共6小題,每小題3分,共18分)
1:參考答案:(P179)BufferedWriter類、flush()方法。
考試題內容:
請寫出:提供緩沖式輸出的類、采用緩沖輸出時如果想立即寫入文件就調用的方法。
2:參考答案:(P49)
(1)錯誤:Java語言不支持多重繼承。
(3)錯誤:abstract和final不能同時修飾類。
(5)錯誤:類名不能使用關鍵字String命名。
考試題內容:
在以下類的聲明中,指出哪些是錯誤的以及錯誤的原因?
①class Class22 extends Class2201,Class2202{......}
②public abstract class Class22{......}
③abstract final class Class22{......}
④abstract public class Class22{......}
⑤public abstract class String{......}
⑥public final class Class22 extends implement interface1{......}
3:參考答案:(P180)RandomAccessFile類
FileNotFoundException異常
考試題內容:
文件隨機訪問應該利用什么類來實現(xiàn)?創(chuàng)建此類對象的代碼應該能捕獲什么類型的異常?
4:參考答案:(P14)b=b+(++a);或者b+=++a;
[解析]這是一個表達式語句,不要忘記加分號。不能寫成b=b+++a;這相當于b=(b++)+a。
考試題內容:
用一個Java語句完成下面的要求:
先將整型變量a的值加1,再把它加到變量b中。
5:參考答案:(P14)c=a-(b--);或c=a-b--;
考試題內容:
用一個Java語句完成下面的要求:
先把變量a與b的差賦值給變量c,再把變量b的值減1。
6:參考答案:(P141)g.fill3DRect(200,100,200,400,true);
考試題內容:
請寫一條語句,實現(xiàn)用預定的顏色填充一個突出顯示的矩形色塊,其中矩形左上角坐標為(200,100),右下角坐標為(400,500),調用的是類型為Graphics的g對象。
本文關鍵詞:Java語言程序設計,由筆耕文化傳播整理發(fā)布。
本文編號:250647
本文鏈接:http://sikaile.net/wenshubaike/mishujinen/250647.html