top 10 core java interview questions

Que 1:- What is OOPs concept?

Ans:- Follow the given link for brief description Click Here.

Que 2:- Why we should override equals() and hashcode() method and tell me methods of Object class?

Ans:- Follow the given link for brief description Click Here
        Every object contains 11 common operations - list is given below
  • Comparing two objects
    • public boolean equals(Object obj)
  • Retrieving hashcode i.e; object identitiy
    • public native int hashCode()
  • Retrieving object information in String format for printing purpose
    • public String toString()
  • Retrieving the runtime class object reference
    • public final native Class getClass()
  • Cloning object
    • protected native Object clone() throws CloneNotSupportedException
  • Executing object clean-up code/ resource releasing code just before object is being destroyed
    • protected void finalize() throws Throwable
  • Releasing object lock and sending thread to waiting state
    • public final void wait() throws InterceptedException
    • public final native void wait(long mills) throws InterrruptedException
    • public final void wait(long mills, int nanos) throws InterruptedException 
  • Notify about object lock availability to waiting threads
    • public final native void notify()
    • public final native void notifyAll()

Que 3:- What is Exception Handling and how to write custom exception ?

Ans:- Exception is a run-time error cause due to logical mistake or wrong input given by user.. when exception is raised program execution terminated abnormally, abnormally means the statement placed after exception causing statement will not be excuted.
Normal termination means all statement till end of the method executed.

Directly subclass of exception is called as Checked exception it must be throws(report).
Error and runtime exception are unchecked exception it is optional to report.
Throwable and Exception are partial checked exception because they contain Unchecked Exception.

Custom Exception:- Creating our own exception is called as custom exception or user defined exception. To create user defined exception your class should extend Exception class 

Que 4:- What is the difference between final finally and finalize ?

Ans:- Final is used to apply restrictions on class, method and variable. Final class can't be inherited, final method can't be overridden and final variable value can't be changed.
Final is keyword.

Finally is used to place important code, it will be executed whether exception is handled or not.

Finalize is used to perform clean up processing just before object is garbage collected.

Que 5:- What is the difference between Iterator and Listinterator ?

Ans:- The main difference is:

Diff 1: 
  • Iterator is unidirectional - means elements are retrieved only in forward directions.
  • Listiterator is bidirectional- means elements are retrieved in both directions.
Diff 2:
  • Iterator is a super interface
  • Listiterator is a sub interface of iterator
Diff 3:
  • Iterator can be applied on both Set and List implemented classes
  • ListIterator can only be applied on List implemented classes. because it works with index
Diff 4:
  • Iterator can only allow us to retrieve and remove elements
  • Listiterator can allow us to retrieve, remove, insert and also replace elements.

Que 6:- What is the difference between Vector and HashTable ?

Que 7:- What  string is immutable and what is the difference between Sting, StringBuffer and StringBuilder ?

Ans:- String is immutable because of String pooling concept. String pooling means grouping string objects.

if we create String objects by assigning same string literal, only one object is created and all reference variable are initialized with that same String object reference. Then all referenced variable are pointing to same String object. This is reason why String object become immutable. it means string data modification is not stored in same memory.

Since a string object is pointing by multiple referenced variable if w change that String value using one reference variable, all other referenced variable those are pointing to that objects are also affected. To solve this problem string is introduced as immutable.

This String pooling concept is not applicable for the string objects those are create using "new keyword and Constructor"

Diff #1: 

  • String is an immutable sequence of character.
  • StringBuffer is a thread safe mutable sequence of character.
  • StringBuilder is also mutable sequence of character but not thread safe.
  • The only difference between StringBuffer and StringBuilder is :
    • StringBuffer object is thread-safe. it means its object is not modified by multiple threads concurrently, because all its method are declared as "synchronized".
Diff #2:
  • String object is created in two ways
    • By assigning string literal ==> String s1="abc";
    • By using it available constructors. 
  • StringBuffer object are crating only in one way
    • By using its available constructor..
Diff #3:
  • String does not have default capacity, the passed string length is it's capacity.
  • String Buffer has default capacity 16 buffers. it increases it's capacity automatically when sized reached its capacity.
Diff #4:
  • Using String object, we can concat new string to the current string in two ways.
    • using + operator
    • using concat() method
  • Using StringBuffer we can perform concat operation
    • using append() method
Que 8: What are the differences between "==" operator and equals() method ?

== operator
  • it always compares objects with references.
  • We can't compare incompatible objects compiler throws CE
  • We can use it also for comparing primitive values and two null directly.
equals() method
  • It compares objects either with reference or with state based on its implementation.
  • In object class it compare object with refence
  • In subclass it compare objects with state.
  • We can compare incompatible objects using eqals() it returns "false".
  • We can't use it for comparing primitive values and two nulls directly it leads to Compiler Error.
Que 9: What is Synchronization and what is the difference between synchronized methods and blocks ?

Ans:-  The process of allowing multiple threads to modifying an object in sequence is called synchronization. In java Synchronization is implemented by using synchronized keyword.

Below program shows defining synchronized mehtod and block.

public class Bank {
private double balance;
public synchronized void withdraw(int amt) {     //Synchronized method
System.out.println("Balance before withdraw"+balance);
System.out.println("balance after withdraw"+balance);

public class Bank {
private double balance;
private void withdraw(int amt) {
System.out.println("Balance before withdraw"+balance);
sychronized(this) {      //Synchronized block
System.out.println("balance after withdraw"+balance);

Que 10: Difference between HashMap and HashSet ?

Ans:- HashMap is collection of key-value pairs whereas HashSet is un-ordered collection of unique elements.

Next - Java Programming Interveiw Questions And Answer For Fresher
© 2014 JavaTechWorld. Designed by Bloggertheme9
Powered by Blogger.