JAVA期末考试卷.doc_第1页
JAVA期末考试卷.doc_第2页
JAVA期末考试卷.doc_第3页
JAVA期末考试卷.doc_第4页
JAVA期末考试卷.doc_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

(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 call s = i = 47B、String call s = iC、s = String call i = 47D、String call s = 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)/* . */ /3public 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)privateB)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 TestThread1Public static void main(String args)MyThread mt= 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(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(Driver 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 conn=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. 从控制台输入一行字符串,降字符串按空格分割,分割结果放在一个数组里,最后再输出数组中的元素(用循环)。可能会用到的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 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(Stringpattern)/构造方法Stringformat(Datedate) Dateparse(Stringsource) /源程序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 ;/ 创建容器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.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 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);for(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 class Exam5 private Frame f; private Button bn, bs, bw, be, bc; public static void main(String args) Exam5 guiWindow2 = new Exam5(); guiWindow2.go(); pub

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论