Utility
Questions With
Answers in Java
Utility Package
Q: 1) What is the Vector class?
Ans : The Vector class provides the capability to implement a growable
array of objects.
Q: 2) What is the Set interface?
Ans : The Set interface provides methods for accessing the elements of
a finite mathematical set.Sets do not allow duplicate elements.
Q: 3) What is Dictionary class?
Ans : 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.
Q: 4) What is the Hashtable class?
Ans : 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.
Q: 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().
Q: 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);
}
}
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
Ans : B) Cast v.elementAt(0) to a String
Q: 7) 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){}
}
}
A) abcdef
B) defabc
C) abcabc
D) defdef
Ans : B) defabc
Q: 9) Which of the following may have duplicate elements?
A)Collection
B) List
C) Map
D) Set
Ans : A and B Neither a Map nor a Set may have duplicate elements.
Q: 10) Can null value be added to a List?
Ans : Yes.A Null value may be added to any List.
Q: 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);
}
}
A) abcdefabcdef
B) defabcdefabc
C) fedcbafedcba
D) defabc
Ans : D) defabc. Sets may not have duplicate elements.
Q: 12) Which of the following java.util classes support internationalization?
A) Locale
B) ResourceBundle
C) Country
D) Language
Ans : A and B . Country and Language are not java.util classes.
Q: 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.
Q: 14) How are Observer Interface and Observable class, in java.util package,
used?
Ans : 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.
Q: 15) Which java.util classes and interfaces support event handling?
Ans : The EventObject class and the EventListener interface support
event processing.
Q: 16) Does java provide standard iterator functions for inspecting a
collection of objects?
Ans : 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: 17) The Math.random method is too limited for my needs- How can I generate
random numbers more flexibly?
Ans : 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();
Q: 18) How can we get all public methods of an object dynamically?
Ans : 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.
Tolal:0 Click:
Show All Comments