Sunday, November 8, 2015

Core Java interview questions and answers for Freshers

In this article, we will learn some of the frequently asked core java interview questions for freshers. Before attending technical round should brush up these questions and answers to get clear your technical round effectively and confidently. 

1) What are difference between ArrayList and Vector?

Ans: 


2) What is Immutable? how you can achieve it?

Ans: Immutable classes are those class whose object can not modified once created,it means any modification on immutable object will result in another immutable object. The example for immutable  and mutable objects are String and StringBuffer. Since String is immutable class, any change on existing string object will result in another string. While in case of mutable object like StringBuffer any modification is done on object itself and no new objects are created. To create a class Immutable,you need to follow the below steps:


  1. Declare the class as final so it can't be extended
  2. Make all fields private so that direct access is not allowed
  3. Don't provide setter methods for variables
  4. Initialize all the fields via a constructor performing deep copy
  5. Perform cloning of objects in the getter methods to return a copy rather than returning the actual object reference.
3) Difference Between HashMap and HashSet?

Ans:

4) Why java doesn't support multiple inheritance?

Ans:  There are few reasons why Java Doesn't support multiple inheritance. They are
  1. First reason is ambiguity problem, that means consider a class A has foo() method and class 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.
  2. Second and more convincing reason is that multiple inheritances does complicate the design and create problem during casting,constructor chaining etc..and given that there are not many scenario on which you need multiple inheritance its wise decision to omit it for the sake of simplicity.

5)  What is Initializer block?

Ans: Instance initializer block is used to initialize the instance data memeber. It run each time when object of the class is created.
6)  What is difference between Exception and Error?
Ans: The main difference on Exception Vs Error is that Exceptions are recoverable and most of the time exception are raised due to program code only whereas errors are non-recoverable most of the cases errors due to lack of system resources but not due to our programs.
7) What are difference between LinkedHashMap and HashMap?
Ans: 
  • The first difference is LinkedHashMap will keep the order in which the elements are inserted into it Whereas  HashMap doesn't maintain any order.
  • LinkedHashMap also require more memory than HashMap because of this ordering feature.
  • LinkedHashMap actually extends HashMap and implements Map interface
 8) What is encapsulation?

Ans: It is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. We use setter and getter methods to access objects. In that way data is hidden inside object and is only accessible through object.

9) When to use abstract class and when interface?

Ans: 
  • An abstract class is good if you think you will plan on using inheritance since it provide a common base class implementation to derived classes
  • An abstract class is also good if you want able to declare non-public members whereas in interface all methods must be public
  • if you think you will need to add methods in future then an abstract class is better choice.
  • Interfaces are a good choice when you think that the API will not change for a while
  • Interfaces are also good when you want to have something similar to multiple inheritance since you can implement multiple interfaces.
10) How Java Throw Exceptions?

Ans:  All methods use throw statement to throw an exception. The throw statement requires a single argument. Throw objects are instances of any subclass of the Throwable class.

11)If a method throws NullPointerException in Super class, can we override it with a method with throws RuntimeException?
     

Ans: 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.

12) 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.

13)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.

14)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.

15)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.


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...