Previous | Home | Next |
Answer: The Vector class provides the capability to implement a growable array of objects.
Answer: The Set interface provides methods for accessing the elements of a finite mathematical set.Sets do not allow duplicate elements.
Answer: The Dictionary class is the abstarct super class of Hashtable and Properties class.Dictionary provides the abstarct functions used to store and retrieve objects by key-value.This class allows any object to be used as a key or value.
Answer: The Hashtable class implements a hash table data structure. A hash table indexes and stores objects in a dictionary using hash codes as the objects' keys. Hash codes are integer values that identify objects.
Answer: The properties class is a subclass of Hashtable that can be read from or written to a stream.It also provides the capability to specify a set of default values to be used if a specified key is not found in the table. We have two methods load() and save().
import java.util.*; class Ques{ public static void main (String args[]) { String s1 = "abc"; String s2 = "def"; Vector v = new Vector(); v.add(s1); v.add(s2); String s3 = v.elementAt(0) + v.elementAt(1); System.out.println(s3); } }
(a). Declare Ques as public
(b). Cast v.elementAt(0) to a String
(c). Cast v.elementAt(1) to an Object.
(d). Import java.lang
Answer: (b). Cast v.elementAt(0) to a String
import java.util.*; class Ques{ public static void main (String args[]) { String s1 = "abc"; String s2 = "def"; Stack stack = new Stack(); stack.push(s1); stack.push(s2); try{ String s3 = (String) stack.pop() + (String) stack.pop() ; System.out.println(s3); }catch (EmptyStackException ex){} } }
(a). abcdef
(b). defabc
(c). abcabc
(d). defdef
Answer: (b). defabc
(a). Collection
(b). List
(c). Map
(d). Set
Answer: A and B Neither a Map nor a Set may have duplicate elements.
Answer: Yes.A Null value may be added to any List.
import java.util.*; class Ques{ public static void main (String args[]) { HashSet set = new HashSet(); String s1 = "abc"; String s2 = "def"; String s3 = ""; set.add(s1); set.add(s2); set.add(s1); set.add(s2); Iterator i = set.iterator(); while(i.hasNext()) { s3 += (String) i.next(); } System.out.println(s3); } }
(a). abcdefabcdef
(b). defabcdefabc
(c). fedcbafedcba
(d). defabc
Answer: (d). defabc. Sets may not have duplicate elements.
(a). Locale
(b). ResourceBundle
(c). Country
(d). Language
Answer:
(a). Locale
(b). ResourceBundle
Answer: The ResourceBundle class also supports internationalization. ResourceBundle subclasses are used to store locale-specific resources that can be loaded by a program to tailor the program's appearence to the paticular locale in which it is being run. Resource Bundles provide the capability to isolate a program's locale-specific resources in a standard and modular manner.
Q.13 How are Observer Interface and Observable class, in java.util package, used?
Answer: Objects that subclass the Observable class maintain a list of Observers. When an Observable object is updated it invokes the update() method of each of its observers to notify the observers that it has changed state. The Observer interface is implemented by objects that observe Observable objects.
Answer: The EventObject class and the EventListener interface support event processing.
Q.15 Does java provide standard iterator functions for inspecting a collection of objects?
Answer: The Enumeration interface in the java.util package provides a framework for stepping once through a collection of objects. We have two methods in that interface.
public interface Enumeration { boolean hasMoreElements(); Object nextElement(); }
Q.16 The Math.random method is too limited for my needs- How can I generate random numbers more flexibly?
Answer: The random method in Math class provide quick, convienient access to random numbers, but more power and flexibility use the Random class in the java.util package.
double doubleval = Math.random();
The Random class provide methods returning float, int, double, and long values.
- nextFloat() // type float; 0.0 <= value < 1.0
- nextDouble() // type double; 0.0 <= value < 1.0
- nextInt() // type int; Integer.MIN_VALUE <= value <= Integer.MAX_VALUE
- nextLong() // type long; Long.MIN_VALUE <= value <= Long.MAX_VALUE
- nextGaussian() // type double; has Gaussian("normal") distribution with mean 0.0 and standard deviation 1.0)
Example:
Random r = new Random(); float floatval = r.nextFloat();
Answer: By using getMethods(). It return an array of method objects corresponding to the public methods of this class.
getFields() returns an array of Filed objects corresponding to the public Fields(variables) of this class. getConstructors() returns an array of constructor objects corresponding to the public constructors of this class.
Previous | Home | Next |