2012年1月自學(xué)考試Java語言程序設(shè)計(jì)(一)試題
課程代碼:04747
一、單項(xiàng)選擇題(本大題共10小題,每小題1分,共l0分)
在每小題列出的四個(gè)備選項(xiàng)中只有一個(gè)是符合題目要求的,請將其代碼填寫在題后的括號內(nèi)。錯(cuò)選、多選或未選均無分。
1.下面供選字符序列中,不屬于...Java語言關(guān)鍵字的是( A )
A.inner
C.false
2.表達(dá)式“-1>>>1”的十進(jìn)制值是(B )
A.-2
C.-(231-1) B.231-1 D.232-1 B.throw D.throws
3.Java語言中,在類定義時(shí)用final關(guān)鍵字修飾,是指這個(gè)類( C )
A.子類必須實(shí)現(xiàn)父類未實(shí)現(xiàn)的方法
C.必須要有實(shí)例 B.沒有具體實(shí)現(xiàn)代碼 D.不能被繼承
4.表達(dá)式 "java程序設(shè)計(jì)".1ength() 的值是( C )
A. 0
C. 8 B.12 D.13
5.以下關(guān)于BorderLayout布局的敘述中,不正確的是( C ) ...
A.把容器內(nèi)的空間劃分成5個(gè)區(qū)域
C.是框架窗口的默認(rèn)布局 B.加入組件應(yīng)該指明要放入的區(qū)域 D.一個(gè)位置可直接放多個(gè)組件
6.利用文件對話框打開或保存文件,在打開文件對話框之前,可用FileFilter類設(shè)置篩選條件,其所用的兩個(gè)方法是(C )
A.accept()和getSelectedFile()
C.accept()和setDescription() B.accept()和getDescription() D.setDescription()和getDescription()
7.設(shè)已經(jīng)有Graphics2D對象g2d,RoundRectangle2D對象rRect,繪制對象rRect的代碼是( B )
A.g2d.draw(rRect)
B.g2d.drawRoundRect(rRect) 1
C.rRect.draw() D.rRect.drawRoundRect()
8.以下關(guān)于線程互斥和同步的敘述中,正確的是( D )
A.臨界段是線程互斥使用資源的程序段
C.互斥和同步能很好地解決死鎖問題 B.臨界段能使線程使用其它線程的資源 D.可用notify()方法讓等待線程結(jié)束等待
9.在編寫訪問數(shù)據(jù)庫的Java程序時(shí),Connection對象的作用是( C )
A.存儲查詢結(jié)果
C.用來表示與數(shù)據(jù)庫的連接 B.在指定的連接中處理SQL語句 D.用來創(chuàng)建數(shù)據(jù)庫的連接
10.獲取InetAddress對象的域名所用到的方法是( C )
A.getHostName()
C.getHostAddress() B.getLocalName() D.getLocalHost()
二、填空題(本大題共10小題,每空2分,共20分)
請?jiān)诿啃☆}的空格中填上正確答案。錯(cuò)填、不填均無分。
11.能在瀏覽器上執(zhí)行的Java程序被稱為____________。
12.如果子類A隱藏了超類B的成員變量x,而類A的某個(gè)方法f()要引用類B的x,則引用類B的x的引用代碼可以寫成____________。
13.接口有與類相似的基本形式,它有常量定義和方法定義,但沒有____________。
14.設(shè)u和v是類A的兩個(gè)對象,則邏輯表達(dá)式“u==v”的意義是____________。
15.要獲得對象的字符串表示,調(diào)用的方法是____________。
16.注冊鼠標(biāo)點(diǎn)擊事件監(jiān)視器的方法是__mouseListener_()_________。
17.在異或模式下繪圖時(shí),實(shí)際繪制的顏色是由當(dāng)前正要繪制的顏色、原先繪制的顏色,以及____________作特定的運(yùn)算確定的。
18.多線程程序設(shè)計(jì)中,讓執(zhí)行臨界段程序時(shí)等待的線程結(jié)束等待,通常使用的方法有resume()、notify()或者_(dá)___________。
19.創(chuàng)建的流既可以隨機(jī)輸出,也可以隨機(jī)輸入的類是____________。
20.Java程序中使用套接字方法實(shí)現(xiàn)C/S模式時(shí),在Client端支持網(wǎng)絡(luò)底層通訊的類是____________。
三、簡答題(本大題共6小題,每小題3分,共18分)
21.請寫出以下程序段的執(zhí)行結(jié)果。
int c=0,d,n=3758;
while(n>0){ if((d=n%l0)>c) c=d;n/10;}
System.out.println("結(jié)果="+c);
22.什么是方法重載,如何正確區(qū)分重載?
23.請寫出定義一個(gè)包含2個(gè)子數(shù)組的二維整型數(shù)組對象aArr的代碼,其中第一個(gè)子數(shù)組有5個(gè)元素,第二個(gè)子數(shù)組有3個(gè)元素。
24.請寫出創(chuàng)建一個(gè)JPanel對象p,并為對象p設(shè)定GridLayout布局的代碼。
JPanel p=new JPanel();
setGridLayout(d);
25.由于創(chuàng)建輸出流對象時(shí),可能會出現(xiàn)IOException異常,所以創(chuàng)建輸出流對象的代碼必須出現(xiàn)在try-catch語句結(jié)構(gòu)中。請寫出為當(dāng)前目錄下的myFile.dat文件創(chuàng)建緩沖輸出流對象outs的代碼。
26.請寫出已知數(shù)據(jù)庫url、用戶名uName和密碼pWord,通過驅(qū)動程序管理器(DriverManager)的方法getConnection()建立連接con的代碼,可能產(chǎn)生的異常是SQLException。
四、程序填空題(本大題共5小題,每空2分,共20分)
在每小題的空格中填上正確答案,完全代碼,使它能正確實(shí)現(xiàn)題意的功能。錯(cuò)填、不填均無分。
27.方法int tenDigNumlnArray(int []a,int d)的功能是統(tǒng)計(jì)數(shù)組中十位數(shù)是d的整數(shù)的個(gè)數(shù)。 提示:對于一個(gè)整數(shù)n,運(yùn)算n/10的作用是求得去掉n的個(gè)位數(shù)后的整數(shù),運(yùn)算n%10的作用是求得n的個(gè)位數(shù)。
static int tenDigNumlnArray(int []a,int d){
int c=0;
for(int i=0;__i>d________;i++){
if(__________)c++;
}
return c;
}
28.某小應(yīng)用程序的類實(shí)現(xiàn)了接口ActionListener,在其窗口有一個(gè)文本區(qū)對象text和一個(gè)標(biāo)上文字“輸入一個(gè)整數(shù)”的按鈕對象,并正確為該按鈕對象注冊了監(jiān)視器。按鈕處理程序要實(shí)現(xiàn)的功能是當(dāng)點(diǎn)擊這個(gè)按鈕時(shí),程序彈出一個(gè)輸入信息的對話框,輸入一個(gè)整數(shù)n,程序接受n后,將2至n的所有偶數(shù)輸出在文本區(qū)text中。以下是其中處理按鈕事件的方法。
public void actionPerformed(ActionEvent e){
if(e. __________.equals("輸入一個(gè)整數(shù)")){
String res=(String) JOptionPane.showInputDialog(null, "輸入一個(gè)正整數(shù)",
"輸入對話框",JOptionPane.PLAIN_MESSAGE,null,null,null);
n=Integer.parseInt(res);
text.setText("");.
for(int k=2;k <= n;k+=2)
text._____________;
}
}
29.一個(gè)示意選擇框選擇的類CheckBoxWin,類的構(gòu)造方法根據(jù)給定的選擇項(xiàng)目表構(gòu)造一組選擇框,這組選擇框允許多選,當(dāng)這組選擇框中的某個(gè)選擇項(xiàng)的選擇狀態(tài)有改變(從選中變成未選中,或從未選中變成選中)時(shí),選擇框的監(jiān)視程序在一個(gè)文本區(qū)中輸出那個(gè)狀態(tài)有改變的選擇項(xiàng)目。以下是類CheckBoxWin的定義。
class CheckBoxWin extends JFrame implements ItemListener{
JPanel p=new JPanel();
JTextArea text;
String[]nameList;
JCheckBox boxArray[];
CheckB oxWin(String[]sp){
Container con=getContentPane();
con.setBackground(Color.BLUE);
con.setLayout(new FlowLayout());
p.setSize(90,(sp.1ength)*20);
p.setLayout(new GridLayout(sp.1ength,1));
nameList=sp;
boxArray=new JCheckB ox[sp.1ength];
for(int i=0;i
JCheckBox box=new JCheckBox(sp[i]);
box._________________;
p.add(box);
boxArray[i]=box;
}
con.add(p);
text=new JTextArea(3,13);
text.setText("");
JScrollPane jsp=new JScrollPane(text);
jsp.setSize(100,60);
con.add(jsp);
setSize(100,(sp.1ength)*20+120);
setLocation(100,100);
setVisible(true);
}
public void itemStateChanged(ItemEvent e){
for(int i=0;i
if(e.getItemSelectable()==_______________________ ){
if(boxArray[i].isSelected())
text.append(nameList[i]+".從未選中變成被選中\n");
else
text.append(nameList[i]+":從選中變成未被選中\n");
return;
}
}
}
}
30.以下是一個(gè)JPanel的子類的paintComponent方法,該方法利用Graphics2D繪制一個(gè)正方形,正方形的位置、邊長和顏色分別存于該類的成員變量lTop、edge和c中。
int edge; Point 1Top; Color c;
public void paintComponent(Graphics g){
Graphics2D g2=(Graphics2D)g;
g2.setColor(c);
Rectangle2D rec=new Rectangle2D.____________(1Top.x,lTop.y,edge,edge);
_____________;
}
31.以下小應(yīng)用程序的界面有一個(gè)文本區(qū),文本區(qū)同時(shí)作為發(fā)生鍵盤事件的事件源,并對鍵盤事件實(shí)施監(jiān)視。程序運(yùn)行時(shí),先點(diǎn)擊文本區(qū),讓它激活,以便能響應(yīng)鍵盤事件。以后輸入英文字母時(shí),在文本區(qū)中順序顯示輸入的字母。
import java.applet.*;import javax.swing.*;
import java.awt.*;import java.awt.event.*;
public class Test31 extends Applet implements____________{
JTextArea text=new JTextArea(5,10);
public void init(){setSize(200,200);
__________________________;
text.setBackground(Color.gray); add(text);
}
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){
int t=e.getKeyCode();
if(t>=KeyEvent.VK_A&&t<=KeyEvent.VK_Z){
text.append(""+(char)t);
}
}
}
五、程序分析題(本大題共5小題,每小題4分,共20分)
32.閱讀下列程序,請寫出該程序的功能。
public class Test32
{ public static void main(String args[])
{ double sum=0.0;
For (int i=1; i<=500;i++)
sum+=1.0/(double)i;
System.out.println( "sum="+sum);
}
}
33.閱讀下列程序,請回答以下問題:
(1)界面中有哪些組件?
(2)點(diǎn)擊每一個(gè)按鈕分別會顯示什么內(nèi)容?
import java.applet.*; import java.awt.*;
import java.awt,event.*;import javax.swing.*;
public class Test33 extends Applet implements ActionListener{
String msg="";
String buttonCom[]={"Yes","No","Undecided"};
JButton bList[]=new JButton[buttonCom.length];
JTeXtField t;
public void init(){
setLayout(new GridLayout(4,1));
for(int i=0;i
bList[i]=new JButton("按鈕"+(i+1));
add(bList[i]);
bList[i].addActionListener(this);
}
t=new JTeXtField();
add(t);
}
public void actionPerformed(ActionEvent e){
for(int i=0;i<3;i++){
if(e.getSource()一bList[i]){
t.setTeXt("You pressed"+buttonCom[i]);
break;
}
}
}
}
34.閱讀下列程序,請寫出該程序的功能。
import java.io.*; import java.awt.*; import javax.swing.*;
import java.awt.event.*;
class MyWindow extends JFrame implements ActionListener{
JTextArea text;BufieredReader in;JTextField fileName;
FileReader file;
MyWindow(){
Contaiher con=this.getContentPane();//獲得內(nèi)容面板
con.setLayout(new BorderLayout());
fileName=new JTextField("輸入文件名");
fileName.addActionListener(this);
text=new JTextArea(20,30);
JScrollPane jsp:new JScrollPane(text);
con.add(jsp,BorderLayout.CENTER);
con.add(fileName,"South");setVisible(true);
}
public void actionPerformed(ActionEvent e)
{ String s;
try{File f=new File(fileName.getText());
file=new FileReader(f);
in=new BufferedReader(file);
} catch(FileNotFoundException el){}
try{ while((s=in.readLine())!=null)
text.append(s+'\n');
}catch(IOException exp){}
}
}
public class Test34{
public static void main(String args[ ]) {new MyWindow();}
}
35.閱讀下列程序,請回答以下問題:
(1)該程序中的類MyPanel的成員變量twoClick的作用是什么,該成員變量的值是如何改變的?
(2)程序運(yùn)行時(shí),用戶在界面的不同位置,用鼠標(biāo)點(diǎn)擊兩次,界面中會出現(xiàn)什么? import java.awt.*;import javax.swing.*;import java.awt.event.*;
class MyPanel extends JPanel implements MouseListener{
int leftx,lefly,rightx,righty;
boolean twoClick;
MyPanel() {
twoClick=false;addMouseListener(this);
}
public void mousePressed(MouseEvent e){
if(!twoClick){
leftx=e.getX();lefty=e.getY();
}else{
rightx=e.getX();righty=e.getY();repaint();
}
twoClick=!twoClick:
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void paintComponent(Graphics g){
g.clearRect(0,0,this.getWidth(),this.getHeight());
g.fillRect(1efix,lefty,Math.abs(rightx-leftx),
Math.abs(righty-lefty));
}
}
class MyWin extends JFrame{
MyPanel p=new MyPanel();
MyWin(){
getContentPane().add(p);
setSize(400,400);setLocation(100,100);setVisible(true);
}
}
public class Test35 extends JFrame{
public static void main(String[]args){new MyWin();}
}
36.閱讀下列程序,請回答以下問題:
(1)程序執(zhí)行時(shí)創(chuàng)建的線程個(gè)數(shù)。
(2)各線程的名稱。
(3)舉例給出程序可能的輸出結(jié)果。
class SeltManaged extends Thread{
int countDown;
public SelfManaged(String name,int c){
countDown=c;
setName(name);start();
}
public void run(){
while(true){
System.out.println(getName()+" ("+countDown+")");
try{
sleep(50);
}catch(InterruptedException e){};
if(--countDown== 0)
return;
}
}
}
public class Test36{
public static void main(String[]args){
for(int i=0;i<2;i++)
new SelfManaged("線程"+String.valueOf(i),2);
}
}
六、程序設(shè)計(jì)題(本大題共2小題,每小題6分,共12分)
37.編寫方法int[] delete(int []a,int d),方法將創(chuàng)建一個(gè)新數(shù)組b,新數(shù)組b中只包含原數(shù)組中所有值不等于d的元素,并返回該新數(shù)組b。
38.以下程序的界面有一個(gè)按鈕ave、一個(gè)文本區(qū)text和一個(gè)標(biāo)簽label。程序運(yùn)行時(shí),在文本區(qū)中輸入數(shù)字序列,單擊ave按鈕,則在標(biāo)簽label中顯示這個(gè)數(shù)字序列中正實(shí)數(shù)的平均值。
注:這里是給定程序的部分代碼,你要編寫的是actionPerformed(ActionEvent e)方法。 import javax.swing.*;
class Ave extends JFrame implements ActionListener{
JLabel label 1,label; JTextArea text; JButton ave; Ave() {
Container con=getContentPane();
con.setLayout(new GridLayout(2,2));
labell=new JLabel("輸入整數(shù)序列");
text=new JTextArea(5,10);
ave=new JButton("求正實(shí)數(shù)平均值");
label=new JLabel();
con.add(1abel 1);con.add(text);con.add(ave); ave.addActionListener(this);
con.add(1abel);
}
public void actionPerformed(ActionEvent e){
∥請?jiān)谝韵挛恢镁帉懘a
}
}
class Test38{
public static void main(String args[]){new Ave(); } }