12% of course grade. CSCI 201L Final Written SOLUTION Fall /7 - PDF

Please download to get full document.

View again

of 7
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Information Report
Category:

Entertainment & Media

Published:

Views: 0 | Pages: 7

Extension: PDF | Download: 0

Share
Related documents
Description
12% of course grade 1. Interfaces and Inheritance Does the following code compile? If so, what is the output? If not, why not? Explain your answer. (1.5%) interface I2 { public void meth1(); interface
Transcript
12% of course grade 1. Interfaces and Inheritance Does the following code compile? If so, what is the output? If not, why not? Explain your answer. (1.5%) interface I2 { public void meth1(); interface I1 extends I2 { public void meth1(); class C1 implements I1 { public void meth1() { System.out.println( 2 ); class C2 extends C1 implements I2 { public void meth1() { System.out.println( 1 ); public class Question1 { public static void main(string[] args) { I1 i1 = new C1(); i1.meth1(); I2 i2 = new C2(); i2.meth1(); C1 c1 = new C2(); c1.meth1(); 0.5% - The code does compile. 0.5% - The output is % The runtime type of the object will determine which method gets called. 1/7 2. Network Addresses Assume that your employer decided to disable DHCP and instead have each employee assigned a static IP address. Your laptop is assigned IP address with no subnets. At home, you have DHCP enabled on your network. You forgot to change your static IP address from work one day on your laptop when you got home, and you didn t have network access. You checked your other computer (which does have network access), and you saw the IP address on your other computer on your home network was with no subnets. Explain why your laptop does not have network access on your home network. (1.5%) = = % - These are Class C addresses. 1.0% - These IP addresses are in different networks. A Class C address uses 24 bits for the network address, which would be the first three octets. Since the first three octets of each IP address do not match ( and ), that means that the IP addresses are in different networks. An IP address in one network will not be able to communicate when it is in a different networek. 2/7 3. Serialization Does the following code compile? If so, what is the output? If not, explain why. (0.5% + 1.0%) import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.io.objectinputstream; import java.io.objectoutputstream; import java.io.serializable; class SerializeMe implements Serializable { public static final long serialversionuid = 2; public transient String s1 = csci201 ; public String s2 = csci103 ; public int num = 3; public void print() { System.out.println(serialVersionUID); System.out.println(s1); System.out.println(s2); System.out.println(num); public class Question3 { public static void main(string[] args) { try { SerializeMe sm1 = new SerializeMe(); sm1.print(); FileOutputStream fos = new FileOutputStream( output.txt )); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeobject(sm1); oos.close(); FileInputStream fis = new FileInputStream( output.txt ); ObjectInputStream ois = new ObjectInputStream(fis); SerializeMe sm2 = (SerializeMe)ois.readObject(); ois.close(); sm2.print(); catch (IOException ioe) { System.out.println( IOE: + ioe.getmessage()); catch (ClassNotFoundException cnfe) { System.out.println( CNFE: + cnfe.getmessage()); 0.5% - The code does compile. 1.0% - The output is 2 csci201 csci null (NOTE: If they have csci201 here, deduct 0.5%) csci /7 4. Monitors and Locks When Java uses the synchronized keyword, it is utilizing monitor functionality. There is also Lock functionality in Java through the Lock interface. Is the functionality behind a monitor the same as that through an explicit Lock variable? Why would a programmer choose to use a Lock instead of a monitor? (1.0% + 1.0%). 1.0% - A monitor consists of a lock, but we do not have access to Conditions on the underlying lock. 1.0% - A programmer would choose to use a Lock when Conditions are needed. 0.5% if they say that a programmer could also choose to use a Lock for personal preference. 4/7 5. Semaphores We learned three different ways to implement critical sections of code with concurrent programming monitors, locks, and semaphores. Assume one of your friends tells you about this new programming language called JavaSem. It has all of the functionality of Java but no monitors and locks, just semaphores. Would you consider this a limitation to the language or would JavaSem be able to implement all of the functionality of Java? Explain your answer. (2.0%) 1.0% - Semaphores can be used to implement monitor and basic lock functionality by setting the number of permits to 1 (which is also called a binary semaphore). 1.0% - We do not have Conditions on semaphores though, so we would need to utilize additional data to implement this functionality. Students can either propose something here (such as using a semaphore with no permits to force waiting), but that would require much though. Grade this part leniently. Other answers may be acceptable here as well, but the above are the two most important reasons. 5/7 6. Distributed Programming RMI, CORBA, and Web Services all have advantages and disadvantages, which is why all three of them still exist. Assume you are working at a company who does not have any distributed application currently but wants to implement one because of the heavy processing power needed to factor large numbers. Give three design decisions that you would need to take into consideration to determine which distributed computing paradigm to use, along with the paradigm to use based on the decision. (0.5% + 0.5% + 0.5%) Here is a sample answer: The language of the client and server should be taken into consideration. If both can be written in Java, RMI should be used. If they may be in different languages, CORBA or Web Services should be used. 0.5% - If the network administrators do not want to open an additional port through the firewall, Web Services should be used since it operates over the standard HTTP port, whereas RMI and CORBA do not. 0.5% - If the data being transmitted is very complicated with many objects, RMI may want to be used to reduce the details of mapping objects in different languages down to IDL. 0.5% - If the server will be accessed from outside the organization, CORBA or Web Services may be used because they allow you to implement explicit security policies. Other answers will be acceptable as well. 6/7 7. Threads Give three possible outputs of the following code? What are two rules that will always be true for the output (i.e. what will always be printed before or after something else)? (1.0% + 1.0%) class T extends Thread { private int num; public T(int num) { this.num = num; public void run() { System.out.println(num + a ); Thread.yield(); System.out.println(num + b ); public class Question7 { public static void main(string[] args) { T t[] = new T[4]; for(int i=0; i 4; i++) { t[i] = new T(i); t[i].setpriority(thread.min_priority + i); for(int i=0; i 4; i++) { t[i].start(); Outputs these are three sample outputs, but as long as the outputs correspond to the rules below, they will be accepted (0.4% for the first output, 0.3% for each of the other two outputs) 3a 3a 2a 3b 2a 3a 2a 1a 2b 2b 0a 3b 1a 0b 1a 1b 1b 0a 0a 2b 1b 0b 3b 0b Rules the following two rules are the only rules. 0.5% - The a output for a specific number will always be printed before the b of a specific number. 0.5% - There will always be 8 outputs. This is something that we will give credit for even though it is not a rule: 0.5% - The priority will not necessarily be acknowledged, but the higher numbered threads (which have higher priorities) will most likely be printed before the lower numbered ones. 7/7
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks