Sunday, May 29, 2011

Java interview questions (1)

1. What is the purpose of serialization?

Answer: Serialization is the conversion of an object to a series of bytes, so that the object can be easily saved to persistent storage or streamed across a communication link. The byte stream can then be deserialised – converted into a replica of the original object.

2. What is the difference between JDK and JRE?

Answer: Java Development Kit (JDK) is the most widely used Java Software Development Kit. Java Runtime Environment (JRE) is an implementation of the Java Virtual Machine which executes Java programs.

3. What is the difference between equals() and “==” ?

Answer: Equals is intended to check logical equality and == checks if both references point to same object.

4.When will you use Comparator and Comparable interfaces?

Answer: java.util.Comparator and java.lang.Comparable
java.util.Comparator compares some other class’s instances, while java.lang.Comparable compares itself with another object.

5. What is the wait/notify mechanism?

Answer: This deals with concurrent programming. The wait() and notify() methods are designed to provide a mechanism to allow a thread to be block until a specific condition is met. However, java.util.concurrent should be used instead of wait() and notify() to reduce complexity.

6. What is the difference between checked and unchecked exceptions?

Answer: In general, unchecked exceptions represent defects in the program (bugs), which are normally Runtime exceptions.
Furthermore, checked exceptions represent invalid conditions in areas outside the immediate control of the program.

7. What is the difference between final, finally and finalize?

Answer: “final” is the keyword to declare a constant AND prevents a class from producing subclasses. (Thanks Tom Ellis)
“finally” is a block of code that always executes when the try block is finished, unless System.exit() was called. finalize() is an method that is invoked before an object is discarded by the garbage collector

8. What is the difference between web server and app server?

Answer: A Web server exclusively handles HTTP requests, whereas an application server serves business logic to application programs through any number of protocols.

9. Explain the Struts1/Struts2/MVC application architecture?

Answer: Struts was adopted by the Java developer community as a default web framework for developing web applications
The MVC(Model–view–controller) an application that consist of three distinct parts. The problem domain is represented by the Model. The output to the user is represented by the View. And, the input from the user is represented by Controller.

10. What is the difference between forward and send redirect?

Answer: Both method calls redirect you to new resource/page/servlet. The difference between the two is that sendRedirect always sends a header back to the client/browser, containing the data in which you wanted to be redirected.

11. How does a 3 tier application differ from a 2 tier one?

Answer: Tiers are the physical units of separation or deployment, while layers are the logical units of separation.
Imagine that you’re designing an e-commerce website. A 3 tier architecture would consist of web pages, a web server and a database, with the corresponding 3 layers being the “Presentation”, “Business Logic” and “Database” layers.
If you take the database tier and layer out then your have a 2 tier architecture.

12. How does the version control process works?

Answer: Initiate, pull, branch, merge, commit, push.
(Init) Make your own repository. (Pull) Download an existing repository from a url. (Branch / Merge )Make revisions. Commit then push your modifications.

13. What is the difference between JAR and WAR files?

Answer: JAR files (Java ARchive) allows aggregating many files into one, it is usually used to hold Java classes in a library.
WAR files (Web Application aRchive) stores XML, java classes, and JavaServer pages for Web Application purposes.

14. What is a Left outer join?

Answer: This deals with SQL. Left outer join preserves the unmatched rows from the first (left) table, joining them with a NULL row in the shape of the second (right) table.

15. What is the difference between UNION and UNION ALL?

Answer: This deals with SQL. UNION only selects distinct values, UNION ALL selects all values.

Wednesday, August 25, 2010

Get the current working directory in Java

The current working directory is stored in the system property "user.dir". The following example shows how to read this system property from your application:
public class UserDirectoryTest 
{     public static void main(String[] args) 
    {         System.out.println("Working Directory = " +            System.getProperty("user.dir"));     } } 
Cheers,
Vinay

Friday, June 18, 2010

Executable Jars

Languages like .NET and C++ have historically had the advantage of being OS-friendly, in that simply referencing their name at the command-line (helloWorld.exe) or double-clicking their icon in the GUI shell would launch the application. In Java programming, however, a launcher application — java — bootstraps the JVM into the process, and we have to pass a command-line argument (com.tedneward.Hello) indicating the class whose main() method we want to launch.

These additional steps make it harder to create user-friendly applications in Java. Not only does the end user have to type all of these elements at the command-line, which many end users would rather avoid, but chances are good that he or she will somehow fat-finger it and get an obscure error back.

The solution is to make the JAR file "executable" so that the Java launcher will automatically know which class to launch when executing the JAR file. All we have to do is introduce an entry into the JAR file's manifest (MANIFEST.MF in the JAR's META-INF subdirectory), like so:


Listing 2. Show me the entrypoint!
Main-Class: com.tedneward.jars.Hello



The manifest is just a set of name-value pairs. Because the manifest can sometimes be touchy about carriage returns and whitespace, it's easiest to use Ant to generate it when building the JAR. In Listing 3, I've used the manifest element of the Ant jar task to specify the manifest:


Listing 3. Build me the entrypoint!












All a user has to do to execute the JAR file now is specify its filename on the command-line, via java -jar outapp.jar. In the case of some GUI shells, double-clicking the JAR file works just as well.