What's Next: View All Tutorial →

Java Interview Question

Core Java Static variable, static method, static block with example abstract example can abstract be empty class? A class called “Sample” implement the interface “interface1” and extends the abstract class “a...

Published Date: 25/08/2014  Last Modified Date: 25/08/2014    New Post

Core Java

  1. Static variable, static method, static block with example
  2. abstract example
  3. can abstract be empty class?
  4. A class called “Sample” implement the interface “interface1” and extends the abstract class “abstract1.class”. Both “interface1” and “abstract1.class” have the same method called “show()” and show() method is defined in “abstract1.class” .  Do we have to override the method  “show()” in the sample class? No
  5. Why abstract method can’t be static? No, as abstract method need to be override
  6. Is field declared in the interface a public, static or final? Or all the three?      All the three
  7. Final keyword?
  8. What is difference between finally, final and finalize ()?
  9. At what scenario the finally block will not get execute?
  10. Try { } catch (Exception e) {} catch(ArithmeticException e){} .     Syntax correct or not? Wrong, since Exception is super class, it should be defined at last.
  11. Is it mandatory to assign the value for the final variable during declaration? Optional, but you can initialize only once. Second time you can’t assign value.
  12. Polymorphism.
  13. Does polymorphism differ by modifiers (public, private, protected)? No
  14. Does polymorphism differ by return type? No
  15. Polymorphism differ by
    1. No of arguments
    2. Types of arguments
    3. Order of arguments
  16. Serializable? Serialization is the process of saving an object in a storage medium (such as a file, or a memory buffer) or to transmit it over a network connection in binary form. 
  17. What are all the data will not be serialized even if implementing the Serializable interface
    1. Transient Keyword
    2. Static variable
    3. When child class object is serialized, the parent will not be Serialized. If parent is  serialized, child is also serializable
  1. Externalizable: Only the identity of the class of an Externalizable instance is written in the serialization stream and it is the responsibility of the class to save and restore the contents of its instances. The writeExternal and readExternal methods of the Externalizable interface are implemented by a class to give the class complete control over the format and contents of the stream for an object and its supertypes. These methods must explicitly coordinate with the supertype to save its state. These methods supercede customized implementations of writeObject and readObject methods.
    Object Serialization uses the Serializable and Externalizable interfaces.
  2. Difference between throws and throw
    1. throws will retrieve the exception if occurred and moved to the parent method
ex. public static void main(String args[]) throws Exception
  1. throw – to raise the expection   .ex throw new NullPointerException
  1. is it possible to override the equals() method? Yes . But hashcode() also need to be overridden. Because the equals method compare the object based on the hashcode. Hascode algorithm need to be defined for comparison process in the equals method
  2. is it possible to override the toString() method? Yes
  3. What is a reflection Class?- java. lang. reflect Class has the ability to analyze itself in runtime. This is used get the metadata information about class like number of methods defined, number of fields
  4. Clonable interface. To copy the object
  5. What is the difference between String and String Buffer? - a) String objects are constants and immutable whereas StringBuffer objects are not. b) String class supports constant strings whereas StringBuffer class supports growable and modifiable strings.
  6. What is the difference between exception and error? - The exception class defines mild error conditions that your program encounters. Exceptions can occur when trying to open the file, which does not exist, the network connection is disrupted, operands being manipulated are out of prescribed ranges, the class file you are interested in loading is missing. The error class defines serious error conditions that you should not attempt to recover from. In most cases it is advisable to let the program terminate when such an error is encountered.
  7. StringBuffer vs StringBuilder?     StringBuffer -synchronized
  8. Synchronization? Only the multiple thread to access the function one by one. Synchronization is the mechanism that ensures that only one thread is accessed the resources at a time.
  9. What is deadlock?- When two threads are waiting each other and can’t precede the program is said to be deadlock.
  10. What are wrapper classes?- Wrapper classes are classes that allow primitive types to be accessed as objects.
  11. Autobox , Unbox?
 Integer counter = 1;        // boxing
int counter2 = counter;     // unboxing
  1. What is Garbage Collection and how to call it explicitly?- When an object is no longer referred to by any variable, java automatically reclaims memory used by that object. This is known as garbage collection. System. gc() method may be used to call it explicitly.
  2. What is finalize() method?- finalize () method is used just before an object is destroyed and can be called just prior to garbage collection
  3. Tell some of the methods in the String class. Length(), split(), indexof(), substring(), toString(), toUpperCase(), toLowerCase()
  4. JDK1.4 vs JDK 1.5
3)Enum support
5)Generic implemention of Class, Method, WildCards, Bounded Type).
6) import static
  1. JDK 1.5 vs JDK 1.6(only performance)
Tell me some of the utils you have used?  ArrayList, Vector, LinkedList,
Array List:
  1. The underlying datastructure for array List is growable array or resizable array.
  2. Insertion order is preserved.
  3. Duplicate objects are allowed.
  4. Hetrogeneous objects are allowed.
  5. Null insertion is possible any number of times.
  • The underlying data structure for LinkedList is DoublyLinkedList.
  • Insersion order is preserved.
  • Duplicate objects are allowed.
  • Null insertion is possible any number of times.
  • Hetrogeneous objects are also allowed.
  • It implemented serializable and clonable interfaces but not RandomAccess interface.
  • LinkedList is the best choice if your frequent operation is insertion or deletion in the middle.
  • LinkedList the worest choice if your frequent operation retrieval operation.
  1. Hashtable vs HashMap
They are almost the same thing. The big differences are that HashMap is the implementation of a Hashtable on a Map interface, is not synchronized, and it allows null values for key and value, while Hashtable doesn't.
  1. LinkedHashMap vs HashMap
  2. HashMap makes absolutely not guarantees about the iteration order. It can (and will) even change completely when new elements are added.
  3. TreeMap will iterate according to the "natural ordering" of the keys according to their compareTo() method (or an externally supplied Comparator). Additionally, it implements the SortedMap interface, which contains methods that depend on this sort order.
  4. LinkedHashMap will iterate in the order in which the entries were put into the map
  1. HashSet vs TreeSet
  • class offers constant time performance for the basic operations (add, remove, contains and size).
  • it does not guarantee that the order of elements will remain constant over time
  • iteration performance depends on the 'initial capacity' and the 'load factor' of the HashSet.
    • It's quite safe to accept default load factor but you may want to specify an initial capacity that's about twice the size to which you expect the set to grow.
  • guarantees log(n) time cost for the basic operations (add, remove and contains)
  • guarantees that elements of set will be sorted [ascending, natural, or the one specified by you via it's constructor]
  • doesn't offer any tuning parameters for iteration performance
  • offers few handy methods to deal with the ordered set like first(), last(), headSet(), and tailSet() etc
Important points:
  • Both guarantee duplicate-free collection of elements
  • It is generally faster to add elements to the HashSet and then convert the collection to a TreeeSet for a duplicate-free sorted traversal.
  • None of these implementation are synchronized. That is if multiple threads access a set concurrently, and at least one of the threads modifies the set, it must be synchronized externally.
  • LinkedHashSet is in some sense intermediate between HashSet and TreeSet. Implemented as a hash table with a linked list running through it, however it provides insertion-ordered iteration which is not same as sorted traversal guaranteed by TreeSet
  1. String tokenior
Awaiting for Administrator approval

← Back to list

Related Post