Intermediate Java
Week 4 Review Questions
-
1. What is a Socket?
- A Socket is a Java object that repreents a connection between two machines, where a peice of software in one machine knows about a piece of software in the other machine.
- 2. What two things you need to know to make a Socket connection?
- You need to know the IP address of the machine you are making the Socket connection with and the TCP port the other machine will use.
- 3. What is a TCP port?
- A TCP port is a unique identifier that allows your computer to connect to the kind of software needed to send your message.
- 4. Why shouldn't you use TCP port numbers from 0 to 1023?
- Ports 0 to 1023 are reserved for specific services your computer is doing, and with which you do not want to interfere, like your own server ( port 80), your FTP communication ( port 20) your telnet and time connections ( ports 23,and 37) your SMTP and POP3 mail servers ( ports 25 and 110. ) If you use these ports you will mess up these programs.
- 5. Can there ever be more than one program running on a single port?
- Only one application can run from a port. If you try to write (in your Java program) another, you will get a BindException.
- 6. What class should you use to read data from a Socket?
- Buffered Reader chained to InputStreamReader chained to InputStream
- 7. What class should you use to write data to a Socket?
- PrintWriter chained to OutputStream.
- 8. What happens when a ServerSocket gets a request?
- The ServerSocket passes the client off to another Socket to deal with the transaction so that the ServerSocket is free to listen for another client. (Like a phone receptionist who answers your call and instantly transfers you off to the person you wanted to talk to ( or to HOLD for eternity).
- 9. True or false: every thread in Java has its own call stack?
- YES.
- 10. What is the difference between thread and Thread?
- A thread is the separate thread of execution with its own call stack, and a Thread is a java object which represents that thread in your program.
- 11. What is a thread's job?
- To do the method that you have assigned it to do ( the Runnable - ie the method called run() ) and then die.
- 12. True or false: The Runnable interface has a single method?
- yes -
public void run()
- 13. What do you need to do to launch a new thread?
- You need to make an object of your Runnable class, and the object must include the method
public void run() ( because that's what the Thread will do, and what the thread will be used for.) Runnable someRunnableObject = new someClassThatImplementsRunnable You pass the instance of the Runnable class as an argument into the Thread constructor when you make your Thread object: Thread someThreadName = new Thread(someRunnableObject)
- 14. What could cause a thread to be moved from the RUNNING state to a BLOCKED state?
- It may have a method to do that it needs to wait for data for; maybe it needs input from another method, and that input is not yet availabel.
- 15. What is the purpose of the static Thread.sleep() method?
- To slow down a program that will execute too quickly , or to force the current thread to change to another thread.
- 16. What is the purpose of setName() method?
- In cases where more than one thread is running, to identify the thread that is
- 17. What can happen if two or more threads access the same object on the heap?
- One thread can interfere with the performance of the other by altering data as it is being processed in an incorrect way.
- 18. What would you use the keyword synchronized for?
- to make a method so that only one Thread can access it at a time.