SARAVANAN SIVAJI

Dedicated to JAVA Technologies

JSP life cycle steps

Posted by SARAVANAN SIVAJI on October 21, 2010

JSP life cylce steps

1.  Translate the JSP into a Servlet

2.  Compile Servlet source code

3.  Instantiate the servlet class

4.  Call jspInit()

5.  Call _jspService()

6.  Call jspDestroy()

Point to be noted:

You can override jspInit() and jspDestroy().

Note the underscoe at the front of the _jspService() method, it means, you must NOT try to override it.

Posted in com.saravananmtech.java | Leave a Comment »

JAVA J2EE interview questions

Posted by SARAVANAN SIVAJI on October 20, 2010

 

My JAVA/J2EE Interview Experience

 

1.      Architecture of my last project.  Few questions based on that.

 

2.      List<Object> obj = new ArrayList<String>();

Will the above compile?

Why polymorphism works for List and ArrayList, why not for generic types <Object> and <String> in the above line?

 

3.      What are the access specifiers?

 

4.      What is the difference between default and protected access specifiers?

 

 

5.      Can I access protected variable in the following code?

Package pack1;

 

Class A

{

Protected int variable = 10;

}

 

Package pack2;

Import pack1.*;

 

Class B extends A

{

 

Public void display()

{

A obj = new A();

System.out.println( obj.variable );  //Will this give output????

}

}

 

 

6.      Write an interface. Write an abstract class? Write a class which extends and implements the above two? Will the code compile. If yes, whether the display method implementation is applicable to interface sample1 or abstract class sample2.  If no, why it won’t compile?

 

interface sample1

{

void display();

}

 

absctract class sample2

{

abstract void display();

}

class A extends sample2 implements sample1

{

public void display()

{

System.out.println( “display method implementation” );

}

}

 

7.      Method Overriding rules? Do I need to take care anything about access specifiers while method overriding?

 

8.      From the following code,

 

import java.io.*;

Class A

{

protected void display() throws IOException

{}

}

 

Class B

{

public void display() throws Exception

{}

 

}

 

Will the above code compile? Justify?

9.      I have an Employee object with attributes like employee ID, employee name.  I have stored these employee objects in an ArrayList.  I want to display the arraylist of employees sorted by employee ID as well as employee name. How you will do that? (NOTE:  They asked me to write complete code.  They mainly focused on the logic)

 

10.  I have a class.  I want to execute the class without any method()?  Is it possible?

 

11.  From the following code,

 

Public void display()

{

——- int variable;

}

 

What are the access level specifiers can be declared with the above variables? (NOTE:  Explained the local variables rules)

 

12.  Can I serialize static variable?

 

13.  From the following code in Servlet

Map mymap = new HashMap();

Mymap.put( “1”, “a” );

Mymap.put( “2”, “b” );

Mymap.put( “3”, “c” );

Mymap.put( “4”, “d” );

Mymap.put( “5”, “e” );

Request.setAttribute( “mycollection”, mymap );

 

Write the JSP code to retrieve the elements and load it in a combo box.

 

14.  What is ServletContext and ServletConfig?

 

15.  How you declare servlet in deployment descriptior? (NOTE: I wrote the complete list of tags)

 

16.  What is load on startup? How you declare in deployment descriptor? What kind of value we need to mention there?

 

 

 

17.  From the following code,

 

Class A

{

int a = 10;

 

void display()

{

System.out.println( “class a” );

}

}

 

Class B extends A

{

Int a = 100;

 

Void display()

{

System.out.println( “class b” );

}

 

Public static void main( String [] args )

{

A aobj = new B();

Aobj.display();                                   //What is the output????? Why???

System.out.println( Aobj.a );           //What is the output????? Why???

}

}

 

 

 

18.  How you will the forward the request from Servlet to other web component?

 

19.  What is the difference between forward() and include() methods of ServletRequestDispatcher?

 

20.  From how many ways we can get the reference to ServletRequestDispatcher?

 

21.  What is the difference between getSession(false) and getSession() and getSession(true)?

 

22.  What are the implicit objects in JSP?

 

23.  How you will handle exception handling in JSP?

 

24.  Write the JAVA code to retrieve data from database?  (NOTE:  I wrote the complete JDBC code.  They are mainly focusing on how you are catching the exception)

 

25.  Write the code showing an example try, catch and finally?

 

26.  How you implemented the AJAX? What object we use?

 

27.  Write an SQL query to display the top 2 salary from the following table,

 

Table: employee

EmpID                        EmpName     Salary                         DepartmentID

1                      A                     1000                2

2                      B                      3000                1

3                      C                     2500                2

4                      D                     5000                3

5                      E                      3000                3

 

Table: department

 

deptID                       deptName

1                      production

2                      development

3                      testing

4                      management

 

28.  Write an SQL query to display the department ID of the employee having salary greater than 2500?

 

 

29.  Second Round – Asked questions from all the projects mentioned in the resume?  Questions like how you implemented? How it works?

 

 

Posted in com.saravananmtech.java | Leave a Comment »

Java 5.0 Features Part 2 – static imports

Posted by SARAVANAN SIVAJI on October 9, 2010

Posted in com.saravananmtech.java | 2 Comments »

Java 5.0 Feature Part 1: Autoboxing and Unboxing

Posted by SARAVANAN SIVAJI on October 9, 2010

The autoboxing feature added to Java 5.0 does the conversion from primitive to wrapper object automatically.

Posted in com.saravananmtech.java | Leave a Comment »

Static and Final in Java

Posted by SARAVANAN SIVAJI on October 9, 2010

 

Static in Java

  • A static method should be called using the class name rather than object reference variables.
  • A static method can be invoked without any instances of the method’s class on the heap.
  • A static method is good for a utility method that does not depend on a particular instance variable value.
  • A static method is not associated with a particular instance – only the class = so it cannot access any instance variable values of its class.  It wouldn’t know which instance’s values to use.
  • A static method cannot access a non-static method, since non-static methods are usually associated with instance variable state.
  • If you have a class with only static methods and you do not want the class to be instantiated you can mark the constructor private.
  • A static variable is a variable shared by all members of a given class.  There is only one copy of a static variable in a class, rather than one copy per each individual instance for instance variables.
  • A static method can access a static variable
  • To make a constant in java, mark a variable as both static and final
  • A final static variable must be assigned a value either at the time it is declared, or in a static initializer

 

  • The naming convention for constants is to make the name all uppercase.
  • A final variable value cannot be changed once it has been assigned.
  • Assigning a value to a final instance variable must be either at the time it is declared, or in the constructor.
  • A final method cannot be overridden.
  • A final class cannot be extended (subclassed)

Posted in com.saravananmtech.java | Leave a Comment »

When to make a class, a subclass, an abstract class, or an interface?

Posted by SARAVANAN SIVAJI on October 6, 2010

Make a class that does not extend anything (other than Object) when your new class does not pass the IS-A test for any other type.

Make a subclass only when you need to make a more specific version of a class and need to override or add new behaviors.

Use an abstract class when you want to define a template for a group of subclasses, and you have at least some implementation code that all subclasses could use.  Make the class abstract when you want to guarantee that nobody can make objects of that type.

Use an interface when you want to define a role that other classes can play, regardless of where those classes are in the inheritance tree.

Posted in com.saravananmtech.java | Leave a Comment »

Java J2EE Interview Questions

Posted by SARAVANAN SIVAJI on September 27, 2010

Recently I have attend the interview in one of the reputed MNC company for JAVA/J2EE technologies.  I tried my level best to recollect all interview questions that I have faced are listed here:

JAVA/J2EE Interview questions

  1. What are the features newly introduced in JAVA SE 5.0
  2. What is the necessity of Generic Programming?
  3. Syntax of Generic Class and Methods declaration
  4. Give collection hierarchy
  5. Which collections are sorted.
  6. Difference between Arraylist and Vector
  7. Difference between Collection and Collections
  8. If I want to sort ArrayList, what we need to do.
  9. Difference between Comparable and Comparator.
  10. What compareTo() method will return and explain each returned data.
  11. Difference between checked and unchecked exception
  12. Hierarchy of Exceptions
  13. Do we need to catch Error? Difference between Error and Exception
  14. List some checked and unchecked exception
  15. Do we need to make sure anything when we write try-catch blocks
  16. Will the statements after catch block run when after an exception was raised in try block.
  17. When finally block executes
  18. What is the use of throws keyword
  19. What are the methods to create thread?  Which is best method?
  20. Does the Thread class implements Runnable interface.
  21. When we implement Runnable interface, which method do we need to implement.
  22. How you will start the thread for execution.
  23. What will happen when you call start () method.  Can I start thread without start () method?
  24. What will happen when we explicitly call run() method to start execution without start() method
  25. Difference between sleep() and wait() methods
  26. When sleep time expired, will the thread enters runnable state or running state.
  27. Difference between notify() and notifyall() methods
  28. Difference between abstract and interface
  29. When we go for abstract and when we go for interface.
  30. How do you implement synchronization?  What are the methods?
  31. What is polymorphism?  How do you implement it?
  32. Difference between overriding and overloading?
  33. Do return type should be same when we implement method overriding
  34. Can I override Static methods?  Are you sure?
  35. Default access specifiers of method declarations in Interface
  36. Default access specifiers of variable declarations in Interface
  37. Can I declare abstract method in non-abstract class

Continues….

34.

Class A {

void display()

{

S.o.p( “class A” );

}

}

Class B extends A

{

void display()

{

S.o.p (“Class B” );

}

}

A obj = new B();

Obj.display();                        //what is the output of this line.

————————————————————————-

Class A {

Static void display()

{

S.o.p( “class A” );

}

}

Class B extends A

{

Static void display()

{

S.o.p (“Class B” );

}

}

A obj = new B();

Obj.display();                        //Are you sure what is the output of this line.  Why??

Class A {

Static int I = 10;

}

Class B extends A

{

Static int I = 30;

}

A obj = new B();

System.out.println(Obj.I);                   //what is the output of this line.

——————————————————-

  1. When garbage collector will run?
  2. Can we call garbage collector? What is the method to call the same?
  3. What is serialization?  How do you implement it?
  4. What happens when serializable object has reference to another object as instance variable?
  5. Difference between private, protected and public access specifiers.
  6. Can I declare static class?
  7. How do you create object for inner class?

J2EE

  1. Lifecycle of Servlet
  2. What is the purpose of init() method.  We can use the servlet constructor for initialization right?  Why do we need init() method still?
  3. Difference between ServletConfig and ServletContext?
  4. Where do you implement ServletConfig and ServletContext parameters?
  5. Do we need to override Service() method while writing Servlet class
  6. Difference between RequestDispatcher and SendRedirect
  7. Can I forward the request if I have already commited the response.  What exception will throw if we already commited a response and forwards the request?
  8. Having Servlet classes, can I use Java Swing instead of JSP in presentation layer?  How do you do that?
  9. How do you create session?
  10. What is the difference between getSession(true), getSession() and  getSession(false)?
  11. How do you declare session timeout in the Deployment Descriptor?

Continues…

  1. What is the difference between GET and POST methods?  What happen If I didn’t declare method attribute in <form> tag?
  2. What are the other HTTP methods?  Purpose of TRACE method?
  3. What is the use of setContentType() method?
  4. How may ways to get reference to ServletContext object?
  5. What is the purpose of ServletContextListener? Give some example when we can use this?
  6. What is the purpose of HttpSessionAttributeListener?  What is the event type of this listener?
  7. Purpose of SingleThreadModel interface?
  8. How does the web container guarantee a servlet gets only one request at a time?
  9. Life cycle of JSP?
  10. List of implicit objects in JSP?
  11. What is the use of application object?  What is the API of out object?
  12. How do import packages in JSP?
  13. How to use <jsp:useBean>….purpose…how to set and get property..?
  14. How do you implement exception handling in JSP?
  15. Difference between include directive and <jsp:include>
  16. What is difference between Statement and Prepared Statement?
  17. What is the use of Callable Statement?
  18. What is inner join?
  19. How do you implement AJAX concept in JavaScript?

Posted in com.saravananmtech.java | Leave a Comment »

What is Platform Neutral?

Posted by SARAVANAN SIVAJI on September 15, 2010

Java programming language - This language is platform neutral which means it should run on any machine that has a compatible virtual machine. Essentially this means it should run on all versions of Windows, most Linux machines and most / all Sun machines. As far as we know it should run on later Apple Macs (OS X) and BSD but both will require a 1.4 virtual machine and a little tweaking probably

NOTE : Due to the platform neutral nature of the software there may be slight variations in the exact look and feel of the software on different platforms.


Ok so what is a platform and what’s this Virtual Machine thingy?

A platform would typically be defined as a hardware and operating system combination for instance one platform would be i386 / Windows 2000 (Intel i386 compatible hardware running the Windows 2000 operating system) another platform might be i386 / Linux (Again Intel i386 compatible hardware but this time running a flavour of Linux as the operating system).

There are hundreds of possible combinations of hardware and software each producing a different platform. To write software that works on all those different platforms (as does so equally well) is a difficult task. Fortunately Sun Microsystems took most of the work out of that by developing the Java language which is platform neutral or independent of the hardware and software combination that it runs on.

Now that sounds great but it does have a down side. All those different platforms use different instructions to achieve the same goals (eg displaying pictures on the screen). Java being neutral has to be able to “speak” all the different languages so that it can communicate with the underlying machine. This communication is handled by another piece of software called a virtual machine (VM). It takes the instructions in the software we wrote and converts them to instructions the machine understands.

The biggest problem with having to use the VM is that it slows down program execution. You may notice (especially on older machines) that things can take a little longer than you would normally expect. Refas uses the latest VM (1.4) which has made may improvements in speed an most people will find it fast enough. We have tested it with 7500 references and it still runs fast on an 800MHz system.

source: http://www.crazysquirrel.com/

Posted in com.saravananmtech.java | Leave a Comment »

Time Count Down in JAVA

Posted by SARAVANAN SIVAJI on August 14, 2010

Posted in com.saravananmtech.java | Leave a Comment »

Java Interview Questions – Part 1

Posted by SARAVANAN SIVAJI on April 16, 2010

Q1: How could Java classes direct program messages to the system console, but error messages, say to a file?

Ans. The class System has a variable out that represents the standard output, and the variable err that represents the standard error device. By default, they both point at the system console. This how the standard output could be re-directed:
Stream st = new Stream(new FileOutputStream(“output.txt”)); System.setErr(st); System.setOut(st);

Q2. What’s the difference between an interface and an abstract class?

A. An abstract class may contain code in method bodies, which is not allowed in an interface. With abstract classes, you have to inherit your class from it and Java does not allow multiple inheritance. On the other hand, you can implement multiple interfaces in your class.

Q3. Why would you use a synchronized block vs. synchronized method?

A. Synchronized blocks place locks for shorter periods than synchronized methods.

Q4. Explain the usage of the keyword transient?

A. This keyword indicates that the value of this member variable does not have to be serialized with the object. When the class will be de-serialized, this variable will be initialized with a default value of its data type (i.e. zero for integers).

Q5. How can you force garbage collection?

A. You can’t force GC, but could request it by calling System.gc(). JVM does not guarantee that GC will be started immediately.

Q6. How do you know if an explicit object casting is needed?

A. If you assign a superclass object to a variable of a subclass’s data type, you need to do explicit casting. For example:

Object a; Customer b; b = (Customer) a;

When you assign a subclass to a variable having a supeclass type, the casting is performed automatically.

Q7. What’s the difference between the methods sleep() and wait()

A. The code sleep(1000); puts thread aside for exactly one second. The codewait(1000), causes a wait of up to one second. A thread could stop waiting earlier if it receives the notify() or notifyAll() call. The method wait() is defined in the class Object and the method sleep() is defined in the class Thread.

Q8. Can you write a Java class that could be used both as an applet as well as an application?

A. Yes. Add a main() method to the applet.

Q9. What’s the difference between constructors and other methods?

A. Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times.

Q10. Can you call one constructor from another if a class has multiple constructors

A. Yes. Use this() syntax.

Q11. Explain the usage of Java packages.

A. This is a way to organize files when a project consists of multiple modules. It also helps resolve naming conflicts when different packages have classes with the same names. Packages access level also allows you to protect data from being used by the non-authorized classes.

Q12. If a class is located in a package, what do you need to change in the OS environment to be able to use it?

A. You need to add a directory or a jar file that contains the package directories to the CLASSPATH environment variable. Let’s say a class Employee belongs to a package com.xyz.hr; and is located in the file c:\dev\com\xyz\hr\Employee.java. In this case, you’d need to add c:\dev to the variable CLASSPATH. If this class contains the method main(), you could test it from a command prompt window as follows:

c:\>java com.xyz.hr.Employee

Q13. What’s the difference between J2SDK 1.5 and J2SDK 5.0?

A.There’s no difference, Sun Microsystems just re-branded this version.

Q14. What would you use to compare two String variables – the operator == or the method equals()?
A. I’d use the method equals() to compare the values of the Strings and the == to check if two variables point at the same instance of a String object.

Q15. Does it matter in what order catch statements for FileNotFoundException and IOExceptipon are written?
A. Yes, it does. The FileNoFoundException is inherited from the IOException. Exception’s subclasses have to be caught first.

Posted in com.saravananmtech.java | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.