Java Flashcards
What is the difference between an interface and an abstract class?
An abstract class can have both concrete and abstract methods whereas an interface must have only abstract methods if any.
Can you instantiate an abstract class? an interface?
No and No. Abstact classes are made to be extended only.
What is the difference between ArrayList and Vector?
Vector is synchronized whereas ArrayList is not.
What is the difference between Treeset and HashSet?
The two general purpose Set implementations are HashSet and TreeSet. HashSet is much faster (constant time versus log time for most operations) but offers no ordering guarantees.
What type of Collection would not allow duplicate elements?
Set.
What is the difference between Hashtable and Hashmap?
a. Hashtable is synchronized whereas Hashmap is not
b. Hashmap permits null values and the null key.
How do you serialize (persist) an object in Java?
a. Step 1: An object is marked serializable by implementing the java.io.Serializable interface, which signifies to the underlying API that the object can be flattened into bytes and subsequently inflated in the future.
b. Step 2: The next step is to actually persist the object. That is done with the java.io.ObjectOutputStream class. That class is a filter stream–it is wrapped around a lower-level byte stream (called a node stream) to handle the serialization protocol for us. Node streams can be used to write to file systems or even across sockets. That means we could easily transfer a flattened object across a network wire and have it be rebuilt on the other side!
c. To restore the object back, you use ObjectInputStream.readObject() method call. The method call reads in the raw bytes that we previously persisted and creates a live object that is an exact replica of the original. Because readObject () can read any serializable object, a cast to the correct type is required. With that in mind, the class file must be accessible from the system in which the restoration occurs. In other words, the object’s class file and methods are not saved; only the object’s 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 a 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 results (if applicable) via the connection.
What is the difference between Statement and PreparedStatement?
PreparedStatements are pre-compiled by the JVM. The database doesn’t have to compile the SQL each and every time it is executed. PreparedStatement can be parameterized, which can make the SQL more readable. Furthermore, PreparedStatement will properly escape reserved characters to prevent SQL injection attacks.
How can you force Garbage Collection?
Garbage collection cannot be forced but only requested using System.gc().
What is final, finalize() and finally?
a. final: final keyword can be used for class, method and variables. A final class cannot be subclassed and it prevents other programmers from subclassing a secure class to invoke insecure methods. A final method can’t be overridden. A final variable can’t change from its initialized value.
b. finalize(): finalize method is used just before an object is destroyed and called just prior to garbage collection.
c. finally: finally, a key word used in exception handling, creates a block of code that will be executed after a try/catch block has completed and before the code following the try/catch block. The finally block will execute whether or not an exception is thrown. For example, if a method opens a file upon exit, then you will not want the code that closes the file to be bypassed by the exception-handling mechanism. This finally keyword is designed to address this contingency.
What is a Marker Interface?
A marker interface is an interface which has no methods at all. Example: Serializable, Remote, Cloneable. Generally, they are used to give additional information about the behavior of a class.
How would you clone an object?
First, tag the class with the Cloneable marker interface. Next, invoke clone (). The clone method is declared in java.lang.Object and does a shallow copy.
Why are strings immutable in java?
Identical String literals are collected in the “String pool” in an effort to conserve memory. Reference variables will then point to the same String object instance. Changing the object’s state in the String pool will make changes to all references to that String object. Instead, when a change to a String is made, the JVM makes a new String object, and the reference variable points to the new String in the String pool.
What is the difference between String, StringBuilder, and StringBuffer?
Strings are immutable. Both StringBuilder and StringBuffer are mutable. Furthermore, StringBuffer is sychronized while StringBuilder is not.