Java Flashcards

1
Q

What are the principles of Object Oriented Design?

A
AEM
ABSTRACTION: break an idea down to simpler parts. an abstract class can specify what the class does but not necessarily how it does it. 

ENCAPSULATION:information hiding. The implementation does not need to reveal how functionality is implemented. A good design will allow different implementations to be used without effecting other parts of the application.

MODULARITY: software is divided into smaller functional units that work together.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is inheritance?

A

Inheritance allows a modular and hierarchical organizing structure for reusing code. It allows you to create generic classes that can be specialized.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is polymorphism?

A

The ability of a variable to take many types. For example if shape is an interface it could be implemented as a concrete class square or rectangle.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What’s the difference between abstract classes and interfaces?

A

You cannot instantiate an interface or abstract class.

Interface: it creates a contract with the implementing class. you do not provide an implementation in the methods of an interface. Variables and methods must be public. A class can implement multiple interfaces. Useful for unrelated classes implementing similar functionality.

An advantage of interfaces is that you can loosely couple your code making the implementation flexible.

Abstract: similar to an interface but allows you to declare private and protected fields and methods. Use abstract class to share code among related classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is an object?

A

An object has state and behavior. The state is represented by the fields. The behavior is realized through the methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference between an ArrayList and a Vector?

A

Vectors are deprecated. Vector is synchronized for concurrent modification exception (different threads accessing the same variable)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is synchronize in Java?

A

Synchronization has to do with different threads accessing the same resources. Synchronized threads prevent interference and memory inconsistency errors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the purpose of Garbage collection?

A

Garbage collection discards objects that are no longer needed and reclaim those resources for further use. You cannot force run garbage collection but you can hint to the JVM to run garbage collection by calling System.gc()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the difference between a HashMap and a HashTable?

A

HashMap: is unsynchronized and allows one NULL key and multiple NULL values. Best for a single threaded application.

HashTable: is synchronized and does not allow null keys and values. Uses a single lock for the whole data.

ConcurrentHashMap: is synchronized and provides better performance than a HashTable. For example there is no lock on the data when doing a read. More efficient for threaded applications.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the difference between String, StringBuilder, and StringBuffer?

A

Strings: are immutable objects and they are thread safe.
StringBuffer: mutable and thread safe.
StringBuilder: mutable not thread safe.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Difference between a checked exception Java.lang.exception, and unchecked exception Java.lang.runtimeexception.

A

Checked exceptions are checked at compile time.

Unchecked exceptions are thrown at runtime. For example ArrayIndexOutOfBoundsException.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Is it possible to have a memory leak in Java?

A

Yes, when an object has not been set to null but it is no longer needed. The Garbage collector will not get the object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Explain JVM vs JRE vs JDK?

A

The JVM is a specification that tells you how to implement a JRE.
The JRE is an implementation of the JVM. Let’s you run Java applications.
JDK: lets you develop and run Java applications. Includes compilers, debuggers, and JavaDoc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the final variable in Java?

A

Its value cannot be changed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a final method?

A

A final method cannot be overridden but it is inherited.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a final class?

A

A final class cannot be extended or inherited from.

17
Q

What is the finalize() method used for in Java?

A

The finalize method is called by the Garbage collector when the object has no more references. It performs cleanup tasks before the object is discarded.

18
Q

What is the difference between an ArrayList and LinkedList ?

A

They both implement the List interface. Linked List is implemented as a doubly linked list. ArrayList is implemented as a dynamic array.

ArrayList faster when reading elements. It is slower when adding because elements have to be shifted.

LinkedList is slower for reading elements because you have to traverse through them. It is faster for adding or removing.

19
Q

What is the difference between a Map and List?

A

Both are interfaces.
Map: contains keys with values. Cannot contain duplicates.

List: a list of values in an ordered collection.