You are on page 1of 2

Java Programmer Technical Test

The Cloud Networks Ltd June 2007


When answering the following questions, you can make use of any gener- ally available reference materials. You are expected to answer coding questions in Java (any version), but answers in pseudo-code or other languages may be acceptable.

1
1.1

Implementation Problems
Question 1

Answer any two of the following three questions. Write a program that performs the following task: Given an arbitrary XML document, reverse the order of all elements, preserving parent-child relationships. If an element contains more than one XML attribute, the order of these should also be reversed. The program should act as a UNIX filter, taking a UTF-8 encoded XML document on standard input and publishing the reversed document to standard output. Example: $ cat input.xml <?xml version="1.0"?><document> <element1 attr1="foo" attr2="bar"><!-- Comment --> <subelement1> Hello world </subelement1><subelement2> Goodbye world </subelement2> Fred &amp; Ginger </element1> <element2> Test XML document </element2></document> 1 1.2 Question 2 1 IMPLEMENTATION PROBLEMS $ cat input.xml | java Reverse <?xml version="1.0"?><document> <element2> Test XML document </element2><element1 attr2="bar" attr1="foo"> Fred &amp; Ginger<subelement2> Goodbye world </subelement2> <subelement1> Hello world </subelement1> <!-- Comment --></element1> </document>

1.2

Question 2

Define a general purpose data structure to represent a user and their login cre- dentials. It must store the users full name, username, password and address and phone number information. When stored in a sorted collection, it must be sorted by surname first, then by their first name. Provide methods to cre- ate a new user object, and to check whether a given password is correct. The object must be capable of serialisation, but when serialised only a hash of the password should be stored.

1.3

Question 3

Consider a database with the following structure: CREATE TABLE catalogue ( product_id INTEGER, product_name VARCHAR(250), price_pence INTEGER ); CREATE TABLE orders ( order_ref INTEGER, ); product_id INTEGER, num_ordered INTEGER Write a SQL query that shows the product ID, name and total ordered value of all items that have more than 100 worth of total orders. Extend the database schema to include foreign key constraints between tables and define appropriate primary keys on each table. 2

2
Extend the schema to ensure that items with a duplicate product ID can- not be inserted. Also ensure that a product can only be ordered once for a particular order reference. What indexes would you add to speed up queries to these tables? What different types of database indexes are generally available, and which ones would you use for this problem and why?

Design Question
Imagine a dynamic web-based application with user authentication. Users en- ter their username and password into a login form before they can access any part of the application. Once authenticated, Javascript running within the users browser refreshes an element of the page by polling the server every thirty seconds. The application is running on a cluster of JBoss machines, a load- balancer will ensure that each user is directed to the same cluster member for the duration of a session (defined by a transient cookie on the users machine which is only held for the duration of a session). Each machine accesses a shared database, but the load on this machine is very high. A new requirement has been raised where we must ensure that users can only be logged in on one client machine at any time. If they attempt to log in on a second client machine, or open a new browser with a new session, this login must succeed and they must be logged out on the first client machine. It is not acceptable to deny the second login (assuming they log in correctly). The original session must be invalidated and the polling from that browser must stop within 1 minute of the second login, this is a hard limit but a quicker solution would be preferable. How would you approach this problem? Identify what areas of the applica- tion would be impacted and lay out a design for your solution, using diagrams and pseudocode where necessary. Indicate how you would deal with the sce- nario where the second login attempt is directed to a different cluster member by the loadbalancer. Is it possible to implement this solution by using services provided by JBoss or another J2EE application server, without accessing the database?

3
3.2

Java Trivia 3.1


Question 2

Question 1

What is the difference between java.util.Vectorand java.util.ArrayList? What would happen if two concurrent threads try and remove the third object of such a collection? The java.util.ArrayList, java.util.HashMapand java.util.TreeSetobjects all store a collection of objects. Why would you choose one over an- other, and what are the performance implications of each for the following situations: 3 3 JAVA TRIVIA 3.3 Question 3 3 JAVA TRIVIA Access to front/back of collection Random access to arbitrary object identified by an index Searching for a given object within the collection Deletion of an object from the middle of the collection Insertion at the beginning of the collection

3.3

Question 3

When executed, the following code displays 2. Why? public class Code { } private A v; public Code() { v = new B(); } public intgetValue() { return v.value; } class A { int value = 2; } class B extends A { int value = 3; } public static void main(String args[]) { Code code = new Code(); } System.out.println(code.getValue()); 4

You might also like