Question 1 (a) F (b) F (c) F (d) F (e) F Question 2 public class Game { private int number[]=new int[2]; public boolean answered=false; public Game() { number[0]=(int)(Math.random()*101); number[1]=(int)(Math.random()*101); } syncrhonized public int[] getNumber() { if (answered) { return null; } return number; } synchronized public boolean answer(int i) { if (!answered && i==number[0]+number[1]) { answered=true; return true; } return false; } } Question 3 (a) If we want to write an object to an output stream, the class of the object must have implemented the Serializable interface. (b) Yes. (c) When an attribute is declared as transient, it will not be written when the object is written to an output stream. Question 4 public class Count extends Thread { private String file; public Count(String file) { this.file=file; } public void run() { try { FileReader input=new FileReader (file); char buffer[]=new char[1000]; int ano=0; while (true) { int no=input.read(buffer); if (no==-1) { break; } for (int i=0;i<no;i++) { if (buffer[i]=='a') { ano++; } } } System.out.println(file+":"+ano); } catch (Exception e) {} } public static void main(String st[]) { for (int i=0;i<st.length;i++) { Count c=new Count(st[i]); c.start(); } } } Question 5 (a) (i) (ii) (iii) Yes. The dropping of a packet does not matter. No. The dropping of a packet is not acceptable. No. The dropping of a packet is not acceptable. (b) public class DatagramServer { public static void main(String st[]) { try { int stack[]=new int[10]; int no=0; DatagramSocket ss=new DatagramSocket(12345); while (true) { byte[] buf=new byte[100]; DatagramPacket rPacket=new DatagramPacket(buf,buf.length); ss.receive(rPacket); DataInputStream input=new DataInputStream(new ByteArrayInputStream(buf)); int code=input.readInt(); switch (code) { case 0: if (no<10) { stack[no++]=input.readInt(); } break; case 1: if (no>0) { int num=stack[--no]; ByteArrayOutputStream bOutput=new ByteArrayOutputStream(); DataOutputStream output=new DataOutputStream(bOutput); output.writeInt(num); output.close(); byte[] b=bOutput.toByteArray(); DatagramPacket sPacket=new DatagramPacket(b,b.length); sPacket.setAddress(rPacket.getAddress()); sPacket.setPort(rPacket.getPort()); ss.send(sPacket); } } } } catch (Exception e) { } } } Question 6 <tr><th>Book</th><th>Authors</th></tr> <% java.sql.Statement statement = con.createStatement(); java.sql.ResultSet re = statement.executeQuery("select callnumber,title from book"); while (re.next()) { String callnumber = re.getString(1); String title = re.getString(2); %><tr><td><%=title%></td><td> <% java.sql.Statement statement2 = con.createStatement(); java.sql.ResultSet re2 = statement2.executeQuery("select author from author where callnumber='" + callnumber + "'"); boolean first=true; while (re2.next()) { String author=re2.getString(1); if (first) { first=false; } else { out.print(","); } out.println(author); } %> </td> </tr><% } %> </table> Question 7 (a) A stateless protocol is one in which the server would not remember the contents of previous requests from the same client. (b) Since HTTP is a stateless protocol, therefore it is difficult to implement services like online shop because in such application, it is necessary to remember those a customer has selected to buy. (c) (d) Using hidden values in a form. When the form is submitted, the hidden values will be sent too. Using cookies. Cookies are pieces of information sent by the server to the client. When a client submit another request to the server, the cookie will be submit too. JavaBeans are used to share information in JSP pages. Question 8 <xs:element name="books"> <xs:complexType> <xs:element minOccurs="0" maxOccurs="unbounded" name="book"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element minOccurs="1" maxOccurs="unbounded" name="author" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:complexType> </xs:element> Question 9 (a) A statement block groups a number of statements together. It is needed when there is a need to group a number of statements into one statement. In languages like C, Java, most constructs usually can accept one statement only. So there is a need in these languages to have statement blocks. (b) The design in Java is better because allowing the declaration of a variable twice is very dangerous. It is very likely that the programmer will be confused by which variable is which. Question 10 (a) "throws" is used in declaring a method to show that it will throw certain kinds of exceptions. "throw" is used to throw an exception. (b) static public void set(int a[], int i) { try { a[i]=i*2; return a[i]; } catch (Exception e) { return 0; } } Question 11 (a) (i) meth2 is static and there we cannot invoke a non-static method meth1 inside it. [1 mark] (ii) We need to handle the exception thrown from opening a file reader. [1 mark] (iii) i has not been initialized before use. [1 mark] (iv) meth1 is an int function and therefore it must have at least one return statement. [1 mark] (v) A does not have the default constructor and therefore B should have a constructor and then involve the super constructor inside it. [1 mark] (b) public class Server extends Thread { private java.net.Socket socket; public Server(java.net.Socket s) { socket=s; start(); } public void run() { try { java.io.DataInputStream input=new java.io.DataInputStream(socket.getInputStream()); java.io.DataOutputStream output=new java.io.DataOutputStream(socket.getOutputStream()); int n1=input.readInt(); int n2=input.readInt(); int hcf=1; int min=n1; if (n1<1 || n2<1) { output.writeInt(-1); input.close(); output.close(); return; } if (n2<min) { min=n2; } for (int i=min;i>=1;i--) { if (n1%i==0 && n2%i==0) { output.writeInt(i); input.close(); output.close(); return; } } } catch (Exception e) { } } /** Creates a new instance of Server */ public static void main(String st[]) throws Exception { java.net.ServerSocket ss=new java.net.ServerSocket(12345); while (true) { java.net.Socket s=ss.accept(); new Server(s); } } } Question 12 (a) (i) A has higher writability as the programmer is now free to use any variables without declaring it. A has lower readability as the reader may find it difficult to guess the type of the variable. A has lower reliability as there is more likely for the program to has type errors. (ii) A has higher writability as the programmer is now free to change the value of the loop counter. A has lower readability as the reader is not difficult to trace the loop. A has lower reliability as the loop is more likely to be an endless loop. (b) public class Server extends UnicastRemoteObject implements ServerInterface { private java.util.Vector<User> users=new java.util.Vector<User>(); public Server()throws RemoteException { } public void register(User user) throws RemoteException { synchronized (users) { users.add(user); } } public void bye(User user) throws RemoteException { synchronized (users) { users.remove(user); } } public void sendMessage(String message) throws RemoteException { synchronized (users) { for (User u:users) { u.sendMessage(message); } } } } Question 13 (a) (i) Language design time [1 mark] (ii) run time [1 mark] (iii) load time [1 mark] (iv) Language design time or language implement time (accept any of these). [1 mark] (b) (i) The maximum length of an identifier should be long enough, like 256 characters. Both uppercase and lowercase letters should be allowed. Therefore should be separators like '_'. (ii) The loop counter must be declared at the loop. The programmer is not allowed to manipulate the counter explicitly. (iii) The subscripts can be any subrange of integers, enumerated data types. (c) a: life time: from start of program to end of program, scope: within the class. b: life time: from start of creation of the object to final garbage collection of the object. scope: everywhere c: life time: from start of the invocation of the method meth to end of the invocation. scope within the method. d: life time: from start of the execution of the method meth to end of the execution of the method. scope: within the statement block. Question 14 (a) (i) It is because the public key method is very computationally expensive. So it is only used to encrypt small amount of information. [2.5 marks] (ii) We can use both the public key and secret key method. First we use a secret key to encrypt the message. Then the secret key is encrypted with the public key. The encrypted message and key were sent to the receiver. The receiver use the private key to decrypt the secret key, then the message is decrypted using the secret key. [2.5 marks] (b) Message digest is generated by applying a hash function on a message. The function is available in the public domain and therefore anybody can intercept a message and the corresponding digest. Then he can make changes to the message and then re-generated the digest on the modified contents. There is no way that the recipient will notice this. In order to protect the integrity, it must be used with cryptography. The sender should encrypt message digest. Then the recipient would decrypt it and compare it with the message digest he generate from the message. (c) import java.security.*; import java.io.*; public class CreateDigest { public static void main(String st[]) { try { MessageDigest md = MessageDigest.getInstance("SHA-1"); byte[] buffer=new byte[1000]; FileInputStream input=new FileInputStream(st[0]); FileOutputStream output=new FileOutputStream(st[1]); while (true) { int no=input.read(buffer); if (no==-1) { break; } md.update(buffer,0,no); } byte[] re=md.digest(); output.write(re); input.close(); output.close(); } catch (Exception e) { } } }