Java Flashcards
What are the principles of Object Oriented Design?
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.
What is inheritance?
Inheritance allows a modular and hierarchical organizing structure for reusing code. It allows you to create generic classes that can be specialized.
What is polymorphism?
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.
What’s the difference between abstract classes and interfaces?
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.
What is an object?
An object has state and behavior. The state is represented by the fields. The behavior is realized through the methods.
What is the difference between an ArrayList and a Vector?
Vectors are deprecated. Vector is synchronized for concurrent modification exception (different threads accessing the same variable)
What is synchronize in Java?
Synchronization has to do with different threads accessing the same resources. Synchronized threads prevent interference and memory inconsistency errors.
What is the purpose of Garbage collection?
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()
What is the difference between a HashMap and a HashTable?
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.
What is the difference between String, StringBuilder, and StringBuffer?
Strings: are immutable objects and they are thread safe.
StringBuffer: mutable and thread safe.
StringBuilder: mutable not thread safe.
Difference between a checked exception Java.lang.exception, and unchecked exception Java.lang.runtimeexception.
Checked exceptions are checked at compile time.
Unchecked exceptions are thrown at runtime. For example ArrayIndexOutOfBoundsException.
Is it possible to have a memory leak in Java?
Yes, when an object has not been set to null but it is no longer needed. The Garbage collector will not get the object.
Explain JVM vs JRE vs JDK?
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.
What is the final variable in Java?
Its value cannot be changed.
What is a final method?
A final method cannot be overridden but it is inherited.