Monday, December 3, 2012

Java Interview Questions -1


System.gc() & Runtime.gc()
=============================
System.gc() internally calls Runtime.gc() or Runtiem.getRuntime().gc();
System.gc() is class method. Runtime.gc() is instance method.

public class Test {

protected void finalize(){

System.out.println("Finalize");

}

int i;
void intaliI(int i)
{ this.i=i; }

int retValueI()
  { return i; }

}


public  class SystemRuntimeGC {

public static void main(String args[]){

System.out.println("Starting");
System.gc();
System.out.println("Ending");

Test t1=new Test();
t1=null;
System.gc();
}
}

Garbage collection is happening via garbage collector.
OutOfMemoryError  Unchecked

Synchronization Block vs method
===============================
Method:
public synchronized void testFun()
{
statements;

}

Block
public void testFun(){

statements;
synchronized(this){
statements...;
}
}

Vector vs ArrayList
==================
Vector is synchronized & arraylist is not.
Vector double the size and arraylist is 50%.
Adding , removing at O(1)
Removing at particular position O(n-i) i is index

Abstract methods & Non Abstract Methods
=====================
Abstract methods can be called via non abstract methods

public abstract class AbstractExamples {

public int i;

public abstract void function1();

public void function2(){
System.out.println("Non abstract method");
function1();
}
}

public class AbstractClassExample extends AbstractExamples {

/**
* @param args
*/
public static void main(String[] args) {

AbstractClassExample abs=new AbstractClassExample();
abs.function1();
abs.function2();

System.out.println("int"+abs.i);

}

@Override
public void function1() {
System.out.println("Implemention abstract methods");

}

}

iterator
==========
iterator is an interface

Used step through collection of elements.

import java.util.Iterator;
import java.util.LinkedList;

public class IteratorExample {

/**
* @param args
*/
public static void main(String[] args) {
LinkedList ll=new LinkedList();
ll.add(2);
ll.add(5);

Iterator it=ll.iterator();
while(it.hasNext())
System.out.println(it.next());
}

}

Interface vs abstract
======================
we can implement multiple interface for class
Interface is limited to public and abstract methods. Abstract class can provide non abstract methods also.
Interface will take time for finding the corresponding methods. Abstract are fast

public abstract class AbstractExamples {

public int i;

public abstract void function1();

public void function2(){
System.out.println("Non abstract method");
function1();
}
}

public interface InterfaceExample {

public int j=0;

public void  interfaceFun1();

public void interfaceFun2();
}

public interface InterfaceExample2 {

public void interfaceExample2Fun1();
}

public class AbstractInterfaceExampleiMpl extends AbstractExamples implements InterfaceExample,InterfaceExample2{

@Override
public void interfaceFun1() {
// TODO Auto-generated method stub
System.out.println("Interface Fun1");
}

@Override
public void interfaceFun2() {
// TODO Auto-generated method stub
System.out.println("Interface Fun2");
}

@Override
public void function1() {
// TODO Auto-generated method stub
System.out.println("Abstract Fun1");
}

public static void main(String args[]){

}

@Override
public void interfaceExample2Fun1() {
// TODO Auto-generated method stub
System.out.println("Interface Example2 Fun1");
}
}

Virtual Functions in Java
=============
Abstract classes contain pure virtual functions in java.
Virual functions are like normal functions in java and we can override in subclasses.


wait vs sleep
================
wait can resume by notify or notifyall
sleep canto resume
wait can be called via synchronized block or method
wait is on Object and sleep is on Thread(sleep is static method)
yield pause the exection and give chance to other thread for execution
IllegalMonitorStateException occur in java if we dont call wait,notify,notifyAll in Synchronized method and race condition may happen between wait and notify



Checked vs Unchecked Exceptions
=============================

Checked exceptions should be handled in the code.

Compile error will throw for checked exception if we are not handling in  code.

Unchecked exception
===================
NullPointerException
ArrayIndexOutOfBound
IllegalArgumentException
IllegalStateException

Checked exception
=============
IOException
SQLException
DataAccessException
ClassNotFoundException
InvocationTargetException

Final
=======
Final methods canot be overriden but can be overloaded.
Final variable canot be reassigned or intialized.
Final classes canot be subclassed

Primitive Vs Wrapper
==============
Primitive - int,double,float,...
Wrapper- Integer,Double...

Reference: http://en.wikipedia.org/wiki/Primitive_wrapper_class