提交 9c9de578 编写于 作者: CSDN-Ada助手's avatar CSDN-Ada助手

add net questions

上级 22391b5f
......@@ -15,7 +15,7 @@ NIO与传统IO变化不大
### A
```
NIO全称是no-blocking io
NIO全称是no-blocking io(或者New IO)
```
### B
......
{
"type": "code_options",
"author": "clong",
"source": "Channel.md",
"exercise_id": "",
"notebook_enable": true
}
\ No newline at end of file
# Channel
以下关于 `Channel` 的说法错误的是:
## 答案
```
DatagramChannel用于TCP数据读写
```
## 选项
### A
```
通道可以异步读写
```
### B
```
通道总是基于Buffer来读写数据
```
### C
```
通道可读可写
```
{
"type": "code_options",
"author": "clong",
"source": "ChannelType.md",
"exercise_id": "",
"notebook_enable": true
}
\ No newline at end of file
# Channel Type
以下属于 `Channel` 类型的是:
## 答案
```
以上选项都是
```
## 选项
### A
```
SctpChannel,SctpMultiChannel,SctpServerChannel
```
### B
```
DatagramChannel,FileChannel
```
### C
```
ServerSocketChannel,ServerChannel
```
......@@ -2,6 +2,6 @@
"node_id": "java-1cbb9d1ea7c74caeaf8d88c2aad75965",
"keywords": [],
"children": [],
"export": [],
"export": ["Channel.json","ChannelType.json"],
"title": "Channel(通道)"
}
\ No newline at end of file
{
"node_id": "java-f824cb48cef64bbcad371bb304b26d10",
"keywords": [],
"children": [],
"export": [],
"title": "Java新IO概述"
}
\ No newline at end of file
{
"type": "code_options",
"author": "clong",
"source": "Charset.md",
"exercise_id": "",
"notebook_enable": true
}
\ No newline at end of file
# NIO Charset
以下关于 `NIO Charset` 说法正确的是:
## 答案
```
向ByteBuffer存放数据需要考虑字符集的编码方式
```
## 选项
### A
```
向ByteBuffer存放数据不需要考虑字符集的编码方式
```
### B
```
向CharBuffer中读取时需要考虑字符集的解码
```
### C
```
Charset类在java.io.charset包中
```
......@@ -2,6 +2,6 @@
"node_id": "java-984b508fc9a54650aa9509eeb198f925",
"keywords": [],
"children": [],
"export": [],
"export": ["Charset.json"],
"title": "字符集和Charset"
}
\ No newline at end of file
{
"node_id": "java-4bae5939bee44a31ba92906e490293b0",
"keywords": [],
"children": [],
"export": [],
"title": "使用Buffer"
}
\ No newline at end of file
{
"type": "code_options",
"author": "clong",
"source": "FileLock.md",
"exercise_id": "",
"notebook_enable": true
}
\ No newline at end of file
# File Lock
使用多线程读写同一个文件,选出你认为最合理的一种读取方法:
## 答案
```java
public void readFile(String path) {
RandomAccessFile file = null;
FileLock lock = null;
try {
file = new RandomAccessFile(path, "rw");
FileChannel fileChannel = file.getChannel();
lock = fileChannel.lock();
// read and write
} catch (IOException e) {
System.out.println(e);
} finally {
if (lock != null) {
try {
lock.release();
} catch (IOException e) {
e.printStackTrace();
}
}
if (file != null) {
try {
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```
## 选项
### A
```java
public void readFile(String path) throws IOException {
RandomAccessFile file = new RandomAccessFile(path, "rw");
FileChannel fileChannel = file.getChannel();
// read and write
file.close();
}
```
### B
```java
public void readFile(String path) throws IOException {
RandomAccessFile file = new RandomAccessFile(path, "rw");
FileChannel fileChannel = file.getChannel();
FileLock lock = fileChannel.lock();
// read and write
lock.release();
file.close();
}
```
### C
```java
public void readFile(String path) {
RandomAccessFile file = null;
FileLock lock = null;
try {
file = new RandomAccessFile(path, "rw");
FileChannel fileChannel = file.getChannel();
lock = fileChannel.lock();
// read and write
} catch (IOException e) {
System.out.println(e);
} finally {
try {
lock.release();
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
......@@ -2,6 +2,6 @@
"node_id": "java-338c2cfa55b346a0a0cd4dfc61c40ae6",
"keywords": [],
"children": [],
"export": [],
"export": ["FileLock.json"],
"title": "文件锁"
}
\ No newline at end of file
{
"type": "code_options",
"author": "clong",
"source": "Path.md",
"exercise_id": "",
"notebook_enable": true
}
\ No newline at end of file
# NIO Path
以下关于 `Path` 说法错误的是:
## 答案
```
Path是在Java 8中新增的
```
## 选项
### A
```
Path表示的路径与平台无关
```
### B
```
Path可以通过Paths.get()方法创建
```
### C
```
File可以替换Path
```
\ No newline at end of file
{
"node_id": "java-386113b4f6274eb591e1f01b39f0c8ce",
"keywords": [],
"keywords": ["Paths", "Path"],
"children": [],
"export": [],
"export": ["Path.json"],
"title": "Path接口"
}
\ No newline at end of file
{
"node_id": "java-9e2f3a44543c457e84d3d8a2e8321b65",
"keywords": [],
"children": [],
"export": [],
"title": "使用Channel"
}
\ No newline at end of file
{
"type": "code_options",
"author": "clong",
"source": "Files.md",
"exercise_id": "",
"notebook_enable": true
}
\ No newline at end of file
# Files
下列对于 `Files` 的相关操作方法,错误的是:
## 答案
```java
// 复制文件,如果目的文件存在则会覆盖
public void copyFile(String sourcePath, String destPath) throws IOException {
Path sp = Paths.get(sourcePath);
Path dp = Paths.get(destPath);
Files.copy(sp, dp);
}
```
## 选项
### A
```java
// 判断文件是否存在
public Boolean isExists(String filePath) {
Path path = Paths.get(filePath);
return Files.exists(path, new LinkOption[]{ LinkOption.NOFOLLOW_LINKS});
}
```
### B
```java
// 删除文件
public void deleteFile(String filePath) throws IOException {
Path path = Paths.get(filePath);
Files.delete(path);
}
```
### C
```java
// 创建文件
public void createFile(String filePath) throws IOException {
Path path = Paths.get(filePath);
Files.createFile(path);
}
```
......@@ -2,6 +2,6 @@
"node_id": "java-868e1323bfdf4c239a6552a45a30d48d",
"keywords": [],
"children": [],
"export": [],
"export": ["Files.json"],
"title": "Files工具类"
}
\ No newline at end of file
{
"type": "code_options",
"author": "clong",
"source": "Internet.md",
"exercise_id": "",
"notebook_enable": true
}
\ No newline at end of file
# 网络编程基础
下列对于 `网络编程` 的相关说法正确的是:
## 答案
```
以上说法均正确
```
## 选项
### A
```
TCP/IP协议是Internet最基础最广泛的协议
```
### B
```
IP地址是网络中计算机的唯一标识
```
### C
```
UDP是无连接的,TCP是有连接的
```
{
"node_id": "java-59e7173695814c90858bc221e8688329",
"keywords": [],
"keywords": ["网络", "通信", "协议"],
"children": [
{
"网络通信协议": {
......@@ -34,6 +34,6 @@
}
}
],
"export": [],
"export": ["Internet.json"],
"title": "网络编程基础"
}
\ No newline at end of file
{
"type": "code_options",
"author": "clong",
"source": "UDP.md",
"exercise_id": "",
"notebook_enable": true
}
\ No newline at end of file
# UDP
观察以下关于 `UDP` 的程序,接收端的输出为:
```java
// 省略import
public class UDPSender {
public static void main(String[] args) throws Exception {
String[] messages = {"你好!", "我叫小白", "很高兴认识你"};
DatagramSocket ds = new DatagramSocket();
for (String message: messages) {
byte[] data = message.getBytes(StandardCharsets.UTF_8);
DatagramPacket dp = new DatagramPacket(data, data.length,
InetAddress.getLocalHost(),8080);
ds.send(dp);
Thread.sleep(1000);
}
ds.close();
}
}
```
```java
// 省略import
public class UDPReceive {
public static void main(String[] args)throws Exception{
DatagramSocket ds = new DatagramSocket(8080);
byte[] data = new byte[1024];
while (!ds.isClosed()) {
DatagramPacket dp = new DatagramPacket(data, data.length);
ds.receive(dp);
int len = dp.getLength();
System.out.println(new String(data, 0, len));
}
ds.close();
}
}
```
## 答案
```
你好!
我叫小白
很高兴认识你
```
## 选项
### A
```
¦ᄑᅠ¥ᆬᄑ￯ᄐチ
₩ネム¥マᆱ¥ᄚマ￧ルᄑ
¥ᄒネ←ᆱリ¥ナᄡ│ᆴᄂ│ᆵニ¦ᄑᅠ
```
### B
```
很高兴认识你
```
### C
```
啥都不输出
```
......@@ -41,6 +41,6 @@
}
}
],
"export": [],
"export": ["UDP.json"],
"title": "UDP通信"
}
\ No newline at end of file
{
"type": "code_options",
"author": "clong",
"source": "TCP.md",
"exercise_id": "",
"notebook_enable": true
}
\ No newline at end of file
# UDP
观察以下关于 `TCP` 的程序,服务端和客户端的输出为:
```java
// 省略import
public class TCPClient {
public static void main(String[] args) throws Exception{
String[] messages = {"你好!", "我叫小白", "很高兴认识你"};
Socket socket = new Socket(InetAddress.getLocalHost(),8080);
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();
for (String message: messages) {
out.write(message.getBytes());
byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf,0, len));
}
socket.close();
}
}
```
```java
// 省略import
public class TCPServer {
public static void main(String[] args) throws Exception {
String response = "收到";
ServerSocket serverSocket = new ServerSocket(8080);
Socket socket = serverSocket.accept();
while (true) {
InputStream in = socket.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
if (len == -1) {
continue;
}
System.out.println(new String(buf,0, len));
OutputStream out = socket.getOutputStream();
out.write(response.getBytes());
Thread.sleep(1000);
}
}
}
```
## 答案
```
服务端的输出为:
你好!
我叫小白
很高兴认识你
```
## 选项
### A
```
客户端的输出为:
```
### B
```
服务端的输出为:
¦ᄑᅠ¥ᆬᄑ￯ᄐチ
₩ネム¥マᆱ¥ᄚマ￧ルᄑ
¥ᄒネ←ᆱリ¥ナᄡ│ᆴᄂ│ᆵニ¦ᄑᅠ
```
### C
```
服务端的输出为:
很高兴认识你
我叫小白
你好!
```
......@@ -48,6 +48,6 @@
}
}
],
"export": [],
"export": ["TCP.json"],
"title": "TCP通信"
}
\ No newline at end of file
{
"type": "code_options",
"author": "clong",
"source": "Support.md",
"exercise_id": "",
"notebook_enable": true
}
\ No newline at end of file
# Java基本网络支持
下列对于Java网络支持的相关说法错误的是:
## 答案
```
URI是统一资源标识符,可以用来定位资源
```
## 选项
### A
```
InetAddress类有两个子类:Inet4Address和Inet6Address
```
### B
```
URLDecoder和URLEncoder用于完成普通字符串和MIME字符串之间的相互转换
```
### C
```
URL是统一资源定位器
```
......@@ -19,6 +19,6 @@
}
}
],
"export": [],
"export": ["Support.json"],
"title": "Java的基本网络支持"
}
\ No newline at end of file
{
"type": "code_options",
"author": "clong",
"source": "HTTPClient.md",
"exercise_id": "",
"notebook_enable": true
}
\ No newline at end of file
# HTTPClient
下列语句皆为使用HTTPClient获取网页的流程之一,其中不正确的是:
## 答案
```java
HttpResponse.BodyHandler<String> bodyHandler = HttpResponse.BodyHandlers;
```
## 选项
### A
```java
String url = "https://www.baidu.com";
HttpClient client = HttpClient.newBuilder().build();
```
### B
```java
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Content-Type", "text/html")
.GET()
.build();
```
### C
```java
HttpResponse<String> response = client.send(request, bodyHandler);
if (response.statusCode() == 200) {
System.out.println(response.body());
}
```
{
"type": "code_options",
"author": "clong",
"source": "WebSocket.md",
"exercise_id": "",
"notebook_enable": true
}
\ No newline at end of file
# WebSocket
`WebSocket`可以实现服务端对客户端的推送,避免客户端轮询,造成带宽资源浪费。以下关于`WebSocket`的说法错误的是:
## 答案
```
WebSockt与HTTP都是基于TCP的,因此url也是以HTTP开头
```
## 选项
### A
```
WebSocket基于TCP协议
```
### B
```
WebSocket是双向的,即全双工协议
```
### C
```
WebSocket是一种有状态的协议
```
{
"node_id": "java-dd60957a0a7f4155a68aea7c6807d504",
"keywords": [],
"keywords": ["websocket", "get", "post", "delete", "put", "请求"],
"children": [
{
"发送同步GET请求": {
......@@ -41,6 +41,6 @@
}
}
],
"export": [],
"export": ["HTTPClient.json", "WebSocket.json"],
"title": "Java 11标准化的HTTP Client"
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册