《JAVA期末考试卷.doc》由会员分享,可在线阅读,更多相关《JAVA期末考试卷.doc(19页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、20102011学年第1学期Java程序设计期中考试题(1) 下列有关构造函数描述正确的是( C )。A、所有类都必须定义一个构造函数B、构造函数必须有返回值C、构造函数必须访问类的非静态成员D、构造函数可以初始化类的成员变量(2) 编译和运行下面代码时显示的结果是( A )。 public class ThisConstructorCall public ThisConstructorCall(String s) System.out.println(s = + s); public ThisConstructorCall(int i) this(i = + i); public stati
2、c void main(String args) new ThisConstructorCall(String call); new ThisConstructorCall(47); A、s = String call s = i = 47B、String call s = iC、s = String call i = 47D、String call s = i = 47 (3) 关于被私有保护访问控制符private修饰的成员变量,以下说法正确的是(C )A)可以被三种类所引用:该类自身、与它在同一个包中的其他类、在其他包中的该类的子类 B)可以被这些类访问和引用:该类本身、该类的所有子类C
3、)只能被该类自身所访问和修改D)只能被同一个包中的类访问(4) 定义主类的类头时可以使用的访问控制符是( B )。A、privateB、publicC、protectedD、private protected(5) 下列哪种说法是错误的( C )A)实例方法可直接调用超类的实例方法(当超类的实例方法没有被重写时)B)实例方法可直接调用超类的类方法(当超类的类方法没有被重写时)C)实例方法可直接调用其他类的实例方法D)实例方法可直接调用本类的类方法(6) 已知如下类定义: class Base public Base() public Base(int m) protected void fun
4、(int n) public class Child extends Base 如下哪句可以正确地加入子类中构成方法的重载?( A )A)public void fun( ) B)private void fun( int n ) C)protected void fun ( int n ) D)public fun ( int n ) (7) class Person public void printValue(int i, int j) /* */ /2public void printValue(int i)/* . */ /3public class Teacher extends
5、Person public void printValue() /* . */ /6public void printValue(int i) /* . */7public static void main(String args)Person t = new Teacher();t.printValue(10);第十行的声明将调用哪些方法? ( D )A)on line 2B)on line 3C)on line 6D)on line 7 (8) 在使用interface声明一个接口时,只可以使用_D_修饰符修饰该接口A)privateB)protectedC)private和protect
6、ed D)public(9) 下面是关于类及其修饰符的一些描述,错误的是( B )A)abstract类只能用来派生子类,不能用来创建abstract类的对象。B)final类不但可以用来派生子类,也可以用来创建final类的对象。C)abstract不能与final同时修饰一个类。D)abstract方法必须在abstract类中声明,但abstract类定义中可以没有abstract方法 (10) 当要将一文本文件当作一个数据库访问,读完一个纪录后,跳到另一个纪录,它们在文件的不同地方时,一般使用( B )类访问。 A. FileOutputStream B. RandomAccessFi
7、le C. PipedOutputStream D. BufferedOutputStream 1. 关键字throws与throw在用法上有什么区别Throw用法:例如自定义了一个异常类,用new throw MyException()Throws用于在函数声明时抛出异常类型,例public void fun throws IOException()2. 描述一下,如何通过继承Thread类的方式实现多线程。注:可以文字描述,也可以写一个例子说明。public class TestThread1Public static void main(String args)MyThread mt=
8、new MyThread();Thread t=new Thread();T.start();For(int a =0;a10;a+)System.out.println(Thread.currentThread.getName()+”:”+a);TryThread.sleep(100);catch(Exception e)Throw new RuntimException(e);Class MyThread1 implements RunnableSystem.out.println(Thread.currentThread.getName()+”:”+a);TryThread.sleep(
9、100);catch(Exception e)Throw new RuntimException(e);3. 向mysql的xscj数据库中的xs表中添加删除数据。请描述如何实现。注:l 可以文字描述,也可以写一个例子说明。l mysql数据库,驱动类名为com.mysql.jdbc.Driver,数据库url为jdbc:mysql:/localhost:3306/xscj,用户名密码均为rootl 可能用到的APIn static Class forName(String className) n DriverManager类u static void registerDriver(Driv
10、er driver) u static Connection getConnection(String url, String user, String password) n Connection接口u Statement createStatement() n Statement接口u int executeUpdate(String sql)Import java.sql.*Public class TestJDBC Public static void main(String args)ResultSet rs=null;Statement stmt=null;Connection c
11、onn=null;TryClass.forName(“com.mysql.jdbc.Driver”);Conn=DriverManager.getConnection(“jdbc:mysql:/localhost:3306/xscj”, “root”,”root”) stmt.executeUpdate(“insert into xs values(1002)”);/添加stmt.executeUpdate(“delete from xs where id=1002”);/删除1. 从控制台输入一行字符串,降字符串按空格分割,分割结果放在一个数组里,最后再输出数组中的元素(用循环)。可能会用到
12、的APIScanner类Scanner(InputStreamsource) 构造一个新的 Scanner,它生成的值是从指定的输入流扫描的。String nextLine() 此扫描器执行当前行,并返回跳过的输入信息String类String split(String regex) 根据给定正则表达式的匹配拆分此字符串。/源程序import java.util.Scanner;public class Exam1 public static void main(String args) / TODO Auto-generated method stubScanner sc = new Sca
13、nner(System.in);String s =sc.nextline();System.out.println(s);String str =s.spilt(“ ”);for(String s1: str)System.out.println(s1);2. 以下使用SimpleDataFormat将日期对象格式化成特定格式,将字符串中的日期转换成日期对象可能用到的APISimpleDateFormat(Stringpattern)/构造方法Stringformat(Datedate) Dateparse(Stringsource) /源程序import java.text.DateFor
14、mat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.*;public class Exam2 public static void main(String args) throws ParseException Date d = new Date();DateFormat df = new SimpleDateFormat(yy-MM-dd);System.out.println(df.format(d);/打印日期String s = 2013-12-5;System.o
15、ut.println(df.parse(s);/从字符串s中解析日期对象3. 以下是一个操作List的源程序,请填空可能用到的APIArrays类static List asList(T. a) /把a变成listCollection接口Object toArray() /将集合变成数组import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.*;import java.util.Iterator;public class Exam3 public static
16、 void main(String args) / TODO Auto-generated method stubList list ;/ 创建容器StringstrArray = F, G, H, I, J ;list=Arrays.asList(strArray);/strArray数组变成Listfor(Iterator it = list.iterator();it.hasNext();)System.out.print(it.hasNext()+ );System.out.println();Objecto =list.toArray();/ 把list变成数组System.out.
17、println(Arrays.deepToString(o);4. 使用缓冲输出流、文件输出流将十个整数写入文件。可能用到的APIRandom类intnextInt(intn) /生成一个随机数,值在0n-1之间缓冲输出流BufferedOutputStreamBufferedOutputStream(OutputStream out) /构造方法类OutputStreamvoid write(byte b) String类byte getBytes() /将字符串变成字节数组import java.io.*;import java.util.Random;public class Exam4
18、 public static void main(String args) throws Exception / TODO Auto-generated method stubint array = new int10;Random ran = new Random();String str;byte buf;int i;for( i=0;i10;i+)arrayi=i;FileOutputStream fos = new FileOutputStream(d:/abc.txt);BufferedOutputStream bos = new BufferOutputStream(fos);fo
19、r(i=0;i10;i+ )str = String.valueOf(arrayi);/整数变成字符串str = str+ ;buf =str.getBytes();/字符串变成字节数组bos.write (buf) ;/字节数组写入输出流bos.close();5. 下列程序功能是在一个窗口内添加五个按钮,采用BorderLayout布局,中间那个按钮点击以后可以关闭程序,事件监听采用匿名内部类。import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public cla
20、ss Exam5 private Frame f; private Button bn, bs, bw, be, bc; public static void main(String args) Exam5 guiWindow2 = new Exam5(); guiWindow2.go(); public void go() f = new Frame(Border Layout); f.setLayout(new La); bn = new Button(B1); bs = new Button(B2); be = new Button(B3); bw = new Button(B4); b
21、c = new Button(close); bc.addActionListener(new WindowAdapter())Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubSystem.exit(0); ); f.add(bn, BorderLayout.NORTH); f.add(bs, BorderLayout.SOUTH); f.add(be, BorderLayout.EAST); f.add(bw, BorderLayout.WEST); f.add(bc,BorderLayout.CENTER); f.pack(); F.setVisible(true);/显示窗口 设计飞的接口IFly,接口包含fly()方法。让鸟类Bird和飞机类Airplane实现这个接口。编写用户程序FlyDemo,在程序中分别把鸟和飞机的对象赋值给接口,并调用接口的方法。飞的接口IFly:类Bird实现接口IFly:类Airplane实现接口IFly:FlyDemo类:页脚内容19
限制150内