Top 10 Core Java Interview Questions

Top 10 Core Java Interview Question

Que 1:- What is Oops concept?

 Ans:- Follow the given link for brief description on Oops Concepts.
 

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 identity
    • 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 executed.
Normal termination means all statement till end of the method executed.

top 10 core java interview questions
Exception Hierarchy
Directly subclass of exception is called as Checked exception it must be throws(report).

Error and run-time 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 Lisiterator ?

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 ArrayList and Vector ?

Ans:- Difference between ArrayList and Vector is given below-

    1. First and most common difference between Vector vs ArrayList is that Vector is synchronized and thread-safe while ArrayList is neither Synchronized nor thread-safe. Now, What does that mean? It means if multiple thread try to access Vector same time they can do that without compromising Vector's internal state. Same is not true in case of ArrayList as methods like add(), remove() or get() is not synchronized.
  • Second major difference on Vector vs ArrayList is Speed, which is directly related to previous difference. Since Vector is synchronized, its slow and ArrayList is not synchronized its faster than Vector.
  • Third difference on Vector vs ArrayList is that Vector is a legacy class and initially it was not part of Java Collection Framework. From Java 1.4 Vector was retrofitted to implement List interface and become part of Collection Framework.

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 ?

 Ans:-
== 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 reference
  • 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 method and block.
public class Bank {

        private double balance;

        public synchronized void withdraw(int amt) {     //Synchronized method

        System.out.println("Balance before withdraw"+balance);

        balance=balance-amt;

        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

       balance=balance-amt;

   }

     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.
 

Leave a Comment

%d bloggers like this: