Interview questions Flashcards

1
Q
  1. What is a static variable?
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. 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?
A

Modifiers can be moved around freely. The visibility modifier (if there is one)must be at the start.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. Why would you pick an ArrayList or LinkedList?
A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. What are the different access modifiers in Java and what do they mean?
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. If you wanted to prevent your methods or classes from being overridden, how would you do this?
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. What is an immutable object?
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. What is the difference between overloading and overriding?
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. What does it mean when we say java does not support multiple inheritance? Is this a good thing?
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. What is an abstract class?
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. What does “write once run anywhere” mean in relation to Java?
A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. What is a shutdown hook?
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. What rule are there around naming variables?
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. Does Java support goto?
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  1. What is the contract between hashcode and equals?
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  1. Can you override a static method?
A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
  1. What do we mean when we say java supports covariant return type
A

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.

17
Q
  1. What is the difference between final, finally and finalize?
A

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 executed 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.

18
Q
  1. What is the difference between HashMap and HashTable?
A

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

19
Q
  1. What is the difference between Collections.synchronizedMap and ConcurrentHashMap?
A

ConcurrentHashMap :
More performant and allows parallel access for reads and writes from multiple threads.
- no guarantee that the iterator will or will not include updates made during the iteration. -ConcurrentHashMap also does not allow null keys or values.
Collections.synchronizedMap - unperformant but has guaranteed safety and data visibility.

20
Q
  1. What is a shallow copy vs. a deep copy?
A

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.

21
Q
  1. What is singleton class in Java and how can we make a class singleton?
A
7. What is singleton class in Java and how can we make a class singleton?
Singleton class is a class whose only one instance can be created at any given time, in one JVM. A class can be made singleton by making its constructor private.