Core Java Interview Questions Page3
Q 1. How can I get the full path of Explorer.exe ?
Ans :
Generally Java sandbox does not allow to obtain this reference so that system
can be access from an applet loaded via the Internet.
Q 2. : How do I limit the scope of a file
chooser?
Ans : Generally FileFilter
is used to limit
file selection. You could write your own custom filter that only takes files in
a specified directory at a particular instance. This does not interrupt users
from browsing the file system but ensures that no other files is selected.
Q 3. Is it possible to convert a string to an
abstract path ?
Ans : The
class java.io.File
class contains
several methods which create abstract file paths, that can be created on the
file system or can be used to test whether the actual file paths exist.
Constructor File(String)
receives an
argument having a complete abstract path name. The constructor File(File,
String)
takes File
and String
where
argument File
contains parent
directory reference and the String
is a path name under that directory.
Q 4. What's the difference between a data store
and a database ?
Ans :
Generally the term data store is used for a storage system such as relational
database that may be used to store and retrieve information. For example, the
term data store is used in Enterprise Java Beans (EJB) specification allowing
application server vendors to implement storage according to their choice.
Mostly data stores are implemented in the form of a relational database, but it
can take other forms also.
Q 5. Why not statically import a database
driver class ?
Ans : The
standard import statement makes a class available to the class that imports it
so that its public variables and method can be used. Importing and using
specific JDBC driver implementations is done in the same way, provided they are
available on the application's classpath. Java SQL package and JDBC scheme are
designed that deal with database interfaces in a generic way that is not bound
to use any single driver implementation. Multiple drivers can be loaded in a
single application.
Loading a specific driver and invoking its connect(String,
Properties)
method directly may be
possible, using the static Class.forName(String)
method and getting connections through the DriverManager
's getConnection()
methods is more suitable. This makes it possible to configure application's
database driver using external configuration or properties, instead of hard
coding the class references. It only requires declaration of Java interface
types for the driver, connection, statements, rather than vendor-specific
classes.
Q 6. How can I insert multiple records at a
time ?
Ans : Use of
multiple inserts to a database with a single SQL statement does not make sense
from a database management point of view. INSERT
statement
creates records and these records should be distinct from each other otherwise
they will be redundant and inefficient use of storage. An UPDATE
statement can update
multiple records by using a single statement and it is a more appropriate way of
managing the user data. Inserting multiple records by using a PreparedStatement
is a better way of adding multiple different records to the database. To add
multiple records to the database PreparedStatement
uses iterator.
Q 7. How can I insert a new row between two
others ?
Ans : Relational
database does not required to insert records in a particular sequence. Inserting
a record to the database the application usually inserts the records at the end
of the table. If listing of data in a particular sequence is important for you
application then you should generally use a data field to determine the sort
order. Lets take an example, Suppose you want to sort the data by a by a
customer's last name and first name by using an SQL ORDER
BY
statement, then you would use the
query:
SELECT * FROM customer ORDER BY last_name, first_name; |
Q 8. How can I select records between two dates
?
Ans : In
order to select records by date, a database table must have a DATE
or TIMESTAMP
field for using in the WHERE
condition of the query statement. E.g. For a table having product order you must
have a date_ordered
field of DATE
data type. Secondly, since the SQL query input is submitted in the form of a
string therefore the two dates must be type cast to DATE
data types.
Q 9. How do I get the keys from a Map ?
Ans : The
method keySet()
is used to get the
keys in a map rather than the values, which returns a java.util.Set
object containing all the keys. Then get an Iterator
from the key set, or process the keys by using other methods of the Set
interface.
Q 10. How do I insert slashes in a date ?
Ans : Java
uses java.text.SimpleDateFormat
in
order to format Date
types. This class
receives a string having time pattern and applies it to the given date like a
template. For instance use the pattern "dd/MM/YYYY" in order to create
a date with slashes.
Q 11. How can I get the difference between two
dates in days ?
Ans : In
order to compare dates by day, deduct the millisecond time of the earlier date
from the later one and divide the result by the number of milliseconds in a day.
This method checks whether the two dates are same and
then applies after(Date)
method to check
the deduction is the right way round. In order
to compare in a better way by hour or minute, checking of any possible timezone
offset is also important.
Q 12. Why are wait(), notify() and notifyall()
methods defined in the Object class ?
Ans : All
of the methods like wait(), notify() and notifyall() are defined in the Object class.
These are the necessary methods for all the objects to undergo
multithreading. All of these are part of every object created in java language programming.
These methods are discussed in detailed on the Java
Software Development Kit JavaDoc page for the Object
class.