Interview Question
Core Java
Basics
- What do you understand by JVM.
- Relate JDK and JRE and its uses.
- How to generate class file from java file
javac
Test.java
- If i create java file in windows and compiles in windows and run in linux .What I have to do to make it compatible.
- What are different ways of instantiating a class.
MyObject
object =
new
MyObject();
MyObject
object =
(MyObject)
Class.forName("subin.rnd.MyObject").newInstance();
MyObject
anotherObject =
new
MyObject();
MyObject
object =
anotherObject.clone();
ObjectInputStream
inStream =
new
ObjectInputStream(anInputStream
);
MyObject
object =
(MyObject)
inStream.readObject();
- Describe public, private and protected modifiers.
- Say I have Test class with main method.
Public class
Tests {
public static void main(String[] args) {
testMethod();
}
private static void testMethod() {
testMethod();
}
}
Stack
overflow error
- Interface vs abstract class
- Method overloading and overriding, which polymorphism works at runtime and compile time, give examples.
- Autoboxing and unboxing.
Exception
handling
- What is hierarchy
- Try, catch and finally. Use of catch, finally
- What to do if I do not want to execute finally
- If I want to write my own exception class
- If I write return statement in try. Below question. Output..?? ab
public
class HelloWorld{
public
static void main(String []args){
String
s= "";
s =
getMsg();
System.out.println(s);
}
private
static String getMsg(){
String
a = "a";
try{
a=a+"b";
return a;
}catch(Exception e){
}finally{
a=a+"c";
System.out.println(a);
}
return
"fi";
}
}
Generic
- What are generics and when and why were they introduced.
- When does generics come into play, compile time or runtime or both.
- What happens to generic code when you compile a class.
- Design a generic class which can except any type employee id.
Log4J
- Levels and there logging hierarchy.
- What is serialization and why do we need serialization.
- How can I achieve serialization
- What are the methods present in serializable interface.
- What is the use of serial version id.
- What I will do if I do not want to serialize a particular attribute of the object.
- What is externalization.
Collections
- What are different types of collections used.
- Diff between hashmap and hashtable.
- What is concurrent hashmap
- How hashing and equals method works
- Hashset internal working – don’t add equals and hash method , then output
- Treeset add objects of simple class – exception occurs
- Comparator and comparable.
- Describe about collection and collections.
- Iterator and list Iterator
- Concurrent modification exception example.
- FailFastVSFailSafe
Threads
- How to implement threads
- Diff between booth implementation
- Static vs instance method calling.
//Creating
new instance for every thread access.
ExtendsThread
tc1 = new
ExtendsThread();
//Multiple
threads share the same object.
ImplementsRunnable
rc = new
ImplementsRunnable();
- Idea about fork joins
Java Design
pattern - singleton
JSP
- JSP lifecycle
JSP Page
Translation:
jSP Page
Compilation:
Class
Loading:
Execution
phase:
Initialization:
jspService()
execution:
jspDestroy()
execution
- What if I write destroy method in jsp, will page destroy or show
- Dynamic vs static include
The
syntax for static include is <%@ include file=”filename.jsp”
%> and the syntax for dynamic include is <jsp:include
page=”filename.jsp” />
- Difference between Scriptlet and Declaration
Declaration
:- Used
for declaring variables and methods.
example :
<%! int num =0; %>
During
translation and compilation phase of JSP life cycle all variables
declared in jsp declaration become instance variables of servlet
class and all methods become instance methods. Since instance
variables are automatically initialized,all variables declared in jsp
declaration section gets their default values.
Scriptlet:- Used
for embedding java code fragments in JSP page.
example :
<% num++; %>
During
translation phase of JSP Life cycle all scriptlet become part of
_jspService() method. So we
cannot declare methods in scriptlet since
we cannot have methods inside other methods. As the variables
declared inside scriptlet will get translated to local variables they
must be initialized before use.
- Load V/S Get –
The
get() method will return a FULL initialized object if nothing is on
the session cache, that means several DB hits depending on your
mappings.
While the load() method will return a proxy (or the instance if already initialized), allowing lazy initialization and thus better performance
While the load() method will return a proxy (or the instance if already initialized), allowing lazy initialization and thus better performance
If
load() can’t find the object in the cache or database, an exception
is
thrown. The load() method never returns null. The get() method returns
null if the object can’t be found.
thrown. The load() method never returns null. The get() method returns
null if the object can’t be found.
- Cache implementation
- Composite key example
- Mappings in hibernate
- Xml based or annotation based
- hibernate_inheritance_strategy
- list vs set
- referred and owned collections
- use of cascade select and orphan removal
- hibernate API’s, query, criteria and hql.
- Session factory and session
- transaction managers
- configuring datasource
Spring
- Explain Spring DI and IOC
- Life cycle of spring bean.
- What happens if I have two beans of same name and using in service through injection
- Scope of beans
- Application context vs bean factory
- Spring aop
- Spring transactional management and programmatic management
- beanPostProcesssor
- singletone with dependency of prototype bean
- spring singleton vs java singleton
- @Autowired
- Spring MVC
- Init binders
- Validators
AJAX
- Make ajax request
- Get html in response.
- Async example
What is
ReadWrite Lock? Does ConcurrentHashMap uses ReadWrite Lock?
ReadWrite Lock is an implementation of lock stripping, where two separate locks are used for read and write operation. Since read operation doesn't modify state of object, it's safe to allow multiple access of shared object to multiple reader without locking, and by splitting lock into ReadLock and WriteLock, you can easily do that. Java provides an implementation of ReadWriteLock in form of ReentrantReadWritLock, which is worth looking. Also ConcurrentHashMap doesn't use ReadWriteLock, instead it divides maps into several segments and lock them separately using different locks, which means any given time, only a portion of map is locked, instead of whole map. This question is also very popular on Senior and experienced level Java interviews, expect Interviewer to go into more detail, e.g. asking you to provided an implementation of ReadWriteLock with different policies.
ReadWrite Lock is an implementation of lock stripping, where two separate locks are used for read and write operation. Since read operation doesn't modify state of object, it's safe to allow multiple access of shared object to multiple reader without locking, and by splitting lock into ReadLock and WriteLock, you can easily do that. Java provides an implementation of ReadWriteLock in form of ReentrantReadWritLock, which is worth looking. Also ConcurrentHashMap doesn't use ReadWriteLock, instead it divides maps into several segments and lock them separately using different locks, which means any given time, only a portion of map is locked, instead of whole map. This question is also very popular on Senior and experienced level Java interviews, expect Interviewer to go into more detail, e.g. asking you to provided an implementation of ReadWriteLock with different policies.
- How to make an Object Immutable in Java? Why should you make an Object Immutable?
Well, Immutability offers several advantage including thread-safety, ability to cache and result in more readable multithreading code. See here to learn how to make object Immutable. Once again, this question can also go into more detail and depending upon your answer, can bring several other questions e.g. when you mention Spring is Immutable, be ready with some reasons on Why String is Immutable in Java.
11) Which design patterns have you used?
Always expect design and patterns related question for Senior developer Core Java Interview. It's best to mention any GOF design pattern rather than Singleton or MVC, which almost every other Java developer use it. Your best bet can be Decorator pattern or may be Dependency Injection Pattern, which is quite popular in Spring Framework. It's also good to mention only design pattern, which you have really used in your project and knows it's tradeoffs. As once you mention a particular design pattern say Factory, Interviewer's next question would be, have you used in your project? So be ready with proper example and why you choose a particular pattern.
14)
How do you prevent SQL Injection in Java Code?
This question is more asked to Java EE developers than core Java developers but still a good question to know, PreparedStatement is the way to go. PreparedStatement not only provides better performance but also shield from SQL Injection attack. If you are working more on Java EE or J2EE side, than you should also be familiar with other security issues including Session Fixation attack or Cross Site Scripting attack and how to resolve them.
This question is more asked to Java EE developers than core Java developers but still a good question to know, PreparedStatement is the way to go. PreparedStatement not only provides better performance but also shield from SQL Injection attack. If you are working more on Java EE or J2EE side, than you should also be familiar with other security issues including Session Fixation attack or Cross Site Scripting attack and how to resolve them.
Why Java doesn't support multiple inheritance
1) First reason is ambiguity around Diamond problem, consider a class A has foo() method and then B and C derived from A and has there own foo() implementation and now class D derive from B and C using multiple inheritance and if we refer just foo() compiler will not be able to decide which foo() it should invoke. This is also called Diamond problem because structure on this inheritance scenario is similar to 4 edge diamond, see belowA foo() / \ / \ foo() B C foo() \ / \ / D foo()
In my opinion even if we remove the top head of diamond class A and allow multiple inheritances we will see this problem of ambiguity.
Read more: http://javarevisited.blogspot.com/2011/07/why-multiple-inheritances-are-not.html#ixzz2xVge91NL
How do you detect deadlock in Java
?
though this could have many answers , my version is first I would look the code if I see nested synchronized block or calling one synchronized method from other or trying to get lock on different object then there is good chance of deadlock if developer is not very careful.
other way is to find it when you actually get locked while running the application , try to take thread dump , in Linux you can do this by command "kill -3" , this will print status of all the thread in application log file and you can see which thread is locked on which object.
other way is to use jconsole , jconsole will show you exactly which threads are get locked and on which object.
though this could have many answers , my version is first I would look the code if I see nested synchronized block or calling one synchronized method from other or trying to get lock on different object then there is good chance of deadlock if developer is not very careful.
other way is to find it when you actually get locked while running the application , try to take thread dump , in Linux you can do this by command "kill -3" , this will print status of all the thread in application log file and you can see which thread is locked on which object.
other way is to use jconsole , jconsole will show you exactly which threads are get locked and on which object.
Why JDBC has
all interfaces and no implementation classes
Securing URL
parameters.
Sorting
algorithms
No comments:
Post a Comment