Sunday 7 April 2013

9 Common Java Interview Questions:


 

What is dependency injection? 

Dependency Injection is a technique used to change the implementation of a method at compile or run-time. Frameworks such as the Spring framework allows dependency injection by specifying the implementation you want to use via a bean in a configuration file.

How does Java implement garbage collection?

A bit pedantic, but Java is just a specification, so it does not technically implement garbage collection. Garbage collection is implemented into a JVM. Reference counting and mark and sweep are two of the more well known techniques for garbage collection. Reference counting involves. This keeps a count of the number of references to a piece of memory allocated in the heap.
When that number is reduced to 0 you know you can safely perform garbage collection on that memory and free it up. The downside of this is that if you have a cyclic data structure in memory (imagine a linked list where the last node points back to the head) then you will always have at least 1 reference and it may never get collected. Mark and sweep is a two phase technique:

Step one Trace through through the graph which is composed of the all of the objects which are referenced and mark each node as visited. Step two Sweep through all of the objects in memory and remove the ones which are not marked.

What is ORM?

ORM stands for object relationship mapping. This is basically how you take an object that exists in memory, and map it on to a persistent data store e.g database.

 What are the advantages of using hibernate?

Hibernate is an ORM technology that can help you abstract over the database layer you write java classes which describe your data model, and by configuring hibernate settings it can generate code to persist the data into various different databases like Oracle or PostgreSQL, without you having to write any more code.

This also gives you some flexibility in that you can change your database without having to write any more code, you just update your Hibernate configurations.

 What is an outer join? 

Outer Join means if you have a "master" table and you join rows from another table on some join criteria, the rows from the master table will be in the result set but the columns from the other table will be blank. For example, say you have if you have an accounts table with a name and id column, and you have a payments table with an account id and amount column, then if you select amount from accounts left outer joining payments on account id, then your results will have a row for every account, even if that account has no payments in the payment table.

What is test driven development?

Test driven development basically means whenever you want to add a new feature or test a bug, the first step is to write a failing unit test which reproduces the bug or demonstrates some part of the feature is not working, then you write code until that test works.

How can test driven development help development of application?

Using this approach helps you write code that is more testable (for obvious reasons) and can help you come up with designs that are more modular because the test functions as a kind of interface.

When would you use stringBuffer vs just string vs stringBuilder? 

String is a immutable type in Java, which basically means once a string gets created and allocated on the heap, it is there in its final form - if you try to concatenate another character to the end of it, you actually end up creating an entirely new string in memory, and the old one still sits around until it is garbage collected.

Therefore, String is best used when you are working with static data that does not change.  StringBuilder is a more memory efficient class to use when performing lots of string operations like appending, splitting, etc. StringBuffer Is used when. their are other processes which will communicate data to be operated on.

What is deadlock?

Deadlock is when a set of concurrent processes are unable to obtain the resources needed in order to perform some set of work and are all stuck.

Think of the dining philosophers problem where you have 5 people who want to eat, and only 5 chopsticks - if each philosopher picks up exactly one chopstick, none will be able to eat because they need another chopstick but they are all held by the other philosophers. Care needs to be taken in designing protocols which avoid deadlock - for example, a philosopher should should put down a chopstick if he is not able to acquire both in some period of time.

There is a related concept of "live lock" where for whatever reason, only certain threads are able to acquire the resource while others are not. For example, maybe you have a producer and consumer reading from a queue, and your implementation is such that if someone is writing to the queue you cannot read. If the producer is constantly writing to the queue and the consumer cannot read from, then you have a "live lock" condition because even though your producer thread is able to acquire the resource, the consumer thread is not.

No comments:

Post a Comment