life ideas

November 9, 2007

Java Interview Questions

Filed under: Uncategorized — manoftoday @ 1:50 am

1. Java says “write once, run anywhere”. What are some ways this isn’t quite true? Any time you use system calls specific to one operating system and do not create alternative calls for another operating system, your program will not function correctly. Solaris systems and Intel systems order the bits of an integer differently. (You may have heard of little endian vs. big endian) If your code uses bit shifting, or other binary operators, they will not work on systems that have opposide endianism.

2. What’s the difference between == and equals method? The typical “gotcha” for most people is in using == to compare two strings when they really should be using the String class’s equals() method. From above, you know that the operator will only return “true” when both of the references refer to the same actual object. But, with strings, most uses want to know whether or not the value of the two strings are the same — since two different String objects may both have the same (or different) values.

3. two types of stream

basically there are 2 types of streams.

byte streams that are used to handle stream of bytes and character streams for handling streams of characters.

in byte streams inputStream/outputStream are the abstract classes at the top of hierarchy,while writer/reader are abstract classes at the top of character streams hierarchy.

4.What class allows you to read objects directly from a stream? The ObjectInputStream class supports the reading of objects from input streams.

5. Can you write Java code for declaration of multiple inheritance in Java ? Class C extends A implements B { }

6. Given two tables Student(SID, Name, Course) and Level(SID, level) write the SQL statement to get the name and SID of the student who are taking course = 3 and at freshman level.


FROM Student, Level

WHERE Student.SID = Level.SID AND Level.Level = “freshman” AND Student.Course = 3;

7. advantagesof OOPL The features of OOPL as inheritance, polymorphism, encapsulation makes it powerful.

8. marker interfaces in java except Serializable.



An interface having no methods is called as a Marker Interface.

9. instanceof() vs isInstance()

Point pt = new Point(3, 4);


System.out.println(“Is pt a String? [using instanceof] ” +

((Object)pt instanceof String));



try {


“Is pt a String? [using isInstance()] ” +

(Class.forName(“java.lang.String”).isInstance(pt))); //you need a Class object.


catch(Throwable e) {



10. Are constructors inherited? Can a subclass call the parent’s class constructor? When? You cannot inherit a constructor. By giving the developer the ability to override a superclasses onstructor you would erode the encapsulation abilities of the language. but you can call the parent’s class constructor. 

11. synchronization

When you synchronize on an object your thread gains exclusive use of that object until synchronization ends or wait is called on the object.Language support for synchronization:

1. Use the synchronized block:

In a method you may: synchronized(object) { code here }

synchronized(SomeClass.class) { code here }

 In the first case access to the instance variables/methods of the class are synchronized.

In the second case access to the static methods/fields are synchronized.

2. Declare the method synchronized:

public synchronized foo() { code here }

public static synchronized foo() { code here }

These two act just like synchronized(this) or synchronized(MyClass.class) (depending if the method is static or not).

In addition the Object class defines the wait, notify, notifyAll, and sleep methods. To execute any of these methods the object must be in a synchronized block (this can be in a calling method). The compiler will not check this, a runtime error will be thrown if this is not the case. The wait method temporaly releases the lock on the object. The notify/notifyAll method allows a previously waiting thread to continue to execute.

Also when executing the static block of a class: public class Foo { static { code here } } A special lock is placed on the class, this lock is different from the synchronized lock, as no other thread may access the class until the static block is done.

12. access control

Public : Any other class from any package can instantiate and execute the classes and methods.

Protected : Only classes inside of the package and subclasses can access the classes and methods.

Private : The original class is the only class allowed to execute the methods.

Default : Only classes inside of the package can access the classes and methods.

13. JDBC steps

The standard steps to connecting the database as follows:


      1: Loading the drivers Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

      2:Getting the connection Connection con=DriverManager.getConnection(“jdbc:odbc:datasourcename”,”scott”,”tiger”);

      3:Create the Statement Statement st=con.createStatement();

      4:Execute the Statement ResultSet rs=st.executeQuery(“select * from emp”);

      5:Accessing the data from the backend


                        System.out.println(rs.getString(1)); System.out.println(rs.getInt(2)); System.out.println(rs.getString(3));

      6:close the connection }catch(SQLException se) { se.printStackTrace(); } finally{ con.close(); rs.close(); st.close(); }

14. java different from c++;

         java : no goto; no destructor;has garbage collection;

         Java has method overloading, but no operator overloading.

        java package;     c++: namespace

15. String abc= “xyz”;

 Important to note that you are NOT calling a java.lang.String constuctor when you type String s = “abc”; For example String x = “abc”; String y = “abc”; refer to the same object. While String x1 = new String(“abc”); String x2 = new String(“abc”); refer to two different objects. 

16. Java Network Programming

 Java supports “low-level” and “high-level” classes.

“Low-level” classes provide support for socket programming: Socket, DatagramSocket, and ServerSocket classes.

“High-level” classes provide “Web programming”: URL, URLEncoder, and URLConnection classes. Networking programming classes ease the programming of network applications, but do not substitute your knowledge of networking. Java networking like anything else in Java is platform-independent.

17.  unreachable object becomes reachable. An unreachable object may become reachable again. This can happen when the object’s finalize() method is invoked and the object performs an operation which causes it to become accessible to reachable objects.

18. modifiers for top class   A top-level class may be public, abstract, or final.

19. default value of boolean The default value of the boolean type is false.

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

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

static: Java environment 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

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.

21. What is the difference between Abstract class and …

1. Abstract Class may contains constructor but interface does not contain constructor.
2.By default, all variables in interfaces are static and final,then we can’t declare method as final.
3.In Interface all methods shuld be public but mot in abstract class.
4.interface supportes multiple inheritance but abstract class does not support.
5.we should initialise variables in interface but not necessory in abstract class.

6.interface has no method implementation.




22. How to track “memory leak” and how to prevent it i…

To reduce possibility of memory leaks, following things should be kept in mind.

Try to avoid long recursive loops in the code. This leads to an OutOfMemoryError.

Avoid collections as static members of a class. If so, carefully manage removals and additions to the Collection. If the collection continues adding members without removing them, it might lead to a leak. Static members persist throughout the life of the program. A static collection will keep references to objects, whether you are using it or not. So the garbage collector might miss collecting the unused contents of the collection.

Avoid populating the HttpSession with too much data. Session persists in the system as long as the user is online, and it might get filled up fast. Additions and removals in the session should be checked.

If you are using AWT/Swing components, call dispose() to kill the windowing component completely.

Check your native code for memory leaks, if you are calling C/C++ code from Java using JNI.

There are other things too, to take care of, but these are the most important ones.

tool: JProbe Memory debugger



  1. Nice collection of questions on java. I am trying to make a collection of limited number of really good questions, so that users can prepare for interview in 3-4 hours. Will you like to work with me.

    Comment by Fastrevise — February 29, 2008 @ 1:12 pm

  2. […] 1. Java questions […]

    Pingback by manoftoday: Java Questions — February 27, 2009 @ 1:22 am

  3. It is science that deals with an observation, and then uses facts, reason, and logic to deduce the story of what happened in the past. ,

    Comment by BadGirl31 — October 22, 2009 @ 9:44 pm

  4. nice info.. thanks

    Comment by Ecko — June 14, 2010 @ 9:24 am

  5. Hello

    Comment by i — May 9, 2011 @ 2:02 pm

  6. Every weekend i used to pay a quick visit this website, for
    the reason that i wish for enjoyment, for the reason that this
    this web site conations genuinely nice funny data too.

    Comment by winter park fl home appraiser — July 30, 2013 @ 11:21 pm

  7. After exploring a handful of the articles on your website, I really like
    your technique of writing a blog. I bookmarked it to my bookmark webpage list and will
    be checking back soon. Please visit my web site too and tell
    me your opinion.

    Comment by alertid — August 6, 2013 @ 12:49 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at

%d bloggers like this: