Interview questions Flashcards
- 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.
- 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?
Modifiers can be moved around freely. The visibility modifier (if there is one)must be at the start.
- 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 LinkedLis
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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).
- What is a shutdown hook?
A shutdown hook allows you to start and run a Thread as the JVM is shutting down.
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.
- 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.
but keep in mind that we don’t, as a rule, create identifiers (variable, field, class, interface, or method names) with leading underscores or dollar signs; those options are generally used by automatic code generation tools (e.g. the Room Annotation Processor). Also, a single underscore is not currently allowed as an identifier.
- 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.
- 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.
1-
if a.equals(b), then a.hashCode() == b.hashCode().
2
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.
- Can you override a static method?
No. Static methods belong to the class itself, not to an object instance.