JAVA J2EE INTERVIEW QUESTIONS


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.


Q1 What do you mean by immutable ? How to create an immutable object ?
Immutability means an object cannot be modified after it has been initialized.  There will not be any setter methods in an immutable class. And normally these classes will be final.

 Q 2. What is class loader in java ?
A class loader is a class that is responsible for loading the class. All JVM contains one class loader called primordial class loader.

 Q3 What is a weak reference ?
A weak reference is the one that does nor prevent the referenced object from being garbage collected. The weak reference will not keep the object that it refers to alive. A weak reference is not counted as a reference in garbage collection. This will make the memory use more effective.

Q4  What is object cloning?
It is the process of duplicating an object so that two identical objects will exist in the memory at the same time.

 Q5  What is object pooling?
Creating a large number of identical short lived objects is called object pooling. This helps to minimize the need of garbage collection and makes the memory use more effective.

Q 6  What is garbage collection?
Garbage collection is the process of releasing memory used by unreferenced objects. It relieves the programmer from the process of manually releasing the memory used by objects .

 Q 7  What is the disadvantage of garbage collection?
It adds an overhead that can affect performance. Additionally there is no guarantee that the object will be garbage collected.

 Q 8  What is a Dictionary?
A
Dictionary is a parent class for any class that maps keys to values., In a dictionary every key is associated with at most one value.