Friday, December 25, 2009

Java Interview Questions

1) What is the difference between static and instance variables ?

2) What is Marker interface ?

3) What is Serialization ?

4) What is the difference between instance and local variables ?

5) What is Cloneable & How to make non cloneable singleton class ?

6) Difference between the abstract class and interface ?

7) What modifiers will the interface properties take ?

"public static final" for interface member variables
"public abstract" for interface member methods

8) Write a code to print odd numbers ?

9) Explain about Garbage Collector? What are the ways GC can be done?

10) Explain about different forms of Polymorphism?

11)What are the types of Inner Classes and Explain?

12)What are the ways Thread can be implemented?
extends Thread and override run(), implements Runnable and implement run().

13)What are the types of scheduling in Thread?
Pre-emptive sceduling and Time Slicing.
14)Explain different states of Thread?

NEW ---> RUNNABLE ---> RUNNING ---> BLOCKED
|_______________________|

15)What is the use of yield?
to give chances to other thread (of same priority)
16)At what criteria a Thread can be in blocked state?
waiting for IO request, waiting for the lock of an object whose lock is acquired by another thread.

17)Explain about Enum?
Type Constants

18)Explain about collection? and how they are classified?
Aggregation of homogenous objects in a single unit and used to access, update and communicate with it.

19)why ArrayList is faster than LinkedList?
implements RandomAccess - a marker Interface tells the JVM to access object directly at the given index

20)what is the difference between Comparable and Comparator?
Comparable::int compareTo(T t) | Comparator::compare( T t1, T t2)
Standard comparision algorithm | allows any no of Comparision algorithm.
If source code is available | if source code is not available.

21)Can there be an abstract class with no abstract methods in it? - Yes

22)Can an Interface be final? - No

23)Can an Interface have an inner class?
Yes, but only static final inner class is possible.

24)Can we declare an anonymous class as both extending a class and implementing an interface?
Yes.

new EmpComparator implements Comparator extends MyAppComparator {
public int compare( Employee e1, Employee e2) {
return calculateAppConstant(e1.getId() ) - calculateAppConstant(e2.getId() );
}
public int calculateAppConstant(int i) { ... }
}

25)What is Externalization?
- programattically handling the control of serialization process.
- implement readExternal(), writeExternal();
- default constructor is must, will be invoked prior to readExternal()
- eg: password encryption before persistence.

26)Access Modifiers ?

* public
* protected
* default
* private

27)Access Specifier ?

* public
* protected
* private


26)Explain protected access specifier?

package certification;
public class Parent {
protected int x = 9; // protected access
}

package other;
import certification.Parent;
class Child extends Parent {
public void testIt() {
System.out.println("x is " + x); // No problem; Child inherits x
Parent p = new Parent(); // Can we access x using the p reference?
System.out.println("X in parent is " + p.x); // Compiler error!
}
}

27) JSP Scriptlets or Tag Libraries?

There are many developers who believe custom tags should be used in JSP pages, rather than scriptlets or expressions. The rationale is:
· Scriptlets mix logic with presentation
· Scriptlets break the separation of roles
· Scriptlets make JSP pages difficult to read and maintain

Custom tags on the other hand centralize code in one place and help maintain the separation of responsibilities. They also support the concept of reuse because the same tag can be inserted into multiple pages, while having the implementation reside in a single location. There’s no redundancy or copy-andpaste programming like there is with JSP scriptlets

28) How can be handle OutOfMemory Error. Is it possible to track what are the objects currently in heap?

29) How to run 10 thread after excuting particular 5 thread?

30) what is the exact use of HashTable and HashMap?

31) How can we configure the outer-join in the Hibernate?

32) How we implement transaction in spring?

33) Can the variable in Interface is reintialized in sub-class?

34) Can the static variable in class is reintialized in sub-class?

2 comments:

  1. Gokul.. where is the answer.. ?? :)

    ReplyDelete
  2. These are questions faced by me in interviews i attended. Plan to provide answers in nearby times.

    ReplyDelete