Tuesday, 1 March 2016

Question on Final keyWord

By With No comments :
What will be the output of these question and answer them by comment i'll be sharing the correct answers later.

These question are frequently asked by interviewer to check your basic java concepts regarding final 

Answering them correctly will ensure you that concept of final in java is cleared to you. Please do comment the answers thanks.

1. final class ParentClass{


   protected int parentId;
  public ParentClass(){
   parentId=1000;
   }
 }
 class ChildClass extends ParentClass{
   private int childId;
   public ChildClass(){
   childId=2000;
   }
   public void display(){
   System.out.println("Parent Id:"+parentId+" Child Id:"+childId);
   }
 }
 class Demo{
   public static void main(String args[]){
   ChildClass childOne = new ChildClass();
   childOne.display();
   }
 }

2. class ClassExample{
   protected int classId;
   private final int MAX=10;
   private static int counter;
  public ClassExample(){
   classId=counter++;
   if(counter>MAX){
  MAX=20;
  counter=0;
  }
  System.out.println("MAX:"+MAX+"counter :"+counter);
   }
 }
 class Demo{
   public static void main(String args[]){
   ClassExample exampleOne = new ClassExample();
   }
 }

3 .class ClassExample{
   protected int classId;
   private final int MAX=10;
   private static int counter;
  public ClassExample(){
   classId=counter++;
   if(counter>MAX){
  counter=0;
  }
  System.out.println("MAX:"+MAX+" counter :"+counter);
   }
 }
 class Demo{
   public static void main(String args[]){
   ClassExample exampleOne = new ClassExample();
   }
 }

4.class ClassExample{
   protected int classId;
   private final int MAX=10;
   private static int counter;
  public ClassExample(){
   classId=counter++;
   if(counter>MAX){
  counter=0;
  }
   }
   public final void display(){
  System.out.println("MAX:"+MAX+" counter :"+counter+ " classId:"+classId);
  }
 }
 class DerivedExample extends ClassExample{
   private int derivedId;
   public DerivedExample(){
   derivedId=10;
   }
   public void display(){
   super.display();
   System.out.println("derivedId:"+derivedId);
   }
 }
 class Demo{
   public static void main(String args[]){
   DerivedExample exampleOne = new DerivedExample();
   exampleOne.display();
   }
 }




Question of Abstract keyword

By With No comments :

What will be the output of these question and answer them by comment i'll be sharing the correct answers later.

These question are frequently asked by interviewer to check your basic java concepts regarding abstract keyword

Answering them correctly will ensure you that concept of abstract keyword in java is cleared to you. Please do comment the answers thanks.

1 . abstract class ParentClass{
   protected int parentId;
   public ParentClass(){
   parentId=1000;
   }
   abstract public void display();
 }
 class ChildClass extends ParentClass{
   private int childId;
   public ChildClass(){
   childId=2000;
   }
 }
 class Demo{
   public static void main(String args[]){
   ChildClass childOne = new ChildClass();
   }
 }

2 . abstract class ParentClass{
   protected int parentId;
   abstract public ParentClass();
 }
 class ChildClass extends ParentClass{
   private int childId;
   public ChildClass(){
   childId=2000;
   }
 }
 class Demo{
   public static void main(String args[]){
   ChildClass childOne = new ChildClass();
   }
 }

3.abstract class ParentClass{
   protected int parentId;
   public ParentClass(){
    parentId=1000;
   }
   abstract private void display();
  }
 class ChildClass extends ParentClass{
   private int childId;
   public ChildClass(){
   childId=2000;
   }
   public void display(){
    System.out.println("Parent Id:"+parentId+" Child Id:"+childId);
   }
 }
 class Demo{
   public static void main(String args[]){
    ChildClass childOne = new ChildClass();
    childOne.display();
   }
 }

4.abstract class ParentClass{
  protected int parentId;
  public ParentClass(){
    parentId=1000;
  }
  abstract public void display();
 }
 class ChildClass extends ParentClass{
   private int childId;
   public ChildClass(){
   childId=2000;
   }
   public void display(){
    System.out.println("Parent Id:"+parentId+" Child Id:"+childId);
   }
 }
 class Demo{
   public static void main(String args[]){
   ChildClass childOne = new ChildClass();
   childOne.display();
   }
 }

5.abstract class Example{
  public void disp(){
   System.out.println("disp in Example");
   }
  public abstract void display();
 }
 class Example1 extends Example{
   public void display(){
    System.out.println("display in Example1");
  }
 }
 class Demo{
  public static void main(String args[]){
  Example obj=new Example1();
  obj.display();
 }
}

6.abstract class Example{ 
   public void disp(){
     System.out.println("disp in Example");
   }
   public abstract void display();
 }
 class Example1 extends Example{
    private void display(){
     System.out.println("display in Example1");
    }
 }
 class Demo{
   public static void main(String args[]){
   Example obj=new Example1();
   obj.display();
 }
}

7. abstract class Example{
   void disp(){
    System.out.println("disp in Example");
  }
  public abstract void display(){}
 }
abstract class Example1 extends Example{
   public void display1(){
    System.out.println("display in Example1");
   }
 }
 class Example2 extends Example1{
   public void display(){
      System.out.println("display in Example2");
    }
 }
  class Demo{
    public static void main(String args[]){
         Example2 obj=new Example2();
        obj.display();
    }
  }

Saturday, 27 February 2016

Question Trend

By With No comments :
Some the interview questions i have faced during my career
How we can put list inside another list
Answer: Make a ArrayList  which contains ArrayList.
How to create it? Its simple guys

List al = new ArrayList<ArrayList<String>>();
and now  you can add List of String inside ArrayList which is putting List inside List 

Example:Suppose i have a List having [1,2,3,4...20] and have to add first five element in first position of list then another five on the second position and so on.For this i have to make a List of List.

public class ListInsideList {
 public static void main(String[] args) {
// This list be have to insert into List with first fifth element at first index of other list and so on...
List<List<Integer>> newList = new ArrayList<List<Integer>>();
List<Integer> tempList = new ArrayList<Integer>();
List<Integer> originalList = new ArrayList<Integer>(); 
for (int i = 1; i <= 20; i++) {
originalList.add(i);
}
for(Integer value : originalList){
  tempList.add(value);
  if(tempList.size()==5)
  {
newList.add(tempList);
  tempList = new ArrayList<Integer>();
  }
}
System.out.println("Final"+newList);
  }
}
Output is: Final[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]

Wednesday, 24 February 2016

List Interface

By With No comments :













Whenever we want a group individual of Object which can have duplicates and insertion order is preserved then we use List
Following are the characteristics of List
  • Child interface of Collection interface
  • Duplicates are allowed 
  • Insertion Order is preserved 
Method in List Interface
List interface is mostly used by index and index play most important role in the List interface
As it is the child of Collection interface it has all the method of Collection interface and list specific method the are:
  • public void add(int i ,Object o);
    • Add Object o at index i
  • public boolean addAll(inti,Collection c);
    • Add group of object at index i
  • public Object get(int i)
    • return the object at  index i 
  • public Object remove(int i)
    • Element at specified index is removed
  • public int indexOf(Object o);
    • Return first index of this Object
  • public int lastIndexOf(Object o);
    • Return last index of the Object o
  • public ListIterator listIterator()
    • To access element of list one by one
  • public Object set(int i,Object o);
    • To update Object at specified index i
As we can see every method is related to index so plays an important role.
Implementation classes of List
  • ArrayList(1.2 v)
  • Linked List (1.2v)
  • Vector(1.0v)
  • Stack(1.0v)
Note Vector and Stack come in 1.0 v and also called as legacy classes 
ArrayList
  • Underlying data structure is growable array
  • Duplicates are not allowed
  • Insertion order is preserved
  • Heterogeneous object are allowed
  • Null insertion is allowed 
  • Implements Serializable and Cloneable interface
  • Implements RandomAccess interface
    • It is a Marker interface means do not have any methods
    • It is used to access element randomly within same time
Constructor 
  • ArrayList al = new ArrayList();
    • Create emptyt ArrayList and default initial capacity 10
    • If the ArrayList reaches its max capacity then new ArrayList capacity is
      • (current capacity *3/2)+1
  • ArrayList al = new ArrayList(int initalCapacity);
    • Create ArrayList with given initial capacity
    • This is useful when you know that total elements are to be inserted
  • ArrayList al = new ArrayList(Collection c)
    • This is used to make ArrayList from any constructor
Example:
import java.util.*;
class ArrayListDemo{
  public static void main(String...s){
  ArrayList al = new ArrayList();
  al.add("A");
  al.add("C");
  al.add("Z");
  al.add(null);
  System.out.println(al);//[A,C,Z,null]
  al.remove(2);
  System.out.println(al);//[A,Z,null]
  al.add(2,"M");
  al.add("N");
  System.out.println(al);//[A,Z,M,null,N]
  }
}
Question: Why theoutput is in square brackets as we know whenever we print aur reference than toString() method of Object class is called and every Collection class has overriden this method 
public toString(Object o){
return "["+o+"]";// inside the square brackets
}

When is the Best and worst choice of ArrayList
Best choice :
Whenever our frequent operation is retrival operation then ArrayList is Best choice  beacause it implements RandomAccess interface. 
Suppose there are 10000 elements in the ArrayList and when you perform 
get(1000) it take 1 sec
get(10) it take 1 sec

Worst choice :
Whenever our frequent operation is updation and deletion in the middle then its is a Worst choice
Suppose we have 1 crore elements in ArrayList and we want to add element in first index then to place the element there is a need to shift places and every element is shifting places so there are several shift operation to add and same for remove operation

Difference Between ArrayList And Vector class

Differences
ArrayList
Vector
1
Every method is not synchronized
Every method is synchronized
2
Performance is high
Performance is slow

3
Not thread safe Object
Thread safe object
4
Not legacy class
Legacy class
5
It come in 1.2 version
It came in 1.0 version

How to get synchronized version of ArrayList
Collections is the utility class that provide method to make our collection objects synchronized or thread safe :
ArrayList al = new ArrayList();
Collections.synchronizedList(al);

Complete signature is
 public static List synchronizedList(List l);
Similarly we can have method to make Set and Map synchronized and they are: 
 public static Set synchronizedSet(Set set);
 public static Map synchronizedMap(Map map);

Linked List class
Some features of Linked list

  • Underlying data structure
  • Duplicates are allowed
  • Insertion order is preserved
  • Heterogeneous object are allowed
  • Null insertion is also possible
  • Serializable and cloneable intgerface are implemented
  • Random access is not implemented
Best choice:  If insertion and deletion is our frequent operation is insertion and deletion in the middle then LinkedList is the best choice
as the element are not stored in  memory location and stored in form of nodes which contains address of the previous element and next element and thus easy to remove element by just removing the node and by adding the node element can be easily added
Methods 
All the method of Collection method and there are some specific method of LinkedList

  • public void addFirst(Object o);
  • public void addLast(Object o);
  • public Object getFirst(Object o);
  • public Object getFirst(Object o);
  • public Object removeFirst(Object o);
  • public Object removeLast(Object o);
Constructor
There are two constructor in LinkedList class
  • LinkeList ll = new LinkedList();
    • It create empty LinkedList with the default capacity 10
  • LinkeList ll = new LinkedList(Collection c);
Example:
import java.util.*;
class LinkedListDemo{
  public static void main(String...s){
  LinkedList l = new LinkedList();
  l.add("A");
  l.add("C");
  l.add("Z");
  l.add(null);
  System.out.println(l);//[A,C,Z,null]
  l.set(0,"X");
  System.out.println(l);//[X,A,Z,null]
  l.removeLast();
  l.addFirst("NNN");
  System.out.println(l);//[NNN,A,Z,M]
  }
}

Difference between ArrayList and LinkedList


Differences
ArrayList
LinkedList
1
Best choice when our frequent operation is retrival
Best choice when our frequent operation is insertion and deletion in th middle
2
Worst choice when our frequent operation is insertion and deletion in th middle
Worst choice when our frequent operation is retrival
3
Underlying data structure growable Array
Underlying data structure id doublly linked list
4
Implements RandomAccess interface
Do not Implements RandomAccess interface