全國(guó)2018年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ù)的類(lèi)型能自動(dòng)轉(zhuǎn)換,按照從左到右的轉(zhuǎn)換次序?yàn)?span style="white-space:normal;">( )
A. byte→int→short→long→float→double
B. byte→short→i nt→l ong→fl oat→double
C. byte→sho rt→int→float→long→double
D. short→byte→int→l ong→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)能用來(lái)創(chuàng)建框架窗口的類(lèi)是( )
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包中的類(lèi)JFileChooser來(lái)實(shí)現(xiàn)打開(kāi)和保存文件對(duì)話框。用戶通過(guò)文件對(duì)話框不可能獲得的信息是 ( )
A.文件名稱(chēng)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ǔ)查詢(xún)結(jié)果
C.在指定的連接中處理SQL語(yǔ)句D.建立新數(shù)據(jù)庫(kù)連接
二、填空題(本大題共10小題,每空2分,共20分>
請(qǐng)?jiān)诿啃☆}的空格中填上正確答案。錯(cuò)填、不填均無(wú)分。
11.Java的編譯程序先將Java源程序翻譯成與機(jī)器無(wú)關(guān)的__________。字節(jié)碼
12.表達(dá)式¨23&18’’的值是__________。
13.如果類(lèi)A某個(gè)成員變量的類(lèi)型是類(lèi)B,則類(lèi)A和類(lèi)B之間是__________關(guān)系。
14.Java語(yǔ)言提供了兩個(gè)用于處理字符串的類(lèi),它們是__________。String\StringBuffer
15.某程序用代碼“JTextField text=new JTextField<);”創(chuàng)建了一個(gè)文本框,并對(duì)這個(gè)文本框注冊(cè)了監(jiān)視器,則在監(jiān)視器處理方法actionPerformed (ActionEvent e>中,判定是在這個(gè)文本框上發(fā)生事件的條件表達(dá)式是__________。e.getSource(>==text
16.一個(gè)水平滾動(dòng)條對(duì)象的初始值是50,滑塊的寬是10個(gè)像素,表示的范圍是[0,250]。
創(chuàng)建這樣的滾動(dòng)條對(duì)象時(shí),提供的5個(gè)參數(shù)依次是__________。(P125>JScrollBar.HORIZONTAL,50,10,0,25 0
17.要在JComponent 子類(lèi)的組件中繪圖,應(yīng)重寫(xiě)__________方法。paintComponet(>
18.當(dāng)線程使用完臨界段后,為了使等待的線程恢復(fù)工作,需要調(diào)用的方法是__________。Notify(> 19.某程序想為一個(gè)long型整數(shù)文件構(gòu)造隨機(jī)讀寫(xiě)對(duì)象,支持這個(gè)要求的類(lèi)是__________。RandomAcces sFile
20.設(shè)已知服務(wù)器的IP地址和端口號(hào),要?jiǎng)?chuàng)建一個(gè)套接字對(duì)象,支持這個(gè)要求的類(lèi)是__________。Socket 三、簡(jiǎn)答題(本大題共6小題,每小題3分,共18分>
21.請(qǐng)寫(xiě)出一段if語(yǔ)句,判斷一個(gè)整數(shù)n是否為偶數(shù)。如果是偶數(shù),輸出“Yes.”;否則,輸出“No.”。
If(n%2==0>{System.out.println(“Yes”>。}els{System.out.println(“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。
Rectangle2D rect=new Rectangle2D.Double(20,30,120,30>
G2d.draw(rect>。
25.打開(kāi)文件對(duì)話框時(shí),還可以設(shè)置篩選條件,即指定文件的類(lèi)型。請(qǐng)寫(xiě)出FileFilter類(lèi)預(yù)設(shè)的兩個(gè)方法。accept(> getDescription(>
26.請(qǐng)寫(xiě)出URLConnection類(lèi)提供的三個(gè)常用方法。
getOutputStream(>
getInputStream(>
connect(>
四、程序填空題(本大題共5小題,每空2分,共20分>
27.方法void moveOddForword(int a[]>的功能是將數(shù)組中的所有奇數(shù)移到所有偶數(shù)之前。
void moveOddForword(int a[]>{
for(int i=0, odd=0。________;i++>i
if(________>{ a[i]%2= =0
int t=a[i];a[i]=a[odd];a[odd]=t;odd++;
}
}
28.以下程序創(chuàng)建了一個(gè)窗口,然后在窗口內(nèi)顯示″Hello,World!″。
importjavax.swing.*; importjava.a(chǎn)wt*;
public class HelloWorld{
public static void main(String[]ares> {
TextFrame frame=new TextFrame(>;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE>;
frame. ________; setVisable(true>
}
}
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. ________;paintComponent(g>
g.drawString(″Hello,World!″,x,y>;
}
public int x=100;public int y=100;
}
29.以下是子窗口中設(shè)置一個(gè)菜單條類(lèi)的定義。類(lèi)的構(gòu)造方法根據(jù)指定的窗口名稱(chēng)和菜單表設(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(intj=1;j
JMenultem anltem=new JMenultem(menuList[i][j]>;
anltem.setActionCommand(menuList[i][j]>;
anltem.________;menu.add(anltem>;ActionListener(this>
}
menubar.__________;add(menu>
}
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>;
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(>;_________;repaint(>
}
public void ________ (Graphics g>{paint(g>;} update(>。
}
31.以下定義的類(lèi)ShareData用于管理多個(gè)線程共享數(shù)據(jù)data。一個(gè)線程生成data,另一個(gè)線程使用data。約定,新生成的data只有被另一個(gè)線程使用后,才能生成下一個(gè)data。反之,一個(gè)data被使用后,也不能再繼續(xù)使用。所以,生成和使用data的線程之間需要互斥和同步。以下是管理上述使用方式的類(lèi),類(lèi)內(nèi)有要管理的共享數(shù)據(jù),以及對(duì)共享數(shù)據(jù)的存操作putData(>和取操作getData(>。
class ShareData{
int data;∥共享數(shù)據(jù)
boolean newData=false;∥有最近新生成data的標(biāo)志
synchronized int getData(>{
while(!newData>{
try{ _________; wait(>
} 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; __________; newData=true
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 method2 (> { System.out.println(″Parent′s method2(>″>;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(>;}
}
Parent′s method 2(>
Parent′s method 1(>
33.閱讀下列程序,請(qǐng)寫(xiě)出該程序的功能。
importjava.util.*;importjavax.swing.*;
public class Test33{
public static void main(String args[]>{
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>;
}
}
>
在輸入對(duì)話框中輸入文字,并以多好作為分割符輸出單詞個(gè)數(shù),及各個(gè)單詞
34.閱讀下列程序,請(qǐng)用示意圖畫(huà)出程序運(yùn)行時(shí)呈現(xiàn)的界面。
importjava.applet.*;importjava.awt.*;importjavax.swing.*;
class MyPanel extends JPanel{
JTextField textl,text2;
MyPanel(Stringsl,Strings2>{
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″>;
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ě)出該程序的功能。
importjava.applet.*:importjava.awt.event.*;importjavax.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>{}
}
程序功能:從鍵盤(pán)輸入字符如果是大寫(xiě)字母A-Z,則顯示在文本區(qū)中
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(>{
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í)針畫(huà)圓
六、程序設(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ì)。publicstaticvoid strReverse(String str>{
byte[] s=str.getBytes(>。
String t=""。
for(int i=s.length-1。i>=0。i-->{
t+=(char>s[i]。
}
System.out.println(t>。
}
38.請(qǐng)?jiān)O(shè)計(jì)實(shí)現(xiàn)如下用于輸入學(xué)號(hào)和姓名的對(duì)話框界面,其中空白格是文本框,用于輸入相應(yīng)的內(nèi)容。
這里給出的是程序的一部分,你要編寫(xiě)的是類(lèi)InputNoNameDialog的構(gòu)造方法InputNoNameDialog(JFram e f,String s,JTextField t>。其中參數(shù)f是對(duì)話框的依賴(lài)窗口,s是對(duì)話框標(biāo)題,t是依賴(lài)窗口中顯示對(duì)話框輸入內(nèi)容的文本框。
以下是類(lè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>>;
con.setSize(200,100>; setModal(false>;
//請(qǐng)?jiān)谝韵挛恢美m(xù)寫(xiě)其余代碼
done=new JButton("確定">。
done.addActionListener(this>。
con.add(title>。con.add(done>。
con.add(new JLabel("學(xué)號(hào)">>。con.add(textl>。
con.add(new JLabel("姓名">>。con.add(text2>。
setVisible(true>。}
public void actionPerformed(ActionEvent e>{
//輸入結(jié)束按確定按鈕后,將對(duì)話框中輸入的學(xué)號(hào)和姓名在它依賴(lài)窗口的文本框中顯示。mainText.setText(″學(xué)號(hào):″+textl.getText(>+″姓名:″+text2.getText(>>;
setVisible(false>;dispose(>;
}
}
11 / 11