CS 33600 Exam 1 Review The exam is on Wednesday, March 5. These problems review the material we covered about Java I/O, byte streams, buffers, data formats, character encodings, and networking. http://cs.pnw.edu/~rlkraft/cs33600/for-class/Standard_streams_&_redirection_&_pipes_&_filters.zip http://cs.pnw.edu/~rlkraft/cs33600/for-class/test-print.zip http://cs.pnw.edu/~rlkraft/cs33600/for-class/data.zip http://cs.pnw.edu/~rlkraft/cs33600/for-class/eof.zip http://cs.pnw.edu/~rlkraft/cs33600/for-class/character-sets.zip http://cs.pnw.edu/~rlkraft/cs33600/for-class/networking.zip Problem 1.) (a) A student wants to read integers from a buffered stream and writes the following code. What is wrong? BufferedInputStream bis = new BufferedInputStream( new DataInputStream( new FileInputStream( new File("input.txt")))); (b) A student wants to read integers from a buffered stream and writes the following code. What is wrong? InputStream is = new DataInputStream( new BufferedInputStream( new FileInputStream( new File("input.txt")))); Problem 2.) What do we mean when we say that a BufferedInputStream "is a" InputStream and that a BufferedInputStream "has a" InputStream? (Your answer should make use of proper Object-Oriented terminology.) How is this "is a" relationship used by Java? How is this "has a" relationship used by the BufferedInputStream? Problem 3.) Explain why the following program will throw an exception (and crash) if the program's input is exactly abc with the end-of-file condition coming right after the c. (You can copy and paste this program directly into the Java Visualizer. Use the "stdin" button for the input.) import java.io.DataInputStream; public class Problem3 { public static void main(String[] args) throws Exception { final DataInputStream in = new DataInputStream(System.in); final int n = in.readInt(); System.out.println(n); } } Problem 4.) Consider the following program (you can copy and paste it directly into the Java Visualizer). (a) If conditionalCompilation is set to true and the program's input is 1234, then explain in detail the program's output. (b) If conditionalCompilation is set to false and the program's input is 1234, then explain in detail the program's output. (c) If conditionalCompilation is set to true and the program's input is 000, then explain in detail the program's output. import java.util.Scanner; import java.io.DataInputStream; import java.io.IOException; public class Problem4 { public static void main(String[] args) throws IOException { // Set conditionalCompilation to true to use the DataInputStream, or // set conditionalCompilation to false to use the Scanner. final boolean conditionalCompilation = true; final int n; if (conditionalCompilation) { final DataInputStream in = new DataInputStream(System.in); n = in.readInt(); } else { final Scanner in = new Scanner(System.in); n = in.nextInt(); } System.out.println(n); } } Problem 5a.) Explain in detail the output from this program. (You can copy and paste it directly into the Java Visualizer.) import java.util.Arrays; import java.io.UnsupportedEncodingException; public class Problem5a { public static void main(String[] args) throws UnsupportedEncodingException { String s = "abcdefghij"; System.out.println(Arrays.toString( s.getBytes("US-ASCII") )); System.out.println(Arrays.toString( s.getBytes("UTF-16LE") )); System.out.println(Arrays.toString( s.getBytes("UTF-16BE") )); System.out.println(Arrays.toString( s.getBytes("UTF-16") )); System.out.println(Arrays.toString( s.getBytes("UTF-32") )); System.out.println(Arrays.toString( s.getBytes("UTF-32LE") )); } } Problem 5b.) Explain in detail the output from this program. (You can copy and paste it directly into the Java Visualizer.) Here is a reference for the ISO-8859-1 code page. https://en.wikipedia.org/wiki/ISO/IEC_8859-1#Code_page_layout import java.util.Arrays; import java.io.UnsupportedEncodingException; public class Problem_5b { public static void main(String[] args) throws UnsupportedEncodingException { String s = "abcd±fghiĆ"; // ± and Ć are in ISO-8859-1 but not ASCII System.out.println(s); System.out.println(Arrays.toString( s.getBytes("US-ASCII") )); System.out.println(Arrays.toString( s.getBytes("ISO-8859-1") )); System.out.println(Arrays.toString( s.getBytes("UTF-8") )); System.out.println(Arrays.toString( s.getBytes("UTF-16LE") )); System.out.println(Arrays.toString( s.getBytes("UTF-16BE") )); System.out.println(Arrays.toString( s.getBytes("UTF-16") )); System.out.println(Arrays.toString( s.getBytes("UTF-32") )); System.out.println(Arrays.toString( s.getBytes("UTF-32LE") )); } } Problem 6.) Explain in detail what each of lines 9 through 14 from the following program do. Explain why the program prints out the number that it does. (You can copy and paste this program directly into the Java Visualizer.) /* 1*/ import java.nio.ByteBuffer; /* 2*/ import java.nio.ByteOrder; /* 3*/ import java.util.Arrays; /* 4*/ public class Problem6 /* 5*/ { /* 6*/ public static void main(String[] args) /* 7*/ { /* 8*/ final int n = 1; /* 9*/ final byte[] b = ByteBuffer.allocate(Integer.BYTES) /*10*/ .order(ByteOrder.LITTLE_ENDIAN) /*11*/ .putInt(n) /*12*/ .array(); /*13*/ final int n2 = ByteBuffer.wrap( b ) /*14*/ .getInt(); /*15*/ System.out.println( n2 ); /*16*/ } /*17*/ } Problem 7.) What are code-pages and why do we need them? If you open a file (with an editor, or a command-line, or a browser, etc.) while using the wrong code-page, what happens? Problem 8.) This problem is about Unicode's UTF-8 encoding. Given each of the following byte sequences, determine if the sequence is, 1) legal and complete, 2) legal but incomplete, 3) illegal. Briefly explain your answers. Use either of these two pictures of the UTF-8 encoding to help with your explanation. https://higheredbcs.wiley.com/legacy/college/horstmann/1119402972/app/appendices.pdf#page=7 or https://en.wikipedia.org/wiki/UTF-8#Description (a) 01000000 10000000 (b) 11000000 01000000 (c) 11000000 10000000 (d) 11110000 10000000 10000000 (e) 11100000 10000000 10000000 (f) 11100000 10000000 01000000 (g) 10000000 01000000 01000000 (h) 10000000 10000000 10000000 Problem 9.) This problem is about Unicode's UTF-8 encoding. How many Unicode characters does the following sequence of 16 bytes encode? Explain your answer. (The x's represent bits that do not change the answer.) 01010101 0000xxxx 0111xxxx 1110xxxx 1010111x 100011xx 1101xxxx 1000000x 11110xxx 1011xxxx 10111xxx 101111xx 0xxxxxxx 110xxxxx 101100xx 011xxxxx Problem 10.) The UTF-16 BOM (byte-order-mark) is the following two bytes. 0xFE 0xFF In this Stack Overflow answer, https://stackoverflow.com/questions/2223882/whats-the-difference-between-utf-8-and-utf-8-with-bom/2223976#2223976 it is mentioned that the "UTF-8 BOM" is the following three bytes. 0xEF 0xBB 0xBF Is this really a three byte UTF-8 character? If so, what actual Unicode character is it? Why is this called the "UTF-8 BOM"? Why does UTF-8 NOT need a BOM, but UTF-16 does? Problem 11.) Write a single Windows cmd.exe command line that successively applies filter programs Filter1.exe and Filter2.exe to the data in file File1.dat and writes the results to the file File2.dat and redirects the error outputs from Filter1 and Filter2 (respectively) to the files Errors1.log and Errors2.log (respectively). Problem 12.) This problem is about the "is a" and "has a" relationships used in Java. See https://link.springer.com/content/pdf/10.1007/978-1-4842-3348-1_7#page=14 Complete the definitions of the two classes, B_is_a_A B_has_a_A so that the output from running the main() method in IsA_HasA_Tester looks like the following (there should always be the same number shown in the all four lines). method 1 in A with secret 6375 method 2 in A with secret 6375, and help from B method 1 in A with secret 6375 method 2 in A with secret 6375, and help from B class A // Do not modify this class. { static private final int secret = (int)(10_000*Math.random()); public String method_1(){return "method 1 in A with secret " + secret;} public String method_2(){return "method 2 in A with secret " + secret;} } class B_is_a_A extends A { } class B_has_a_A { private final A aRef; public B_has_a_A(A aRef){this.aRef = aRef;} } public class IsA_HasA_Tester // Do not modify this class. { public static void main(String[] args) { final B_is_a_A b1 = new B_is_a_A(); System.out.println( b1.method_1() ); System.out.println( b1.method_2() ); final B_has_a_A b2 = new B_has_a_A(new A()); System.out.println( b2.method_1() ); System.out.println( b2.method_2() ); } } Problem 13.) What does an IP address identify? What does a port number identify? Problem 14.) What makes a computer into a "server"? Problem 15.) Identify all the parts of the following URL. http://www.bigcompany.com:42/consumer/products/lookup.php?sku=0&color=greenish#item=3 Problem 16.) In the picture below, draw arrows to indicate in which direction data flows through the streams. Socket Socket +-------------+ +-------------+ | | tcp connection | | | IP address |O--------------------O| IP address | | Port number | | Port number | | | | | +-------------+ +-------------+ | | | | | | | | | | | | out in out in stream stream stream stream Problem 17.) The following picture is a picture of a TCP connection, but not yet a picture of a communicating TCP connection. Explain why. Also explain why this connection may stay in this state for a long time. Then draw a new picture so that the new picture describes the communicating TCP connection. ServerSocket Socket +--------------+ +----------------+ | | tcp connection | | | 16.52.9.4:75 |O--------------------O| 10.12.3.2:1234 | | | | | | | | | +--------------+ +----------------+ Problem 18.) Fill in as much detail as you can in this outline of a TCP client/server connection. Server +----------------+ | | | | | | +----------------+ | | | +----------------+ | | | | | | +----------------+ | | Client | +-------------+ +----------------+ | | | | | | | | | | | | +-------------+ +----------------+ | | | | | | +-------------+ +----------------+ | | | | | |--------------------| | | | | | +-------------+ +----------------+ | | | | | | +-------------+ +----------------+ | | | | | |--------------------| | | | | | +-------------+ +----------------+ | | | | | | +-------------+ +----------------+ | | | | | |--------------------| | | | | | +-------------+ +----------------+ | | | | | | +-------------+ +----------------+ | | | | | | | | | | | | +-------------+ +----------------+ Problem 19.) Write five lines of Java that creates a server socket, waits for a connection on that socket, reads one line from the connecting process, writes that line back to the process, and then closes the connection (but not the server socket). Problem 20.) Here are two screen shots of TCPView. How many servers does each show? How many clients does each show? In each screen shot, which clients are connected to which servers? (The two screen shots are independent of each other. No information crosses over from one to the other.) http://cs.pnw.edu/~rlkraft/cs33600/for-class/Problem_20a_tcpview.png http://cs.pnw.edu/~rlkraft/cs33600/for-class/Problem_20b_tcpview.png Problem 21.) Suppose we have three processes, P1, P2, and P3 that communicate using TCP. The processes are supposed to form a ring in which P1 can send messages to P2, P2 can send messages to P3, and P3 can send messages to P1, and the processes do not communicate in any other way. More specifically, suppose that when process P1 starts, it prompts a user at a console to type in a message, and then P1 sends that message to P2. P2 receives the message and prints it on its console and then prompts a user at the console to type a new message that P2 sends to P3. P3 receives its message, prints it to its console, prompts a user for a new message, and sends that to P1. P1 receives the message from P3, prints it to its console window, and prompts its user for a message to send to P2, etc. Write a Java pseudo-code outline of the steps that P1 must go through to implement this "ring protocol". How many sockets, and what kind of sockets, will P1 need? What information would P1 need at its startup time? Would the pseudo-code for processes P2 and P3 be the same or different than P1's? Problem 22.) How can one end of a TCP connection know that it is time to stop receiving data from its socket and to begin transmitting data using its socket? (Hint; There are essentially three ways.) Problem 23.) Unicode has a 21-bit address space for all of its characters. UTF-8 is an encoding of Unicode's 21-bit address space. Every valid UTF-8 character encoding represents a 21-bit address in Unicode's address space. For each valid UTF-8 character encoding below, what is the decoded 21-bit Unicode address? Use this picture of the UTF-8 encoding to help with the decoding. https://higheredbcs.wiley.com/legacy/college/horstmann/1119402972/app/appendices.pdf#page=7 a) 0B01100011 0B __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ b) 0B11100110 0B10001100 0B10111000 0B __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ c) 0B11110100 0B10101010 0B10010101 0B10100000 0B __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __