Java interview questions and answers

  1. What is garbage collection? What is the process that is responsible for doing that in java? - Reclaiming the unused memory by the invalid objects. Garbage collector is responsible for this process
  2. What kind of thread is the Garbage collector thread? - It is a daemon thread.
  3. What is a daemon thread? - These are the threads which can run without user intervention. The JVM can exit when there are daemon thread by killing them abruptly.
  4. How will you invoke any external process in Java? - Runtime.getRuntime().exec(….)
  5. What is the finalize method do? - Before the invalid objects get garbage collected, the JVM give the user a chance to clean up some resources before it got garbage collected.
  6. What is mutable object and immutable object? - If a object value is changeable then we can call it as Mutable object. (Ex., StringBuffer, …) If you are not allowed to change the value of an object, it is immutable object. (Ex., String, Integer, Float, …)
  7. What is the basic difference between string and stringbuffer object? - String is an immutable object. StringBuffer is a mutable object.
  8. What is the purpose of Void class? - The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the primitive Java type void.
  9. What is reflection? - Reflection allows programmatic access to information about the fields, methods and constructors of loaded classes, and the use reflected fields, methods, and constructors to operate on their underlying counterparts on objects, within security restrictions.
  10. What is the base class for Error and Exception? - Throwable
  11. What is the byte range? -128 to 127
  12. What is the implementation of destroy method in java.. is it native or java code? - This method is not implemented.
  13. What is a package? - To group set of classes into a single unit is known as packaging. Packages provides wide namespace ability.
  14. What are the approaches that you will follow for making a program very efficient? - By avoiding too much of static methods avoiding the excessive and unnecessary use of synchronized methods Selection of related classes based on the application (meaning synchronized classes for multiuser and non-synchronized classes for single user) Usage of appropriate design patterns Using cache methodologies for remote invocations Avoiding creation of variables within a loop and lot more.
  15. What is a DatabaseMetaData? - Comprehensive information about the database as a whole.
  16. What is Locale? - A Locale object represents a specific geographical, political, or cultural region
  17. How will you load a specific locale? - Using ResourceBundle.getBundle(…);
  18. What is JIT and its use? - Really, just a very fast compiler… In this incarnation, pretty much a one-pass compiler — no offline computations. So you can’t look at the whole method, rank the expressions according to which ones are re-used the most, and then generate code. In theory terms, it’s an on-line problem.
  19. Is JVM a compiler or an interpreter? - Interpreter
  20. When you think about optimization, what is the best way to findout the time/memory consuming process? - Using profiler
  21. What is the purpose of assert keyword used in JDK1.4.x? - In order to validate certain expressions. It effectively replaces the if block and automatically throws the AssertionError on failure. This keyword should be used for the critical arguments. Meaning, without that the method does nothing.
  22. How will you get the platform dependent values like line separator, path separator, etc., ? - Using Sytem.getProperty(…) (line.separator, path.separator, …)
  23. What is skeleton and stub? what is the purpose of those? - Stub is a client side representation of the server, which takes care of communicating with the remote server. Skeleton is the server side representation. But that is no more in use… it is deprecated long before in JDK.
  24. What is the final keyword denotes? - final keyword denotes that it is the final implementation for that method or variable or class. You can’t override that method/variable/class any more.
  25. What is the significance of ListIterator? - You can iterate back and forth.
  26. What is the major difference between LinkedList and ArrayList? - LinkedList are meant for sequential accessing. ArrayList are meant for random accessing.
  27. What is nested class? - If all the methods of a inner class is static then it is a nested class.
  28. What is inner class? - If the methods of the inner class can only be accessed via the instance of the inner class, then it is called inner class.
  29. What is composition? - Holding the reference of the other class within some other class is known as composition.
  30. What is aggregation? - It is a special type of composition. If you expose all the methods of a composite class and route the method call to the composite method through its reference, then it is called aggregation.
  31. What are the methods in Object? - clone, equals, wait, finalize, getClass, hashCode, notify, notifyAll, toString
  32. Can you instantiate the Math class? - You can’t instantiate the math class. All the methods in this class are static. And the constructor is not public.
  33. What is singleton? - It is one of the design pattern. This falls in the creational pattern of the design pattern. There will be only one instance for that entire JVM. You can achieve this by having the private constructor in the class. For eg., public class Singleton { private static final Singleton s = new Singleton(); private Singleton() { } public static Singleton getInstance() { return s; } // all non static methods … }
  34. What is DriverManager? - The basic service to manage set of JDBC drivers.
  35. What is Class.forName() does and how it is useful? - It loads the class into the ClassLoader. It returns the Class. Using that you can get the instance ( “class-instance”.newInstance() ).
  36. Inq adds a question: Expain the reason for each keyword of
    public static void main(String args[])

This entry was posted in Java, Networking. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

23 Comments on Java interview questions and answers

  1. Ramesh.B
    Posted 10/21/2004 at 3:29 am | Permalink

    Its Really good for people who attend Technical interview.
    The contents are very crisp and informative.
    Try to put more questions so that it may help people like me who want to succeed in Interviews.
    Thanks in Advance.

  2. Biswajit Dash
    Posted 10/21/2004 at 12:08 pm | Permalink

    How you are going to distinguish between stateless and statefull ejb ?
    Ans. by overloading of create method.

  3. Inq
    Posted 11/18/2004 at 12:04 pm | Permalink

    A good interview question :
    Expain the reason for each keyword of “public static void main(String args[])”

  4. uttam
    Posted 12/1/2004 at 6:30 am | Permalink

    It’s very good.
    I have a Q that is when the designer have to use interface and when he has to use abstract class

  5. Weavo
    Posted 12/9/2004 at 7:24 am | Permalink

    Referring to Q36, can someone explain the purpose of the Static keyword in public static void main(String args[])

    Thanks

  6. Yevgeniy
    Posted 12/10/2004 at 2:30 pm | Permalink

    Using answers Q27,Q28,Q29 on the interview will have rather negative result.

  7. amol gavali
    Posted 12/15/2004 at 2:18 am | Permalink

    your technical questions are very good for interviews

    Thank you

  8. GS
    Posted 3/14/2005 at 3:24 pm | Permalink

    Q36. Expain the reason for each keyword of public static void main(String args[])

    Ans:
    public- main(..) is the first method called by java enviroment when a program is executed so it has to accessible from java enviroment hence the access specifier has to be public.

    static: Java enviroment should be able to call this method without creating an instance of the class , so this method must be declared as static.

    void: main does not return anything so the return type must be void

  9. Buland
    Posted 3/16/2005 at 6:42 am | Permalink

    Dear Weavo/uttam ,

    Wave Question:
    ————-
    Actually, jvm will call main method with the corresponding class name(class in which main method is declared). And all methods,attibutes which you will access from class name not from class object(menas only one copy will be there for each object) you will declare those stuff as static.

    uttam Question:
    ————–
    make interface, if you wanna use multiple inheritence as you can’t do it in java with extends keyword(you can only extend one class but implements many interfaces)

    for ex: public class A extends B implements C,D,E,…

    hope, it may answers your questions.

    Cheers Up.
    Bye

  10. Posted 3/17/2005 at 6:23 am | Permalink

    This is excellent Q & A type stuff in java, It is really useful for the java guys, who are attending for j2ee tech. interviews.

    thanks and regards
    narasimharao konjeti.

  11. Posted 3/22/2005 at 4:05 am | Permalink

    Dear Friends,

    My sincere thanks to the entire team who prepared this Java Question page .. It is really very helpful to prepare for interview.

  12. Srinivasa Kankipati
    Posted 4/18/2005 at 1:36 am | Permalink

    Hi All,

    I want to make a comment on the following question ?

    How you are going to distinguish between stateless and statefull ejb ?
    Ans is it is described in the Deployment Descriptor.

  13. md.rafi
    Posted 4/28/2005 at 4:27 am | Permalink

    how to persistence the session bean?

  14. anu
    Posted 5/4/2005 at 4:00 pm | Permalink

    Hi,
    Reg Q27 I don’t think ans is apporiate. Inner class will be called as nested class only if it has static modifier that doesn’t mean that inner class methods are ” all static”. You can instantiate nested class without using instance of outer class.
    Thanks

  15. Ramesh
    Posted 5/25/2005 at 2:25 am | Permalink

    Hi,

    The definition which you have given is not sufficient (not all quesitons).Please make it as brief.

    Rgds/Ramesh.

  16. satyendra kumar
    Posted 6/2/2005 at 5:18 am | Permalink

    what is basic difference between javabeans and EJB?

  17. Salman
    Posted 7/27/2005 at 8:43 pm | Permalink

    Q-35 You can also say that Class.forName is a kind of reflection and the class that is mentioned in it is loaded on runtime not before that..

  18. Prab.B
    Posted 7/25/2006 at 5:59 pm | Permalink

    The difference between EJB and javabean is that EJB is for Multiple heterogeneous env running on CORBA IIOP. Where as Java bean is simple java class object.
    Stateful session bean is one which can retain informations of sessions, where as stateless session bean cannot retain informations. Eg: loan calculator is stateless session bean, which loses information after you use it.

  19. parthasarathy
    Posted 9/10/2006 at 3:09 pm | Permalink

    we may call the class loading operations by using class.forName(”") as a late binding.i need more detailed about this.

  20. Janani
    Posted 2/19/2007 at 2:03 am | Permalink

    driver managers provides services for the transaction with the database, there are several vendors for those driver managers, these driver managers provide code for the connection with the database. driver managers consists of drivers, these driver managers consists of driver libraries in which these drivers has to register with.

  21. Janani
    Posted 2/19/2007 at 2:21 am | Permalink

    answer for the question 36 public static void main(string args[]).

    public modifier explains that the entire class can be acessed from anywhere. The reason why we declare the public for main() is that in java the main() function comes inside another class eg class a{
    public static void main(String args[]){}}, static means it need not be instantiated, i.e for every class you need to create an instance(copy) objects inorder to acess the class and its method. But for main method, need not have an instance it can be called without an instance(it indicates the interpreter to start the execution of the program following the main), void indicates a place holder( uninstantiable ) for those which follows the return type void for the main function, The argument String indicates the argument type which is given at the command line and arg is an array for string given during command line.

  22. chinmay
    Posted 3/8/2007 at 2:15 am | Permalink

    is

  23. chinmay
    Posted 3/8/2007 at 2:16 am | Permalink

    is Void a Wrapper class??

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*