SARAVANAN SIVAJI

Dedicated to JAVA Technologies

Archive for October, 2010

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 »

 
Follow

Get every new post delivered to your Inbox.