面试题4

1. 下面关于Socket套接字对象说法错误的是()

A. DatagramSocket是用于UDP协议发送和接收的Socket对象

B. DatagramPacket是UDP协议发送的数据包

C. ServerSocket可以当TCP协议和UDP协议的服务端

D. Socket对象只能当做TCP的客户端对象

2. 下面关于反射说法正确的是()

A. 反射不能获取私有的成员

B. 想要进行反射必须要提前知道当前类的所有组成部分

C. 反射技术目前还不成熟,不建议使用

D. 反射机制是框架的重要组成部分

3. LinkedList类的特点是()

A. 查询快

B. 增删快

C. 元素不重复

D. 元素自然排序

4. 下面程序的运行结果是()

String s1 = “abc”;     

String s2 = new String(“abc”);    

System.out.println(s1==s2);     

String s3 = “你好吗”;    

String s4 =”你”;    

String s5 =”好吗”;     

System.out.println(s3==(s4+s5))     

//s3==new  StringBuffer(s4).append(s5).toString();

A. true true

B. false true

C. true flase

D. false false

5. 下列哪个选项正确?()

A.  Error类是一个RuntimeException异常

B. 任何抛出一个RuntimeException异常的语句必须包含在try块内

C. 任何抛出一个Error对象的语句必须包含在try块内

D. 任何抛出一个Exception异常的语句必须包含在try块内

6. Java中,下面对于构造函数的描述正确的是()

A. 类必须显式定义构造函数

B. 构造函数的返回类型是void

C. 构造函数和类有相同的名称,并且不能带任何参数

D. 一个类可以定义多个构造函数

7. 下述哪些说法是不正确的?

A. 实例变量是类的成员变量

B. 构造方法可以有参数,所以也可以有返回值

C. 方法变量在使用之前必须初始化

D. 系统提供默认的构造方法

8. 下列说法中,错误的一项是(   

A. 线程一旦创建,则立即自动执行

B. 线程创建后需要调用start()方法,将线程置于可运行状态

C. 调用线程的start()方法后,线程也不一定立即执行

D. 线程处于可运行状态,意味着它可以被调度

9. 下列说法中,错误的一项是(   

A. Thread类中没有定义run()方法

B. 可以通过继承Thread类来创建线程

C. Runnable接口中定义了run()方法

D. 可以通过实现Runnable接口创建线程

10. 下面程序的运行结果是什么(   

public static void main(String[] args){   

String s1 = “abc”;   

String s2 = “xyz”;  

show(s1,s2);  

System.out.println(s1+”-----”+s2);

 }  

static void show(String s1,String s2){   

s1 = s2+s1+”Q”;      

s2 = “W”+s1;    

 }

A. abc-----xyz

B. xyzabcQ-----xyzWabc

C. xyzabcQ---- xyzabcQWabc

D. xyzQ----Wabc

二、简答题(32分)

1.  Date和字符串相互转换的方法

2.  简述HashMap的实现原理

3.  简述实现多线程的两种方式

4.创建一个HashMap<String,String>,遍历里边元素,打印keyvalue

三、编程题(48分)

1. 创建一个Person(包含姓名,年龄,和分数)类型的集合,实现对集合中元素按照分数降序的排序,如果分数相同,按照年龄的升序进行排序。(15分)

2. 使用反射向一个HashMap<String,String>的集合中存入以下数据

key=1,value=“哈哈”

key=12.1F,value=12L

并将结果打印输出(15分)

3. 使用TCP协议实现客户端向服务端传送一张图片,要求服务端使用多线程实现。(18分)

C D B D D D B A A A



简答题
1.
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatDemo {
public static void main(String[] args) throws Exception {
zhengxiang();
nixiang();
}
private static void zhengxiang() throws Exception {
String txt = "18-4-12 下午5:25";
SimpleDateFormat sdf = new SimpleDateFormat();
Date date = sdf.parse(txt);
System.out.println(date.getTime());
txt = "2018/04/12 17:25:51";
sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//自定义日期模板
date = sdf.parse(txt);
System.out.println(date.getTime());
}
private static void nixiang() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat();
String txt = sdf.format(date);
System.out.println(txt);
sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
txt = sdf.format(date);
System.out.println(txt);
}
}




2.
HashMap里实现了一个静态的内部类Entry,重要的属性有key,value,next,hash。
map中的数据就存储在Entry[]中,每次 put数据时,通过该key的hashcode,
得到该key在Entry[]中的索引,然后以e.next遍历,如果存在对应value,返回
value,不存在就将其添加到Entry[]。
put(key,value)时,如果Entry[]的size超过threshold,则进行扩容,即table.length*2。
get(key) 时先定位到该数组元素,再遍历该元素处的链表。






3.
方式一:定义一个子类继承Thread,然后重写run方法,将需要给子线程执行的代码放在run方法里面;
然后创建子类对象,通过子类对象调用start方法
方式二:定义一个子类实现Runnable接口,实现run方法,
将需要给子线程执行的代码放在run方法里面;然后在创建线程时,将子类对象当做参数传递,
然后调用线程的start方法


4.
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class MapDemo2 {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put( "张三","昌平");
map.put( "李四","朝阳");
map.put( "王五","海淀");
map.put( "赵六","顺义");
Set<String> keySet = map.keySet();
for (String key : keySet) {
String value = map.get(key);
System.out.println(key + "=" + value);
}
System.out.println("————分割线—————");
Collection<String> values = map.values();
for (String value : values) {
System.out.println(value);
}
}
}




编程题


1.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Student {
private String name;
private int age;
private float score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student(String name, int age, float score) {
super();
this.name = name;
this.age = age;
this.score = score;
}
public Student() {
super();
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", score=" + score + "]";
}
public static List<Student> getListStudents() {  
    List<Student> listStudents = new ArrayList<Student>();  
    Student stu2 = new Student();  
    stu2.setName("stu2");  
    stu2.setScore(11);  
    Student stu1 = new Student();  
    stu1.setName("stu1");  
    stu1.setScore(12);  
    Student stu3 = new Student();  
    stu3.setName("stu3");  
    stu3.setScore(13);  
    listStudents.add(stu2);  
    listStudents.add(stu1);  
    listStudents.add(stu3);  
    return listStudents;  
}  
public static void printStuListName(List<Student> list) {  
    for (int i = 0; i < list.size(); i++) {  
        System.out.println(list.get(i).getName());  
    }  
}
public static void main(String[] args) {  
    List<Student> listStudents = getListStudents();  
    printStuListName(listStudents); 
    System.out.println("----------------------------------------");  
    Collections.sort(listStudents, new Comparator<Student>() {  
        public int compare(Student stu1, Student stu2) {  
        return stu1.getScore() > stu2.getScore() ? 1 : -1;  
        }  
    });  
    printStuListName(listStudents);  
}  
}




2.
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) throws Exception {
Map<String, String> map = new HashMap<String, String>();
Class clazz = map.getClass();
Method put = clazz.getMethod("put", new Class[] { Object.class, Object.class });
put.invoke(map, 1, "哈哈");
put.invoke(map, 12.1F, 12L);


System.out.println(map);
}
}
3.
客户端:


import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class Test1 {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 8888);
OutputStream os = socket.getOutputStream();
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(new File("A/a.jpg"));
bis = new BufferedInputStream(fis);
byte[] buffer = new byte[1024];
int length = -1;
while ((length = bis.read(buffer)) != -1) {
os.write(buffer, 0, length);
os.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
bis.close();
fis.close();
os.close();
socket.close();
}
}
}


服务器端:


import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;


class ServeOneClient extends Thread {
private Socket socket;
private BufferedReader in;
private PrintWriter out;


public ServeOneClient(Socket s) throws IOException {
socket = s;
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
start();
}
public void run() {
try {
while (true) {
String str = in.readLine();
if (str.equals("END"))
break;
System.out.println("Echoing:" + str);
out.println(str);
yield();
}
System.out.println("closing......");
} catch (IOException e) {
} finally {
try {
socket.close();
} catch (IOException e) {
}
}
}
}
public class ThreadServerTest {
static final int PORT = 9998;
public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(PORT);
System.out.println("Server Started");
InputStream is = null;
FileOutputStream fos = null;
try {
while (true) {
Socket socket = s.accept();
try {
new ServeOneClient(socket);
socket.getInputStream();
is = socket.getInputStream();
fos = new FileOutputStream(new File("D:\\老开.jpg"));
byte[] buffer = new byte[1024];
int length = -1;
while ((length = is.read(buffer)) != -1) {
fos.write(buffer, 0, length);
fos.flush();
}
} catch (IOException e) {
socket.close();
}
}
} finally {
s.close();
fos.close();
is.close();
fos.close();
is.close();
}


}
}

猜你喜欢

转载自blog.csdn.net/a331685690/article/details/80100940