Core Java Interview Questions List: Part I
Core Java Interview Questions List: Part I
You may not be a fan of these interview questions since some of them are not the things legitimate companies should be asking, but they're good to know.
Join the DZone community and get the full member experience.Join For Free
As I’ve discussed several times on this site, I’m not a fan of learn by wrote Java questions. However, a lot of people continue to ask them during their interview process so it means you need to know how to answer them.
The 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.
1. What is a static variable?
A static variable is a value that remains the same through all the instances and can be modified by all the instances. It is a shared value.
2. If I change the order of a methods modifiers will it still work? Is public static void main the same as public void static main? What about void static public main?
Modifiers can be moved around freely. The visibility modifier (if there is one)must be at the start.
3. Why would you pick an ArrayList or LinkedList?
ArrayList is great if you need fast access to objects but can cope with slower writes. Conversely, if you need fast writes (moving, removing or adding) but can cope with slower direct access then choose a LinkedList (read our in depth article on collections here).
4. What are the different access modifiers in Java and what do they mean?
public- accessible to everyone in the JVM
private- only accessible from inside the class.
protected- accessible from by any class in the same package or any subclass in any package
default-when no visibility modifier is present. accessible from any class in the same package only.
5. If you wanted to prevent your methods or classes from being overridden, how would you do this?
By declaring something as final you prevent it from being overridden. Nothing is perfect though, as crafty developers can always use Reflection to get around this, or alternatively just copy and paste your code into their own version of the class. It is rarely a good idea to prevent your methods or classes being overridden, and you should code defensively to reflect this.
6. What is an immutable object?
The state of an immutable object cannot be changed after construction. Immutable objects can play an important roll in threading. (read more on threading here). A good example of an immutable objeect is String. Any modification to an immutable object will result in the creation of a new object.
7. What is the difference between overloading and overriding?
Method overloading is having multiple methods with the same name. They can be differentiated as they will have a different signature, e.g. take different method parameters. Overriding is used when creating a subclass of a class and specifying your own functionality for the method by copying the method signature identically.
8. What does it mean when we say java does not support multiple inheritance? Is this a good thing?
Java cannot extend functionality from more than one concrete or abstract class. If both parent classes had a jump() method, it would be unclear which functionality the caller would need to use. We can implement multiple interfaces however as the the implementation occurs in our actual class so this problem does not occur.
9. What is an abstract class?
Similar to an interface, an abstract class cannot actually be instantiated. Unlike an interface, an abstract class can have method implementations. Any method without implementation will have the modifier “abstract” to indicate to classes which extend it that they must provide the implementation.
10. What does “write once run anywhere” mean in relation to Java?
Java is a cross platform language; java compiles down to byte code which can be run on a Java Virtual Machine (JVM). JVMs are available on many platforms including the major operating systems. This means that any Java application can in theory run on any platform where a JVM is available, hence write once (for the JVM) and run anywhere (there is a JVM).
Published at DZone with permission of Sam Atkinson , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.