Java Flashcards
What is the difference between an interface and an abstract class?
Interface
- Interface can have only abstract, default and static methods
- The interface keyword is used to declare an interface
- The implement keyword is used to make a class abide by the methods defined by the interface
- Interface supports multiple inheritance
- Interface can’t provide the implementation of abstract class
- Interface has only static and final variables
- All method declarations are implicitly public
- You can implement any number of interfaces
Abstract
- Abstract class can have abstract and non-abstract methods
- Abstract class doesn’t support multiple inheritance
- You can declare fields that have final, non-final, static and non-static variables
- Abstract class can provide the implementation of methods, have concrete methods.
- The abstract keyword is used to declare abstract class
- All fields are automatically public, static, and final
- All methods that you declare or define are public
- You can extend only one class, whether or not it is abstract
Both
- Can not be instantiated
- Can be subclassed
- If the subclass does not provide the implementation for the parent class abstract methods, the subclass must be declared abstract.
What are abstract methods?
An abstract method is a method that is declared without an implementation.
What are default methods?
Default methods enable you to add new functionality to the interfaces by specifying the method definition and using the default keyword as the method signature
What is an interface class?
Interface is a reference type and is a contract that binds specific method signatures and fields to the class that implements it
What is an abstract class?
An abstract class is a class that is declared abstract. It may or may not include abstract methods.
What is the diamond problem?
There is a class A. Both class B and C derives from class A. Thus both class B and class C implements method from class A. Now we have class D. Class D derives from class B and C. The diamond problem comes from when class D makes a call to method definitions in class A. This call is ambiguous because it is not sure whether to call the version of the method derived from class B or class C.
Java does not have the diamond problem since it does not support multiple inheritance. Instead, Java uses interfaces.
What is the static keyword?
Variables that are declared static is called a class variable that is initialized one and all instances share the same copy of the variable. It can be accessed directly without creating an instance of the class. It can be accessed directly by the class name and doesn’t need any object.
What is an instance variable
A variable without the static keyword and each instance of the class has its own copy of the variable.
What is the difference between an ArrayList and a Vector?
Vector is synchronized, which means only one thread is working and no other thread can perform an operation at a time.
Arraylist is not synchronized, so multiple threads can work on ArrayList at the same time.
Both ArrayList and a Vector can grow and shrink dynamically, but the way they resize is different. Vector double itself by ArrayList grow by half of its size.
ArrayList is faster since it is non-synchronized and Vectors are slower because it is thread-safe since the Vector gets locked until the work is complete by the thread.
An ArrayList is fail-fast since it is not thread safe unlike a Vector
What is fail-fast?
Fail-fast is an exception that gets thrown when a collection is being iterated and it detects some changes being made to the collection.
What is the difference between a tree set and a hashset?
HashSet
- Better performance than Tree Set, offers constant time
- Does not maintain any order of elements
- Store contents according to the key, or the hash code. The hash code is used as an index
Tree Set
- Takes log(n) time
- Sort in ascending order by default
Both
- Extends AbstractSet and implements the Set interface
- Does not hold duplicate elements
- It is more optimal to add elements to HashSet and convert it into Tree Set for sorted elements.
- Both are non-synchronized, so they are not thread-safe
What is a hashcode?
A number generated from an object. It is used to store and retrieve objects quickly in a hashtable.
What type of Collections that does not allow duplicates?
Set
A set is a Collection that cannot contain duplicate elements.
What is the difference between HashTable and HashMap?
HashMap
- It is not synchronized, so it is not thread safe
- HashMap allows one null key and any number of null values
- HashMap object values are iterated by using iterator
- Iterator is fail-fast
- HashMap is much faster and uses less memory than HashTable since it is not synchronized
HashTable
- It is synchronized, so it is thread safe
- HashTable do not allow null keys and null values
- HashTable is the only class other than Vector which uses enumerator to iterate the object values
- Not fail-fast
- Slower performance since it is synchronized
- Legacy
How do you serialize (persist) an object in Java?
Java has the Serialization API
Serialization is the process of saving an object’s state to a persistence store and also rebuilding the object from the save information when needed in the future.
To serialize an object
- Implement the Serializable interface. This is consider a marker interface and have no default implementation. The interface is simply an indication to the Java runtime that the object is valid for persistence
- Use the writeObject(someObject object) method from the ObjectOutputStream class.
- To restore the object, you use the readObject() method from the ObjectInputStream class and you will need to cast the object to the specific class.
- Only the object state is saved
What is a stored procedure and how would you call it in Java?
A stored procedure is an executable block of code that is written in PL/SQL and stored in the Oracle database. A stored procedure is called from a Java class using CallableStatement object. When the procedure is called, its name and any relevant parameters are sent over the JDBC connection to the DBMS which executes the procedure and returns the result (if applicable) via the connection.
What is PL/SQL
PL/SQL is a procedural language extension to Structured Query Language. The purpose is to combine database language and procedural programming language. The basic unit in PL/SQL is called a block, which is part up of three parts: a declarative part, an executable part, and an exception-building part.
What are the difference between stored procedures and functions?
Stored Procedure
- Is used to perform business logic
- Must not have the return type
- May return 0 or more values
- We can call functions from the procedure
- Procedure supports input and output parameters
- Exception handling using try/catch block can be used in stored procedures
Function
- Is used to perform calculation
- Must have the return type
- May return only one value
- Procedure cannot be called from function
- Function supports only input parameter
- Exception handling using try/catch can’t be used in user defined functions
What is JDBC?
JDBC stands for Java Database Connectivity, which is a standard Java API for database-independent connectivity between the Java programming language and the database.
- Can make connection to a database
- Can create SQL statements
- Can execute SQL queries in the database
- Can view and modify records
- Can write Java Applications
- Can write Java Servlets
- Can write Java ServerPages (JSP)
How do you establish a connection for JDBC?
DriverManager
.getConnection(“jdbc:mysql://localhost:3306/test”, “username”,”password”)
test is a database schema
What is JSP?
JavaServer Pages
It is a server-side programming technology that enables the creation of dynamic, platform-independent method for building Web-based applications.
What is the difference between Statement, PreparedStatement, CallableStatement?
Statement
- It is used to execute normal SQL queries
- It is preferred when a particular SQL query is to be executed only once
- You cannot pass the parameters to SQL query using this interface
- This interface is mainly used for DDL statements like CREATE, ALTER, DROP
- The performance of this interface is very low
PreparedStatement
- It is used to execute parameterized or dynamic SQL queries
- It is preferred when a particular query is to be executed multiple times
- You can pass the parameters to SQL query at run time using this interface, making it more readable
- It is used for any kind of SQL queries which are to be executed multiple times
- The performance of this interface is better than the Statement interface (when used for multiple execution of same query)
- It is precompiled by the JVM so the database doesn’t have to compile the SQL each and every time it is executed
- It will properly escape reserved characters to prevent SQL injection attacks
CallableStatement
- It is used to call the stored procedures
- It is preferred when the stored procedures are to be executed
- You can pass 3 types of parameters using this interface (In, Out, In Out)
- It is used to execute stored procedures and functions
- The performance of this interface is high
What is Java Garbage Collection?
Garbage are unreferenced objects. Garbage Collection is process of reclaiming the runtime unused memory automatically, so Java provides better memory management. It only collections objects that are created by the new keyword. The finalize method is used to perform cleanup processing (destroying remaining objects). You can use the gc() method to invoke garbage collector . Neither finalization nor garbage collection is guaranteed.
What is the final, finalize(), and finally
Final
- Final is used to apply restrictions on class, method, and variable
- Final class can’t be inherited
- Final method can’t be overridden
- Final variable value can’t be changed
- Final is a keyword
Finally
- Finally is used to place important code
- It will be executed whether exception is handled or not
- Finally is a block
Finalize
- Finalize is used to perform clean up processing just before object is garbage collected
- Finalize is a method