Core 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 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 is the difference between ArrayList and Vector?
Vector is synchronized whereas ArrayList is not.
Vector is considered as a legacy class in Java now; use ArrayList instead
Also Vector is not thread safe. Use CopyOnWriteArrayList type or synchronize ArrayList externally using the synchronizedList()
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?
An executable block ofPL/SQL code& stored in Oracle db.
Is called from a Java class using a CallableStatement object. When called, its name & any relevantparametersare sent over theJDBCconnection toDBMS, which executes the procedure & returns the results (if applicable) via 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.
What is the difference between static and final variables?
Static variable is a global variable shared by all the instances of objects and it has only single copy. A final variable is a constant variable and it cannot be changed.
What are the implicit modifiers required for interface variables?
public static final
What are transient variables?
Transient variables are those variables which cannot be serialized.
public – can be accessed from any package.
private – only members of the same class can access.
protected – can be accessed by classes inside the package and subclasses anywhere.
default - no access by classes or subclasses outside the package
What is a wrapper class?
Wrapper class is a wrapper around a primitive data type. It represents primitive data types in their corresponding class instances e.g. a boolean data type can be represented as a Boolean class instance. All of the primitive wrapper classes in Java are immutable i.e. once assigned a value to a wrapper class instance cannot be changed further.
What is Reflection API?
The first component of the Reflection API is the mechanism used to fetch information about a class. This mechanism is built into the class named Class. The special class Class is the universal type for the meta information that describes objects within the Java system. Class loaders in the Java system return objects of type Class. The three most interesting methods in this class were:
“forName, which would load a class of a given name, using the current class loader
getName, which would return the name of the class as a String object,which was useful for identifying object references by their class name
newInstance, which would invoke the null constructor on the class (if it exists) and return you an object instance of that class of object “
“To these three useful methods the Reflection API adds some additional methods to class Class. These are as follows:
getConstructor, getConstructors, getDeclaredConstructor
getMethod, getMethods, getDeclaredMethods
getField, getFields, getDeclaredFields
getSuperclass
getInterfaces
getDeclaredClasses”
Is Java pass by value or pass by reference?
Java is strictly pass by value.
In what ways can you create a thread?
By extending the Thread Class or by implementing the Runnable Interface
What is encapsulation?
Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled by an interface.
What is polymorphism?
Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class. An object can also be polymorphically referenced by its supertype “parent” class, for example ParentClass obj = new SubClass( );