


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The answers to the final exam questions from a fall 2005 computer science course. The questions cover topics such as programming language design, interface implementation, xml apis, and concurrent data structures.
Typology: Exams
1 / 4
This page cannot be seen from the preview
Don't miss anything!



December 20th, 2005
public V putIfAbsent(K key,V value)
Provide a static method for a utility class
public static V putIfAbsent(Map map, K key, V value)
that tries to provide the same functionality as best as possible for any Map Don’t use any casts. If is does not provide exactly the same functionality as the putIfAbsent defined in ConcurrentHashMap, explain why. Answer: Here is an method that works in a single threaded context.
public static V putIfAbsent(Map map, K key, V value) { V v = map.containsKey(key); if (v != null) return v; map.put(key, value); return null; }
This won’t work in a multithreaded context, because the containsKey and put operations will not be performed atomically. You could try locking the map:
public static V putIfAbsent(Map map, K key, V value) { synchronized(map) { V v = map.containsKey(key); if (v != null) return v; map.put(key, value); return null; } }
but that won’t provide thread safety because not all thread safe Maps block by synchronizing on the map object. In particular, ConcurrentHashMap doesn’t perform synchronization on the that way, and there is no way to prevent other updates from occurring to a Concurren- tHashMap. Thus, any sequences of operations that need to be performed atomically on a ConcurrentHashMap need to be supported as a primitive operation.
(a) Give a well-formed XML document that validates against simple.dtd that does not contain a D element. Answer: Hello (b) Give a well-formed XML document that validates against simple.dtd that contains a D element. Answer: Hello World
(c) Give an XPath query string that will match all C elements. Answer: //C (d) Give an XPath query string that will match all C elements contained inside a B element. Answer: //B/C (e) Give an XPath query string that will match all C elements not contained inside a B element. Answer: /A/C