Sunday, May 3, 2009

Java Interview Questions : Basics I

Here are some questions that are normally asked in interviews for Java candidates.

1. Which is the default package that is imported even if you don’t specify it explicitly?

Ans. java.lang

2. When you specify import java.package.*, does it import all the sub-packages inside it?

Ans. No, for importing them, you will need to add more import statements such as, import java.package.sub.* etc.

3. What is the concept of a package in Java, why do we need packages?

Ans. This is a feature by which you can manage your classes, and prevent class name collisions. Think of a situation when your application has 1000s of classes. Say, for a bank application, there can be a Credit Card account and a Debit Card Account. You need to have a class for Account. But, you cannot have two different classes with the same name. Putting the two accounts classes in different packages will allow you to have two classes with the same name which exist in different packages.

4. Is it an error to add a package more than once?

Ans. No, neither the compiler or the JVM will report any error. While executing, it will be imported only once, no matter how many times you import a package.

5. What is the difference between the following operations, equals() and == ?

Ans. == will only compare the references, i.e, if they are present in the same memory location, while equals() will compare the contents of the references. But the output of the equals() operator might not be as expected if the equals() method is overwritten in any class. It is imperative that whenever you overwrite the equals() method , you should also override the hashCode() method, if you want your classes to be used by some collection classes.

6. Which is the root class of all Java classes?

Ans. It is the java.lang.Object class.

7. Will there be a problem if the main method is not declared as static?

Ans. You will not get a compile time error, but at run time, it throws an error, “Main method not public”, since the JVM will look for a static main method to execute a program. The main method is the only entry point to a Java program.

8. I have an object of some type, how do I figure out it’s class?

Ans. If you want to get its class name, then 

object.getClass() will return the fully qualified Class name of that object. If you want to check if that object is a type of a specific class, you can use instanceOf keyword which will return a boolean value depending upon the match.

9. Can a constructor of a class be private?

Ans. Yes, but then, you cannot create an instance of such a class directly. This is done in building Singleton classes. Please refer to Singleton design pattern to know more.

10. Can a constructor return a value?

Ans. No, this will give a compiler error. You cannot even return a void.

11. Can a constructor be static?

Ans. No.

12. Are constructors inherited, i.e, can they be overwritten in a sub class?

Ans. No.

13. Can a constructor be declared as “native”?

Ans. No.

14. Can a constructor be synchronized?

Ans. No.

15. In Java, do we have Pass by Value or Pass by Reference?

Ans. Java doesn’t support pass by reference. Everything in Java is Pass by Value.

16. How can we restrict a class from being instantiated directly?

Ans. This can be done by either marking it as “Abstract” or making it’s constructor “private”.

17. What is the difference between J2SDK 1.5 and J2SDK 5?

Ans. No difference, they are the same, but SUN came up with a fancy name. It is widely referred to as “Java 5”.

18. A method doesn’t have an access specifier. Is this OK?

Ans. Yes, this method will now have default access, meaning within that package in which the class is defined.

2 comments:

Linda said...

Great Post!Interview Questions

julie anderson said...

Interesting blog that they even mentioned the answers too ! Java programmers has demand in the market for designing the projects!
Questions