Evalueserve Java Questions: Utility Package

Download PDF of This Page (Size: 155K)

Examrace Placement Series prepares you for the toughest placement exams to top companies.

  1. What is the Vector class? ANSWER: The Vector class provides the capability to implement a growable array of objects.

  2. What is the Set interface? ANSWER: The Set interface provides methods for accessing the elements of a finite mathematical set. Sets do not allow duplicate elements.

  3. What is Dictionary class? 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.

  4. What is the Hashtable class? 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.

  5. What is the Properties class? 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 ().

  6. What changes are needed to make the following prg to compile? 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); } } ANSWER: Declare Ques as public Cast v. ElementAt (0) to a String Cast v. ElementAt (1) to an Object. Import java. Lang ANSWER: Cast v. ElementAt (0) to a String

  7. How can we get all public methods of an object dynamically? 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.

  8. What is the output of the prg. 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) { } } } ANSWER: Defabc

  9. Which of the following may have duplicate elements? ANSWER: A and B Neither a Map nor a Set may have duplicate elements.

  10. Can null value be added to a List? ANSWER: Yes. A Null value may be added to any List.

  11. What is the output of the following prg. 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); } }

    1. abcdefabcdef

    2. defabcdefabc

    3. fedcbafedcba

    4. defabc

    ANSWER: Defabc. Sets may not have duplicate elements.

  12. Which of the following java. Util classes support internationalization?

    1. Locale

    2. ResourceBundle

    3. Country

    4. Language

    ANSWER: A and B, Country and Language are not java. Util classes.

  13. What is the ResourceBundle? 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.

  14. 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.

  15. Which java. Util classes and interfaces support event handling? ANSWER: The EventObject class and the EventListener interface support event processing.

  16. 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 (); }

  17. 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

Eg. Random r = new Random ().

float floatval = r. NextFloat ().