Monday, November 23, 2015

Frequently asked Core Java Interview questions with answers

In this post i am Sharing Questions those frequently asking java technical interview questions by Interviewer. So we try our level best to provide you the Core java interview questions for freshers. You should brush up the fallowing technical questions to gain confident level to clear technical round.





1. Can you Override Private or Static method in Java?

This is popular java question. By using Method Overriding topic Interviewer may rise many questions regarding it.Anyway, you can not override private or static method in java.
If you create similar method with same return type and same method arguments called method hiding.

2. What is difference when String is gets created using literal or new() operator?
    
When we create String with new() its created in heap and not added into String Constant       Pool While String created using literal are created in String Constant Pool itself.

3.Can you access non-static variable in static context?
    
No you can not access static variable in non-static context in java.

4. How do you ensure that N thread can access N resources without DeadLock?
    
 If you are not well versed with multi-thread code then this is tricky question for you.This question is really tricky for even experienced programmers.Key point is Order,if you acquire resources in particular order and release resources in reverse order you can prevent deadlock while writing coding itself.      

5. If a method throws NullPointerException in Super class, can we override it with a method with throws RuntimeException?
     
This popular java question from overloading and overriding concept.Answer is you can     very well  throw super class of RuntimeException  in overridden method but you can not      do same if it is Checked Exception.

6. What is Immutable Object? Can you write Immutable Object?
     
Immutable classes are java classes whose objects can not modified once created.Any               modification in immutable object result in new object.For example a String is Immutable in java. final also immutable in java.In order to prevent   sub class   from overriding              methods in java which can compromise immutability.You can achieve same functionality by making member as non final but private not modifying them except in constructor. 


7.What is difference StringBuffer and StringBuilder in java?
    
This is very popular interview question.StringBuilder in java is introduced in Java 5  and       only difference between both of them is  that StringBuffer methods are synchronized              while StringBuilder methods are non-synchronized.
     
8) What is RACE condition in java? How to avoid race condition?

Ans: RACE condition is a situation in which two or more threads or process are reading or writing some shared data,and the final result depends on the timing of how the threads are scheduled. RACE conditions can lead to unpredictable results and subtle program bugs. A Thread can prevent this from happening by locking an object. When an object is locked by one thread and another thread tries to call a synchronized method on the same object, the second thread will block until the object is unlocked.

9) What is Singleton class?

Ans:  The main purpose is to control object creation,limiting the number of objects to one only. Since there is only one Singleton instance,any instance fields of a Singleton will occur only once per class,just like static field. Singleton often control access to resource such as database connections or sockets.

For more details: Singleton class with example 

 10) What is String Pool?

Ans:  String pool is a special storage area in java heap. When a string is created and if the string already exists in the pool,the reference of the existing string will be returned,instead of creating a new object and returning its reference.

11) Can we delete an object in java? If yes then how?

Ans: NO, since all objects are created in the heap, the JVM has a garbage collector that relies on reference counts. Once there are no more references to an object,it becomes available for collection by the garbage collector.

12) What is Semaphore? How to achieve it?

Ans: A semaphore is a thread synchronization construct that can be used either to send signals between threads to avoid missed signlas, or to guard a critical section like you would with a lock. It has introduced in java 5 in the implementation java.util.concurrent package. So, you do not have to implement  your own semaphores.
  
  

No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...