Title: Java Interview Questions & Answers
1Java Interview Q A
- By www.bestonlinetrainers.com
2Q1 To implement key Object in HashMap which two
methods are used?
- Answer
- In HashMap, to use any object as Key it must
implement equals and HashCode method in Java.
Learn Java. Ask for a free demo
www.bestonlinetrainers.com
3Q2What is an immutable object? Mention an
immutable object?
- Answer
- Objects that cannot be modified once created are
immutable objects in JAVA. A new object can be
resulted due to any modification in an immutable
object. For example, in JAVA, String is
immutable. In order to prevent sub class from
overriding methods in Java which can compromise
Immutability, immutable object are mostly final
in JAVA. This same functionality can be achieved
by making member as non final but private and not
modifying them except in constructor.
Learn Java. Ask for a free demo
www.bestonlinetrainers.com
4Q3WState the difference between creating String
as new() and literal?
- Answer
- When string is created with new() Operator,
its created in a heap and not added into string
pool whereas using literal String are created in
String pool itself which exists in PermGen area
of heap, Example String s new String("Test")
It does not put the object in String pool , but
needs to be called String.intern() method which
is used to put them into String pool explicitly.
Its only then thats it create String object as
String literal. For e.g. String s "Test" Java
automatically put that into String pool
5Q4What is difference between StringBuffer and
StringBuilder in Java?
- Answer
- Classic Java questions can seem to be tricky
and very easy. In Java 5, StringBuilder is
introduced and the only difference between both
of them is that StringBuffer methods are
synchronized while StringBuilder is non-
synchronized.
6Q5 Overriding HashCode() method has any
difference in performance implication?
- Answer
- Yes .A poor HashCode function will result in
frequent collision in HashMap which eventually
increase time for adding an object into Hash Map.
7Q6 While writing stored procedure or accessing
stored procedure from java how is the error
condition handled?
- Answer
- This is one of the tough questions normally
asked in Java interview. The stored procedure
should return error code if some operation fails
but if stored procedure itself fails than
catching SQLException is the only choice left .
8Q7 At the compile time are the imports checked
for validity? That is, will the code containing
an import such as java.lang.ABCD compile up?
- Answer
- Yes. The imports are checked for the
semantic validity at the compile time. The above
line of import will not compile if it has the
code, an error is shown saying, cannot resolve
symbol. - Symbol class ABCD
- Location package io
- Import java.io.ABCD
9Q8 State difference between Executor.submit()
and Executer.execute() method?
- Answer
- When looking at exception handling, there is
a difference. If the tasks throws an exception
and if it was submitted with the execute then
this exception will go to the uncaught exception
handler (that is when one explicitly is not
provided, the default one will just print the
stack trace to System.err). If the task is
submitted with any thrown exception or a checked
exception or not, is then part of the task's
return status. And for a task that was submitted
with submit and that terminates with an
exception, the future.get will re-throw this
exception wrapped in an ExecutionException.
10Q9 What is the difference between factory and
abstract factory pattern?
- Answer
- One more level of abstraction is provided by
Abstract Factory .Different factories from each
Abstract Factory responsible for the creation of
different hierarchies of objects based on type of
factory are considered. For example Abstract
Factory extended by AutomobileFactory,
UserFactory, RoleFactoryetc.For every object
created in that genre, each individual factory
would be responsible.
11Q10 Define Singleton? Which is better to make
whole method synchronized or only critical
section synchronized?
- Answer
- Singleton in JAVA is a class with just one
instance in the whole Java application. One such
example is java.lang.Runtime is a Singleton class
.With the introduction Enum by Java 5, creating a
Singleton is easy rather than being tricky as
earlier.
12Want to learn Java?
- Visit http//www.bestonlinetrainers.com/programm
ing/java-training.html - Email info_at_bestonlinetrainers.com
- Phone USA (1) 6783896789
- UK (44)- 2032393637
- India (91) 9246449191
- Ask for a free demo TODAY http//www.bestonline
trainers.com/demo/index.html