[计算机类试卷]国家二级(JAVA)机试模拟试卷71及答案与解析.doc

上传人:eventdump275 文档编号:503607 上传时间:2018-11-29 格式:DOC 页数:6 大小:39.50KB
下载 相关 举报
[计算机类试卷]国家二级(JAVA)机试模拟试卷71及答案与解析.doc_第1页
第1页 / 共6页
[计算机类试卷]国家二级(JAVA)机试模拟试卷71及答案与解析.doc_第2页
第2页 / 共6页
[计算机类试卷]国家二级(JAVA)机试模拟试卷71及答案与解析.doc_第3页
第3页 / 共6页
[计算机类试卷]国家二级(JAVA)机试模拟试卷71及答案与解析.doc_第4页
第4页 / 共6页
[计算机类试卷]国家二级(JAVA)机试模拟试卷71及答案与解析.doc_第5页
第5页 / 共6页
点击查看更多>>
资源描述

1、国家二级( JAVA)机试模拟试卷 71及答案与解析 一、基本操作题( 30分) 1 下面程序是关于类的继承的用法。阅读下面程序,根据程序中的注释在每一条横线处填写一个语句,使程序的功能完整,且运行程序后的输出结果为: I am parentclass! I am childclass! I am childclass! 注意:请勿改动 main()主方法和其他已有的语句内容,仅在下划线处填入适当的语句。 class Parent void printMe() System. out. println( “I am parentclass ! “); class Child extends P

2、arent void printMe() System. out. println( “I am childclass! “) void printAll() _. printMe(); /调用父类的方法 _. printMe(); /调用本类的方法 printMe ( ); public class TestJieCheng public static void main(String args) _ myC. printAll(); 二、简单应用题( 40分) 2 请完成下列 Java程序:创建一个具有 2行 3列的 GridLayout管理器,包括Choice,Label,Button构

3、件,布局为 第 1行包括一个 Choice构件 (包括 2个选项 item1和 item2),一个 Label构件 (当选择 Choice构件中的选项时, Label构件显示相应的名称,即,如果点击 item1则 Label中显示 item1),和一个 exit按钮 (点击则退出应用程序 ),第 2行包括 3个 Button构件。程序运行结果如下: 注意:请勿改动 main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。 import java.awt.*; import java.awt.event.*; public class ex1_2 extends Frame imple

4、ments ActionListener, ItemListener private Label 1; private String str=“label“; private Choice choice1_2; public static void main(String arg) new ex1_2(); ex1_2() setLayout(_); choice1_2=new Choice(); choice1_2.addltem(“item1“); choice1_2.addltem(“item2“); choice1_2 _; add(choice1_2); 1=new Label(st

5、r); add(1); Button exit1_2=new Button(“exit“); exit1_2.addActionListener(this); add(exit1_2); for(int i=0; i 3; i+) add(new Button(“button“+i); setsize(300, 300); pack(); show(); public void actionPerformed(ActionEvent event) if(event.getActionCommand() equals(“exit“) System.exit(0); public void ite

6、mStateChanged(ItemEvent event) str=choice1_2 getSelectedltem(); 1 setText(str); 三、综合应用题( 30分) 3 下面是一个 Applet程序,其功能是实现一个计数器,每隔 0.15秒计数器数值加1,数值动态变化,并且能够控制计数器的暂停和继续。要求通过使用 swing的构件建立图形用户界面,主要包括一个文本区域,用于显示计数器结果;两个按钮,一个使计数器暂停,一个使计数器继续工作。请改正程序中的错误 (有下划线的语句 ),使程序能输出正确的结果。注意:不改动程序的结构,不得增行或删行。 import javax,

7、swing. * import java. awt. * import java. awt. event. * /* applet code= “ex4_2. class“ width=800 height=400 /applet */ public class ex4_2 extends JApplet private JTextField jtf=new JTextField(15); private JButton Hold = new JButton (“Hold“), resume = new JButton ( “Resume“ ); private ex4_2th obj4_2t

8、h= new ex4_2th(); class ex4_2th extends Thread private int cnt=0; private boolean bIsHold=false; public ex4_2th() start(); public void hold() bIsHold=true public synchronized void fauxResume() bIsHold=false; wait( ); public void run() while (true) try sleep(150) synchronized(this) while(bIsHold) not

9、ify( ); catch(InterruptedException ie) System. err. println(“Interrupted“); jtf. setText(cnt); public void init() Container cp = getContentPane( ) cp. setLayout(new FlowLayout( ) ); cp. add(jtf) Hold. addActionListener( new ActionListener() public void actionPerformed(ActionEvent ae) obj4_2th. hold(

10、) ); cp. add(Hold); resume, addActionListener ( new ActionListener() public void actionPerformed(ActionEvent e) obj4_2th. fauxResume( ) ); cp. add(resume) public static void main(String args) ex4_2 obj4_2 =new ex4_2() String str= obj4_2. getClass(), toString(); if (str. indexOf(“class“)!=-1) str=str

11、. substring (6) JFrame frm=new JFrame(str) frm. addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent we) System. exit(0); ); frm. getContentPane(), add(obj4_2) frm. setSize(300,200) obj4_2. init() obj4_2. start( ); frm. setVisible(true) ex4_2. html HTML HEAD TITLE ex4_2 /TITLE

12、 /HEAD BODY applet code=“ex4_2. class“ width=800 height=400 /applet /BODY /HTML 国家二级( JAVA)机试模拟试卷 71答案与解析 一、基本操作题( 30分) 1 【正确答案】 super this Child myC=new Child(); 【试题解析】 本题主要考查 super, this关键字以及如何生成对象。主要是熟练super, this的用法、对象的生成。在本题中, super printMe();浯句的功能是调用父类的 printMe()方法, this printMe ();语句的功能是调用本类的

13、 printMe()方法, Child myC=new Child();语句的功能是生成 Child类的对象 myC。 二、简单应用题( 40分) 2 【正确答案】 new GridLayout(2, 3) addItemListener(this) 【试题解析】 本题主要考查 Java常用构件 Choice和高级事件 ItemEvent以及GridLayout布局管理器的用法。解题关键是,熟悉 GridLayout布局管理器和ItemEvent的用法。在本题中, ItemEvent在教材中并没有给出确切的用法,但是可以根据 ActionEvent的 addActionEvent()方法和重载

14、 actionPerformed()方法来完成对动作事件监听的用法来类推出 ItemEvent事件的监听方法,这里要求有根据已有知识进行举一反三的能力 。 三、综合应用题( 30分) 3 【正确答案】 notify() wait() jtf setText(Integer toString(cnt+) 【试题解析】 本题主要考查图形用户界面, swing以及线程同步、共享、死锁相结合的综合应用。解题关键是熟悉 wait()方法和 notify()方法的含义。 wait()必须被声明为 synchronized,这样它才能拥有对象锁。 fauxResume()把 bIsHold标志设为 false,并调用 notify(),为了唤醒 synchronized子句 中的 wait(),所以 notify()也必须被声明为 synchronized,这样才能在调用 notify()之前获得对象锁 (然后该对象锁才能在 wait()执行时被运用 )。本题中,第一和第二处,应该在 bIsHold为 true时调用 wait(),而在 fauxResume()中调用 notify();第三处,需要对 int类型作转换才能够作为 String类型输出,并且要对计数器变量 cnt做累加。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试资料 > 职业资格

copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1