您的当前位置:首页正文

JAVA期末考试卷

2024-10-18 来源:威能网
(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 = 47 B、String call s = i

C、s = String call i = 47 D、String call s = i = 47

(3) 关于被私有保护访问控制符private修饰的成员变量,以下说法正确的是(C )

A)可以被三种类所引用:该类自身、与它在同一个包中的其他类、在其他包中的该类的子类

B)可以被这些类访问和引用:该类本身、该类的所有子类 C)只能被该类自身所访问和修改 D)只能被同一个包中的类访问

(4) 定义主类的类头时可以使用的访问控制符是( B )。 A、private B、public C、protected

D、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) {/* … */ }//2 public void printValue(int i){/* .。. */ }//3 }

public class Teacher extends Person {

public void printValue() {/* 。.. */ }//6

public void printValue(int i) {/* 。.。 */}//7 public static void main(String args[]){ Person t = new Teacher(); t.printValue(10); }

第十行的声明将调用哪些方法? ( D ) A)on line 2 B)on line 3 C)on line 6 D)on line 7

(8) 在使用interface声明一个接口时,只可以使用__D__修饰符修饰该接口

A)private B)protected C)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〈10;a++){

System。out。println(Thread.currentThread。getName()+”:”+a); Try{

Thread.sleep(100); }catch(Exception e){

Throw new RuntimException(e); } } } }

Class MyThread1 implements Runnable{

System.out。println(Thread.currentThread.getName()+”:”+a); Try{

Thread.sleep(100); }catch(Exception e){

Throw new RuntimException(e); }

}

3。 向mysql的xscj数据库中的xs表中添加删除数据.请描述如何实现。 注:

 可以文字描述,也可以写一个例子说明.

 mysql数据库,驱动类名为com.mysql。jdbc.Driver,数据库url为jdbc:

mysql://localhost:3306/xscj,用户名密码均为root  可能用到的API

 static Class<?> 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。 从控制台输入一行字符串,降字符串按空格分割,分割结果放在一个数组里,

最后再输出数组中的元素(用循环)。 可能会用到的API Scanner类

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 stub Scanner 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将日期对象格式化成特定格式,将字符串中的日期转换成日期对象 可能用到的API

SimpleDateFormat(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的源程序,请填空 可能用到的API Arrays类

static List〈T〉 asList(T.。。 a) //把a变成list Collection接口

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 stub List list ;// 创建容器 String[]strArray = { \"F\", ”G”, \"H\ list=Arrays.asList(strArray);//strArray数组变成List for(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. 使用缓冲输出流、文件输出流将十个整数写入文件. 可能用到的API Random类

int nextInt(int n) //生成一个随机数,值在0~n—1之间 缓冲输出流BufferedOutputStream

BufferedOutputStream(OutputStream out) //构造方法 类OutputStream void 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 stub int[] array = new int[10]; Random ran = new Random(); String str; byte[] buf; int i; for( i=0;i〈10;i++) array[i]=i; FileOutputStream fos = new FileOutputStream(\"d:/abc。txt”); BufferedOutputStream bos = new BufferOutputStream(fos); for(i=0;i<10;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()){ @Override

public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub System.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类:

因篇幅问题不能全部显示,请点此查看更多更全内容