Core Java Interview Questions – Set 19

What is a stream and what are the types of Streams and classes of the Streams

A Stream is an abstraction that either produces or consumes information. There are two types of Streams and they are: Byte Streams: Provide a convenient means for handling input and output of bytes. Character Streams: Provide a convenient means for handling input & output of characters. Byte Streams classes: Are defined by using two abstract classes, namely InputStream and OutputStream. Character Streams classes: Are defined by using two abstract classes, namely Reader and Writer.

What is stored procedure?-

?– Stored procedure is a group of SQL statements that forms a logical unit and performs a particular task. Stored Procedures are used to encapsulate a set of operations or queries to execute on database. Stored procedures can be compiled and executed with different parameters and results and may have any combination of input/output parameters.

What method should the key class of Hashmap override

The methods to override are equals() and hashCode().

When does Java read values of Classpath environment variable?

Java uses the CLASSPATH environment variable to read the classes and libraries from file system. This variable is used by all JDK Tools and Extension including Java Compiler (javac) and JRE(java) use this variable to locate the dependent user classes and jar files to perform specific tasks.

Java Compiler uses it to locate the dependent user classes and jar files to compile Java source files.

Java Run-time Environment (JRE) uses the classpath variable to identify the location of files to be loaded for the run-time dependencies (e.g. classes and jar files) of java program.

Where will you use Hashtable and where will you use HashMap

  • of possible future changes.

What is static initializer code

: A class can have a block of initializer code that is simply surrounded by curly

braces and labeled as static e.g.

Code:

public class Demo{

static int =10;

static{

System.out.println(�Hello world�);

}

}

And this code is executed exactly once at the time of class load

How to find which jar file is being used by Java run-time

On Windows

You need use below windows program Process Explorer

that lets you see which files are open for a particular process or program

On Unix, Linux or Mac

It can be done using lsof command. lsof is one of my favorite and useful java debugging commands on Unix

. Below is the syntax for using this command:

view plain

print

?

lsof -p [pid]

What is Connection pooling? What are the advantages of using a connection pool

Connection Pooling is a technique used for sharing the server resources among requested clients. It was pioneered by database vendors to allow multiple clients to share a cached set of connection objects that provides access to a database.
Getting connection and disconnecting are costly operation, which affects the application performance, so we should avoid creating multiple connection during multiple database interactions. A pool contains set of Database connections which are already connected, and any client who wants to use it can take it from pool and when done with using it can be returned back to the pool.

Apart from performance this also saves you resources as there may be limited database connections available for your application.

How to convert a string array to arraylist

new ArrayList(Arrays.asList(myArray))

How to set Java Classpath on Windows, Unix, Linux and Mac

Setting CLASSPATH on Windows XP

  • Right-click My Computer, and then click Properties.
  • Click the Advanced tab.
  • Click Environment variables.
  • Click one the following options, for either a user or a system variable
  • Click New to add a new variable name and value.
  • Enter Variable name as CLASSPATH
  • enter all directories and jar files separated by semicolon. (e.g. c:dir1;c:dir2;c:dir3abc.jar)

Setting CLASSPATH on Windows 7

  • Click Start
  • Then right-click on Computer,
  • select Properties
  • click Select Advanced System Settings tab.
  • click the Environment Variables button.

Setting CLASSPATH on Unix, Linux and Mac

  • Use export command to set the CLASSPATH environment variable in your system.
  • ExportCLASSPATH=/path/to/dir1:/path/to/dir2:path/to/abc.jar