java期末期末考试卷,JAVA期末考试卷.doc

6894c33e9ea850e48a01b600503581d3.gifJAVA期末考试卷.doc

(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 static void main(String args[]) {new ThisConstructorCall(“String call“);new ThisConstructorCall(47);}}A、s = String calls = i = 47B、 String calls = iC、s = String calli = 47D、String calls = i = 47(3) 关于被私有保护访问控制符 private 修饰的成员变量,以下说法正确的是(C )A)可以被三种类所引用:该类自身、与它在同一个包中的其他类、在其他包中的该类的子类 B)可以被这些类访问和引用:该类本身、该类的所有子类C)只能被该类自身所访问和修改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(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){/* . */ }//3}public class Teacher extends 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)private B)protectedC)private 和 protected D)public(9) 下面是关于类及其修饰符的一些描述,错误的是( B )A)abstract 类只能用来派生子类,不能用来创建 abstract 类的对象。B)final 类不但可以用来派生子类,也可以用来创建 final 类的对象。C)abstract 不能与 final 同时修饰一个类。D)abstract 方法必须在 abstract 类中声明,但 abstract 类定义中可以没有abstract 方法(10) 当要将一文本文件当作一个数据库访问,读完一个纪录后,跳到另一个纪录,它们在文件的不同地方时,一般使用( B )类访问。A. FileOutputStream B. RandomAccessFile C. PipedOutputStream D. BufferedOutputStream 1. 关键字 throws 与 throw 在用法上有什么区别Throw 用法:例如自定义了一个异常类,用 new throw MyException()Throws 用于在函数声明时抛出异常类型,例 public void fun throws IOException()2. 描述一下,如何通过继承 Thread 类的方式实现多线程。注:可以文字描述,也可以写一个例子说明。public class TestThread1{Public static void main(String args[]){MyThread mt= new MyThread();Thread t=new Thread();T.start();For(int a =0;a forName(String className)  DriverManager 类 static void registerDriver(Driver driver)  static Connection getConnection(String url, String user, String password)  Connection 接口 Statement createStatement()  Statement 接口 int executeUpdate(String sql)Import java.sql.*Public class TestJDBC {Public static void main(String args[]){ResultSet rs=null;Statement stmt=null;Connection conn=null;Try{Class.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. 从控制台输入一行字符串,降字符串按空格分割,分割结果放在一个数组里,最后再输出数组中的元素(用循环) 。可能会用到的 APIScanner 类Scanner(InputStream source) 构造一个新的 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 Scanner(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(String pattern)//构造方法String format(Date date) Date parse(String source) //源程序import java.text.DateFormat;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.out.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 void main(String[] args) {// TODO Auto-generated method stubList list ;// 创建容器String[]strArray = { “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();Object[]o =list.toArray(); // 把 list 变成数组System.out.println(Arrays.deepToString(o));}}4. 使用缓冲输出流、文件输出流将十个整数写入文件。可能用到的 APIRandom 类int nextInt(int n) //生成一个随机数,值在 0~n-1 之间缓冲输出流 BufferedOutputStreamBufferedOutputStream(OutputStream out) //构造方法类 OutputStreamvoid write(byte[] b) String 类byte[] getBytes() //将字符串变成字节数组import java.io.*;import java.util.Random;public class Exam4 {public static void main(String[] args) throws Exception {// TODO Auto-generated method stubint[] array = new int[10];Random ran = new Random();String str;byte[] buf;int i;for( i=0;i10;i++)array[i]=i;FileOutputStream fos = new FileOutputStream(“d:/abc.txt“);BufferedOutputStream bos = new BufferOutputStream(fos);for(i=0;i10;i++ ){str = String.valueOf(array[i]);//整数变成字符串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 class 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“); bc = 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 类:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值