Java高级编程期末考试真题试卷及复习资料 下载本文

内容发布更新时间 : 2024/5/18 6:38:50星期一 下面是文章的全部内容请认真阅读。

7、String是最基本的数据类型吗?基本类型有哪些?

答:String不是基本数据类型,基本数据类型包括:byte, short, int, long, float, double, boolean, char八大类型。而String是类,属于引用类型,引用类型包括:类,接口,数组等。

8、说出ArrayList,Vector, LinkedList的存储性能和特性?

答:ArrayList和Vector均使用数组方式存储数据,能自动增加数组容量,允许按序号索引元素,效率高,而插入设计数组元素移动所以效率低,vector线性安全,ArrayList线性不安全。

LinkedList使用双向链表实现存储,插入效率高,索引效率低。 9、&和&&的区别。

答:&是位运算符,表示按位与运算,&&是逻辑运算符,表示逻辑与(and)。 10、sleep() 和 wait() 有什么区别?

答:sleep是Thread类的方法,没有释放对象锁,让程序暂停执行指定的时间,之后恢复运行状态,期间监控状态依然保持

wait是Object类的方法,释放了对象锁,进入等待此对象的等待锁定池,必须用notify()或notifyAll()方法唤醒该进程 11、GC是什么? 为什么要有GC?

答:GC是垃圾收集的意思,自动监测对象是否超过作用域从而达到自动回收内存的目的,Java语言没有显示地释放已分配内存的方法。

综合题

1.udp编程

1) 说明在java在UDP协议与TCP协议实现中连接和数据传输方面的不同 TCP:通过端口来区分程序间的通信进程,按字节流顺序传播。

UDP:通过端口来区分程序间的通信进程,按数据报传播,包到达的先后顺序不固定。

2) 编程举例udp如何发送数据

byte[] data = \你好UDP\.getBytes();

InetAddress inet = InetAddress.getByName(\);

DatagramPacket dp = new DatagramPacket(data, data.length, inet,6000); DatagramSocket ds = new DatagramSocket(); ds.send(dp); ds.close();

3) 编程举例udp如何接受数据

byte[] data = new byte[1024];

DatagramSocket ds = new DatagramSocket(6000);

DatagramPacket dp = new DatagramPacket(data, data.length); ds.receive(dp); ds.close();

2. 在理解线程同步概念的基础上,针对一个银行账户对象account,编程实现使用同步代码块和同步方法来完成多线程正确取钱行为,请编写两段代码,分别实现这两种实现方法。 同步代码块

public class DrawThread extends Thread { public void run() {

synchronized (account) {

if (account.getBalance() >= drawAmount) { System.out.println(\取钱成功\); } else

System.out.println(\取钱失败\); } } }

同步方法

public class Account {

public synchronized void draw(double drawAmount){ if(account.getBalance() >= drawAmount) { System.out.println(\取钱成功\); } else

System.out.println(\取钱失败\); } }

3. 请使用jdbc和SQL语法完成如下任务

1) 创建学生表student(字段:逻辑主键id,学号studentId,姓名studentName,年龄age,性别sex-0表示女生-1表示男生),并给studentName字段创建索引。

create table if not exists `student` (

`id` int auto_increment not null, `studentId` int not null, `studentName` varchar(20), `age` int, `sex` int,

primary key(`id`) );

create index idx_studentName on student(studentName);

2)使用jdbc完成对MySql数据库(ip:127.0.0.1, port:3306, DataBase:

studentdb; 用户名:student, 密码:student)中student表的访问,输出全部学生的学号和姓名。

Connection con; Statement stmt; ResultSet rs;

String dburl = \; String user = \; String password = \;

con = DriverManager.getConnection(dburl,user,password); System.out.println(\连接成功\); stmt = con.createStatement();

rs = stmt.executeQuery(\) while(rs.next()){

int id = rs.getInt(1); String name = rs.getString(2) System.out.print(\ + id); System.out.print(\ + name); }

rs.close(); stmt.close(); con.close();

4、编写代码,使用ServerSocket和Socket类完成从客户端发送一行字符串给服务器,服务器接收后再发回同样一行字符串给客户端,请完成Server.java和Client.java类的主要代码;请参考上述代码从客户端发送一个double数据、一个文件、一个java对象给服务器端,分别在服务器端输出、另存文件和读取对象。

public class Client { }

public class Server {

public static void main(String[] args) throws IOException {

ServerSocket ss = new ServerSocket(30000); while (true) {

System.out.println(\接受客户连接前 \); Socket s = ss.accept();

System.out.println(\接受客户链接后\);

DataInputStream dis = new DataInputStream(new BufferedInputStream(

s.getInputStream()));

Double d = dis.readDouble();

}

socket.close();

byte[] buff = new byte[32];

BufferedInputStream bis = new BufferedInputStream(new bis.read(buff, 0, buff.length); System.out.println(buff.length);

OutputStream os = socket.getOutputStream(); os.write(buff, 0, buff.length); os.flush();

ObjectOutputStream oos = new ObjectOutputStream(

new BufferedOutputStream(socket.getOutputStream()));

oos.writeObject(new Student(\, 35)); oos.flush();

DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(

socket.getOutputStream()));

dos.writeDouble(10.01); dos.flush();

public static void main(String[] args) throws IOException {

Socket socket = new Socket(\, 30000); // ①

FileInputStream(\));

}

}

}

System.out.println(\第一个数据:\ + d);

ObjectInputStream ois = new ObjectInputStream( }

byte[] buff = new byte[32];

InputStream is = s.getInputStream();

FileOutputStream fos = new FileOutputStream(\);

BufferedOutputStream bos = new BufferedOutputStream(fos); int bytesRead = is.read(buff, 0, buff.length); bos.write(buff, 0, buff.length); bos.close(); s.close();

new BufferedInputStream(s.getInputStream()));

try {

Student st = (Student) ois.readObject();

System.out.println(\学生姓名:\ + st.getName() + \学生年龄:\

+ st.getAge());

} catch () {

5、编写Java代码实现单例(Singleton)模式。

public class Singleton{

private static Singleton instance; private Singleton(){}

public static Singleton getInstance() { if(instance==null) {

instance = new Singleton(); }

return instance; } }