Java Flashcards

1
Q

What is the difference between an interface and an abstract class?

A

An abstract class can have both concrete and abstract methods whereas an interface must have only abstract methods if any.

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

Can you instantiate an abstract class? an interface?

A

No and No. Abstact classes are made to be extended only.

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

What is the difference between ArrayList and Vector?

A

Vector is synchronized whereas ArrayList is not.

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

What is the difference between Treeset and HashSet?

A

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.

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

What type of Collection would not allow duplicate elements?

A

Set.

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

What is the difference between Hashtable and Hashmap?

A

a. Hashtable is synchronized whereas Hashmap is not

b. Hashmap permits null values and the null key.

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

How do you serialize (persist) an object in Java?

A

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.

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

What is a stored procedure and how would you call it in Java?

A

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.

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

What is the difference between Statement and PreparedStatement?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How can you force Garbage Collection?

A

Garbage collection cannot be forced but only requested using System.gc().

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

What is final, finalize() and finally?

A

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.

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

What is a Marker Interface?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How would you clone an object?

A

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.

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

Why are strings immutable in java?

A

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.

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

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

A

Strings are immutable. Both StringBuilder and StringBuffer are mutable. Furthermore, StringBuffer is sychronized while StringBuilder is not.

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