全國(guó)2010年1月自考Java語(yǔ)言程序設(shè)計(jì)(一)試題
課程代碼:04747
一、單項(xiàng)選擇題(本大題共10小題,每小題1分,共10分)
在每小題列出的四個(gè)備選項(xiàng)中只有一個(gè)是符合題目要求的,請(qǐng)將其代碼填寫(xiě)在題后的括號(hào)內(nèi)。錯(cuò)選、多選或未選均無(wú)分。
1.在下述字符串中,不屬于Java語(yǔ)言關(guān)鍵字的是( )
A.float B.new
C.java D.return
2.Java語(yǔ)言中數(shù)值數(shù)據(jù)的類型能自動(dòng)轉(zhuǎn)換,按照從左到右的轉(zhuǎn)換次序?yàn)? )
A.byte→int→short→long→float→double
B.byte→short→int→long→float→double
C.byte→short→int→float→long→double
D.short→byte→int→long→float→double
3.在以下供選擇的概念中,不屬于面向?qū)ο笳Z(yǔ)言概念的是( )
A.消息B.模塊
C.繼承D.多態(tài)性
4.在下述Java語(yǔ)言語(yǔ)句中,錯(cuò)誤的創(chuàng)建數(shù)組的方法是( )
A.int intArray [ ];intArray=new int[5];
B.int intArray [ ]=new int[5];
C.int [ ] intArray ={1,2,3,4,5};
D.int intArray [5]={1,2,3,4.5};
5.在Swing中,它的子類能用來(lái)創(chuàng)建框架窗口的類是( )
A.JWindow B.JFrame
C.JDialog D.JApplet
6.MouseListener接口不能處理的鼠標(biāo)事件是( )
A.按下鼠標(biāo)左鍵B.點(diǎn)擊鼠標(biāo)右鍵
C.鼠標(biāo)進(jìn)入D.鼠標(biāo)移動(dòng)
7.以下不屬于文字字型要素的是( )
A.顏色B.字體
C.風(fēng)格D.字號(hào)
8.在以下四個(gè)供選的整數(shù)中,能作為線程最高優(yōu)先級(jí)的整數(shù)是( )
A.0 B.1
C.10 D.11
9.Java語(yǔ)言可以用javax.swing包中的類JFileChooser來(lái)實(shí)現(xiàn)打開(kāi)和保存文件對(duì)話框。用戶通過(guò)文件對(duì)話框不可能獲得的信息是( )
A.文件名稱B.文件路徑
C.文件內(nèi)容D.文件對(duì)象
10.在編寫(xiě)訪問(wèn)數(shù)據(jù)庫(kù)的Java程序時(shí),Connection對(duì)象的作用是( )
A.用來(lái)表示與數(shù)據(jù)庫(kù)的連接B.存儲(chǔ)查詢結(jié)果
C.在指定的連接中處理SQL語(yǔ)句D.建立新數(shù)據(jù)庫(kù)連接
二、填空題(本大題共10小題,每空2分,共20分)
請(qǐng)?jiān)诿啃☆}的空格中填上正確答案。錯(cuò)填、不填均無(wú)分。
11.Java的編譯程序先將Java源程序翻譯成與機(jī)器無(wú)關(guān)的__________。
12.表達(dá)式¨23 & 18’’的值是__________。
13.如果類A某個(gè)成員變量的類型是類B,則類A和類B之間是__________關(guān)系。
14.Java語(yǔ)言提供了兩個(gè)用于處理字符串的類,它們是__________。
15.某程序用代碼“JTextField text=new J TextField();”創(chuàng)建了一個(gè)文本框,并對(duì)這個(gè)文本框注冊(cè)了監(jiān)視器,則在監(jiān)視器處理方法actionPerformed(ActionEvent e)中,判定是在這個(gè)文本框上發(fā)生事件的條件表達(dá)式是__________。
16.一個(gè)水平滾動(dòng)條對(duì)象的初始值是50,滑塊的寬是10個(gè)像素,表示的范圍是[0,250]。創(chuàng)建這樣的滾動(dòng)條對(duì)象時(shí),提供的5個(gè)參數(shù)依次是__________。
17.要在JComponent 子類的組件中繪圖,應(yīng)重寫(xiě)__________方法。
18.當(dāng)線程使用完臨界段后,為了使等待的線程恢復(fù)工作,需要調(diào)用的方法是__________。
19.某程序想為一個(gè)long型整數(shù)文件構(gòu)造隨機(jī)讀寫(xiě)對(duì)象,支持這個(gè)要求的類是__________。
20.設(shè)已知服務(wù)器的IP地址和端口號(hào),要?jiǎng)?chuàng)建一個(gè)套接字對(duì)象,支持這個(gè)要求的類是__________。
三、簡(jiǎn)答題(本大題共6小題,每小題3分,共18分)
21.請(qǐng)寫(xiě)出一段if語(yǔ)句,判斷一個(gè)整數(shù)n是否為偶數(shù)。如果是偶數(shù),輸出“Yes.”;否則,輸出“No.”。22.請(qǐng)寫(xiě)出接口定義的一般形式。
23.請(qǐng)寫(xiě)出Java語(yǔ)言編寫(xiě)事件處理程序的兩種主要方案。
24.設(shè)已經(jīng)有Graphics2D對(duì)象g2d, 請(qǐng)用Java語(yǔ)句繪制一個(gè)矩形rect,其左上角坐標(biāo)是(20,30),寬是120,高是30。
25.打開(kāi)文件對(duì)話框時(shí),還可以設(shè)置篩選條件,即指定文件的類型。請(qǐng)寫(xiě)出FileFilter類預(yù)設(shè)的兩個(gè)方法。26.請(qǐng)寫(xiě)出URLConnection類提供的三個(gè)常用方法。
四、程序填空題(本大題共5小題,每空2分,共20分)
2010年01月試題 2
27.方法void moveOddForword(int a[ ])的功能是將數(shù)組中的所有奇數(shù)移到所有偶數(shù)之前。void moveOddForword(int a[]){
for(int i=0,odd=0;________;i++)
if(________){
int t=a[i];a[i]=a[odd];a[odd]=t;odd++;
}
}
28.以下程序創(chuàng)建了一個(gè)窗口,然后在窗口內(nèi)顯示″Hello,World!″。
import javax.swing.*;import java.a(chǎn)wt*;
public class HelloWorld {
public static void main(String[ ]ares) {
TextFrame frame=new TextFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame. ________;
}
}
class TextFrame extends JFrame {
public TextFrame() { .
setTitle(″HelloWorld″);
setSize(WIDTH,HEIGHT);
TextPanel panel=new TextPanel();
Container contentPane=getContentPane();
contentPane.a(chǎn)dd(panel);
}
public static final int WIDTH=300;
public static final int HEIGHT= 200;
}
class TextPanel extends JPanel {
public void paintComponent(Graphics g) {
super. ________;
g.drawString(″Hello,World! ″,x,y);
}
public int x=100;public int y=100;
2010年01月試題 3
}
29.以下是子窗口中設(shè)置一個(gè)菜單條類的定義。類的構(gòu)造方法根據(jù)指定的窗口名稱和菜單表設(shè)置菜單條,菜單和菜單項(xiàng),當(dāng)選中某個(gè)菜單項(xiàng)時(shí),在文本框中顯示相應(yīng)菜單項(xiàng)被選中的信息。
class MenuWindow extends JFrame implements ActionListener {
public static JTextField text;
public MenuWindow(String s,String menuList[][]) {
setTitle(s);
Container con=this.getContentPane();
con.setLayout(new BorderLayout());
this.setLocation(100,100);this.setSize(300,100);
JMenuBar menubar=new JMenuBar();
for(int i=0; i
JMenu menu=new JMenu(menuList[i][0]);
for(int j=1;j
JMenultem anltem=new JMenultem(menuList[i][j]);
anltem.setActionCommand(menuList[i][j]);
anltem.________;menu.add(anltem);
}
menubar.__________;
}
text=new JTextField();setJMenuBar(menubar);
con.add(text,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e){
text.setText(e.getActionCommand()+″菜單項(xiàng)被選中! ″);
}
}
public class Test29 extends Applet {
MenuWindow window;
String menuList[][]={{″體育″,″跑步″,″打藍(lán)球″,″打乒乓″},
{″娛樂(lè)″,″唱歌″,″跳舞″}};
public void init() {
window=new MenuWindow(″體育娛樂(lè)之窗″,menuList);
2010年01月試題 4
window.setVisible(true);
}
}
30.以下是一個(gè)用鼠標(biāo)自由作畫(huà)的小應(yīng)用程序。最簡(jiǎn)單的方法是根據(jù)鼠標(biāo)所在位置畫(huà)點(diǎn),跟隨鼠標(biāo)的移動(dòng),不斷畫(huà)圓點(diǎn),就能實(shí)現(xiàn)用鼠標(biāo)作畫(huà)。
import java.a(chǎn)wt.*;
import java.a(chǎn)wt.event.*;
public class Test30 extends java.a(chǎn)pplet.Applet implements MouseMotionListener {
Color color;int lineSize=2:
int x=-1,y=-l;
public void init(){
setLocation(30,20);setSize(300,300);
setBackground(Color.green);addMouseMotionListener(this);
}
public void paint(Graphics g){
if(x!=-1&&y!=-1) {
g.setColor(color);g.fillOval(x,y,1ineSize,lineSize);
}
}
public void mouseMoved(MouseEvent e){}
public void mouseDragged(MouseEvent e){
x=e.getX();y=e.getY();_________;
}
public void ________ (Graphics g){ paint(g);}
}
31.以下定義的類ShareData用于管理多個(gè)線程共享數(shù)據(jù)data。一個(gè)線程生成data,另一個(gè)線程使用data。約定,新生成的data只有被另一個(gè)線程使用后,才能生成下一個(gè)data。反之,一個(gè)data被使用后,也不能再繼續(xù)使用。所以,生成和使用data的線程之間需要互斥和同步。以下是管理上述使用方式的類,類內(nèi)有要管理的共享數(shù)據(jù),以及對(duì)共享數(shù)據(jù)的存操作putData()和取操作getData()。
class ShareData{
int data;∥共享數(shù)據(jù)
boolean newData=false;∥有最近新生成data的標(biāo)志
synchronized int getData(){
2010年01月試題 5
while(!newData){
try{ _________;
} catch(InterruptedExceptipn e){
System.out.println(″因錯(cuò)誤,而中斷!″);
}
}
newData=false;notify();return data;
}
synchronized void putData(int n){
while(newData){
try{wait();
}catch(InterruptedException e){
System.out.println(″因錯(cuò)誤,而中斷! ″);
}
}
data=n;__________;
notify();return;
}
}
五、程序分析題(本大題共5小題,每小題4分,共20分)
32.閱讀下列程序,請(qǐng)寫(xiě)出該程序的輸出結(jié)果。
class Parent{
private void method 1 () { System.out.println(″Parent′s method 1()″);}
public void method 2 () { System.out.println(″Parent′s method 2()″);method 1();} }
class Child extends Parent {
public void method l (){ System.out.println(″Child′s method 1 ()″);}
public static void main(String args[]){ Parent p = new Child();p.method2();}
}
33.閱讀下列程序,請(qǐng)寫(xiě)出該程序的功能。
import java.util.*;import javax.swing.*;
public class Test33{
public static void main(String args[]){
2010年01月試題 6
String str=(String)JOptionPane.showInputDialog(null,″請(qǐng)輸入信息″,
″輸入對(duì)話框″,JOptionPane.PLAIN_MESSAGE,null,null,null);
StringTokenizer pas=new StringTokenizer(str, ″,″);
int n=pas.countTokens();
System.out.println(″輸入的信息有單詞:″+n+″個(gè),全部單詞如下:″);
while(pas.hasMoreTokens()){
String s=pas.nextToken();
System.out.println(s);
}
}
)
34.閱讀下列程序,請(qǐng)用示意圖畫(huà)出程序運(yùn)行時(shí)呈現(xiàn)的界面。
import java.applet.*;import java.awt.*;import javax.swing.*;
class MyPanel extends JPanel{
JTextField textl,text2;
MyPanel(String sl,String s2) {
textl=new JTextFieId(s1);text2=new JTextField(s2);
add(text 1);add(text2);
}
}
class MySubPanel extends MyPanel {
JTextField text;
MySubPanel(String sl,String s2,String s3) {
super(s1,s2);text = new JTextField(s3);add(text);
}
}
public class Test34 {
public static void main(String args[]) {
JFrame mw=new JFrame(″一個(gè)示意窗口″);
mw.setSize(350,150);
Container con = mw.getContentPane();
con.setLayout(new BorderLayout());
MyPanel pl=new MyPanel(″文本框l″,″文本框2″);
2010年01月試題7
MySubPanel p2=new MySubPanel(″文本框3″,″文本框4″,″文本框5″);
JTextArea text=new JTextArea(″這里是一個(gè)文本區(qū)″);
con.add(pl,″North″);con.add(p2,″South″);
con.add(text,″Center″);mw.setVisible(true);
}
}
35.閱讀下列程序,請(qǐng)寫(xiě)出該程序的功能。
import java.applet.*:import java.awt.event.*;import javax.swing.*
public class Class 1 extends Applet implements KeyListener{
JButton button=new JButton(″開(kāi)始″);
JTextArea text=new JTextArea(5,20);
public void init() {
button.addKeyListener(this);add(button);add(text);
}
public void keyPressed(KeyEvent e){
int t=e.getKeyCode();
if(t>=KeyEvent.VK_A&& t<=KeyEvent.VK_Z) {
text.append(″″+(char)t);
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}
36.閱讀下列程序,請(qǐng)寫(xiě)出該程序的功能。
import java.applet.*;import java.awt.*;
public class Test36 extends java.applet.Applet implements Runnable{
Thread myThread = null;
double seta=0.0;
public void start() {
setSize(500,400);
if(myThread=null){ myThread=new Thread(this);myThread.start();} }
public void run() {
2010年01月試題8
while(myThread!=null) {
try {myThread.sleep(40);
} catch(InterruptedException e){}
seta+=3.0;if(seta>=360)seta=0;repaint();
}
}
public void paint(Graphics g) {
final double pi=3.14159;final double r = 100.0;
int x0=250+(int)(r*Math.cos(3.1415926/180.0*seta));
int y0=200+(int)(r*Math.sin(3.1415926/180.0*seta));
g.setColor(Color.red);g.drawOval(x0,y0,10,10);
}
}
六、程序設(shè)計(jì)題(本大題共2小題,每小題6分,共1 2分)
37.請(qǐng)編寫(xiě)方法void strReverse(String str),該方法的功能是輸出一個(gè)新字符串,新字符串字符排列順序與原字符串str的字符排列順序相反。例如,strReverse(″ABCD″) 所輸出的結(jié)果是″DCBA″。請(qǐng)使用字符串與字節(jié)數(shù)組的相互轉(zhuǎn)換方法進(jìn)行設(shè)計(jì)。
38.請(qǐng)?jiān)O(shè)計(jì)實(shí)現(xiàn)如下用于輸入學(xué)號(hào)和姓名的對(duì)話框界面,其中空白格是文本框,用于輸入相應(yīng)的內(nèi)容。
這里給出的是程序的一部分,你要編寫(xiě)的是類InputNoNameDialog的構(gòu)造方法InputNoNameDialog(JFrame f,String s,JTextField t)。其中參數(shù)f是對(duì)話框的依賴窗口,s是對(duì)話框標(biāo)題,t是依賴窗口中顯示對(duì)話框輸入內(nèi)容的文本框。
以下是類InputNoNameDialog的程序框架。
class InputNoNameDialog extends JDialog implements ActionListener{
JLabel title;JTextField textl,text2,mainText;JButton done;
InputNoNameDialog(JFrame f String s,JTextField t) {
super(f,s,true);mainText = t;Container con = getContentPane();
title=new JLabel(s);textl=new JTextField(10);
text2=new JTextField(10);con.setLayout(new GridLayout(3,2));
2010年01月試題9
con.setSize(200,100);setModal(false);
//請(qǐng)?jiān)谝韵挛恢美m(xù)寫(xiě)其余代碼
}
public void actionPerformed(ActionEvent e) {
//輸入結(jié)束按確定按鈕后,將對(duì)話框中輸入的學(xué)號(hào)和姓名在它依賴窗口的文本框中顯示。
mainText.setText(″學(xué)號(hào):″+textl.getText()+″姓名:″+text2.getText());
setVisible(false);dispose();
}
}
2010年01月試題10