Java Interview Questions

Q:

What are pass by reference and passby value?

A: Pass By Reference means the passing the address itself rather than passing the value. Passby Value means passing a copy of the value to be passed. 
 

Q:

Difference between HashMap and HashTable?

A: The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls. (HashMap allows null values as key and value whereas Hashtable doesnt allow). HashMap does not guarantee that the order of the map will remain constant over time. HashMap is unsynchronized and Hashtable is synchronized.
 

Q:

Difference between Vector and ArrayList?

A: Vector is synchronized whereas arraylist is not.
 

Q:

What is the difference between a constructor and a method?

A: A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator.
A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.
 


Q:

What if the main method is declared as private?

A: The program compiles properly but at runtime it will give "Main method not public." message.
 

Q:

What if the static modifier is removed from the signature of the main method?

A: Program compiles. But at runtime throws an error "NoSuchMethodError".
 

Q:

What if I write static public void instead of public static void?

A: Program compiles and runs properly.
 

Q:

What if I do not provide the String array as the argument to the method?

A: Program compiles but throws a runtime error "NoSuchMethodError".
 

Q:

If I do not provide any arguments on the command line, then the String array of Main method will be empty or null?

A: It is empty. But not null.
 

Q:

How can one prove that the array is not null but empty using one line of code?

A: Print args.length. It will print 0. That means it is empty. But if it would have been null then it would have thrown a NullPointerException on attempting to print args.length.
 


Java Interview Questions