Core Java Interview Questions Part II

Core Java Interview Questions

Core Java Interview QuestionsThe name of the game is core java interview questions and I will be your quiz master. Each week I will publish 10 new quick fire questions and answers. If you have any questions you’d like to suggest then drop me an emailat hello@corejavainterviewquestions.com.

1. What is a shutdown hook?

A shutdown hook allows you to start and run a Thread as the JVM is shutting down.

Runtime.getRuntime().addShutdownHook(new Thread() {

});

There are a number of reasons you may want to use one, such as to stop accepting new clients, or to correctly close up connections.  It cannot be relied on to execute, for example if there is a forced shutdown such as kill -9.

2. What rule are there around naming variables?

Variables must begin with a letter, $ (dollar sign) or _ (underscore). As a result a variable cannot begin with a number, however subsequent characters can include numbers.

3. Does Java support goto?

goto is a reserved keyword in java, however it is marked as not used.  As a result you cannot directly do goto statements in java.

4. What is the contract between hashcode and equals?

Two objects which are equal must have the same hashcode.  Two objects with the same hashcode may or may not be equal.

if a.equals(b), then a.hashCode() == b.hashCode().
if a.hashCode() == b.hashCode(), a may or may not equal() b

In practice this means when you override one you should override the other.

5. Can you override a static method?

No.  Static methods belong to the class itself, not to an object instance.

6. What do we mean when we say java supports covariant return type

When implementing/overriding a method, we can return a class which is a subtype of the original return type.  For example, the parent returns a Car, it is ok for the overriding class to return a Tesla.

public interface CarFactory {
    Car makeCar();
}
interface Car {
}

class TeslaFactory implements CarFactory{

    @Override
    public Tesla makeCar() { //Returns a Tesla, a subtype of Car, wheras the parent returns a car
        return null;
    }

    private class Tesla implements Car {
    }
}

This has been the case since Java 5.

7. What is the difference between final, finally and finalize?

final is a keyword that can be used on classes, methods or variables to denote that the value cannot be changed or overridden.  finally is excuted at the end of a try catch block and is guaranteed to execute under normal circumstances. finalize is a method called on an object when it is being garbage collected. There is no guarantee that it will be ever executed so it’s best not to rely on it.

8. What is the difference between HashMap and HashTable?

HashTable is syncronized and will not allow null values.  HashMap is not syncronized and will allow 1 null key and all null values.  HashTable is generally discouraged now, with Collections.synchronizedMap(Map) and ConcurrentHashMap as preferred alternatives.

9. What is the difference between Collections.synchronizedMap and ConcurrentHashMap?

ConcurrentHashMap is significantly more performant and allows parallel access for reads and writes from multiple threads.  However, it makes no guarantee that the iterator will or will not include updates made during the iteration.  There is therefore the potential for a Thread to have an out of date view.  Collections.synchronizedMap locks the entire map for reads and writes and is therefore unperformant but has guaranteed safety and data visibility.  ConcurrentHashMap also does not allow null keys or values.

10. What is a shallow copy vs. a deep copy?

A shallow copy will keep all of the same references as the original object.  For example, if a Person object has a reference to a Car, let’s call it Car A, and we do a shallow copy of the Person then the new Person will point to Car A. If we do a deep copy we will navigate the entire object graph from that Person and copy every other object. In this case we would make a copy of Car A to belong to our new person.

Be the first to leave a comment. Don’t be shy.

Join the Discussion

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>