Friday, 19 September 2014

Capgemini Interview Questions and Answers for Java 3-8 Year Experience

Experience Java/J2ee interview Questions asked by Capgemini.

1. Tell me about yourself ?

Ans : Give your brief introduction.

2. Explain about your current project?

Ans : It’s quite easy to describe your projects and your key role on this project. But, be careful and get ready about the functionality when you are describing your working module / part of the project. Show your confidence that you have done the major part and you can face challenges in future.

3. How many types of literals are there in JAVA ?

Ans : The literals means the value you are assigning to variable. You can specify the below types of literal in java.As per the primitive data types(int,short,long,float,double,boolean,char ,etc there is respective literal. Some literal needs to be ended with a specific character.Read More.

           long var=20L; //specify L or l for long literal
int var=20; // If not mentioned any character then its can be short or int
char var=’A’;
float var=10.44f; //specify f or F for float literal
double var=10.44;
boolean var=true; //or false

4. What is meant by Garbage collection ?

Ans :  Garbage collection is a automatic feature of java for cleaning the unused object from heap. It helps to developer for releasing the reserved memory without any extra effort by developer. It helps developer to save time and extra mental tension for handling object allocation in memory. When there is no reference to an object found, it will clean that object from memory . You can run the garbage collection explicitly by using System.gc() .    

5. Diffference between string s= new string (); and string s = "Hi Dude"; ?

Ans : Both statements are different to each other. Always ‘new’ keyword is used to create object.

String s=new String(); // This statement creates new object in heap. S is the object here.
String s=”Hi Dude” ; // This statement do not create object, its creating reference and its storing in String Constant Pool.S is the reference here.

6. What is singleton class? where is it used ?

7. What is the difference between JSP and Servlets ?

Ans : As simple JSP is pre-compiled but Servlets are not. JSP is specially use for displaying/populating the data on browser. If we take an example of MVC architecture JSP plays the role of View (V). But, Servlets are used to handle the request and process the business logic. In MVC architecture Servlets are knows as Controller (C) .

8. What is the difference in using request.getRequestDispatcher() and context.getRequestDispatcher()?

Ans : Both are taking String parameter, but request.getRequestDispatcher() will dispatch the request inside the application. Where as context.getRequestDispatcher() will dispatch the request outside the context also. If you are using absolute path for dispatching then both are similar.

9. How the JSP file will be executed on the Server side ?

Ans : First its converted to java file. Then its compiled  by java compiler and creates the .class file.Now once you get the .class file you can execute it. Internally a JSP converts into respective servlet.

Conversion => Compilation => Execution

10. What is ActionServlet ?

Ans : ActionServlet provides the controller in struts application with MVC (Model-View-Controller) Model 2. 
ActionServlet is a sub class of javax.servlet.http.HttpServlet.It has few methods like

11. What is Struts Validator Framework ?
Ans : Struts provides a convenient way to validate. Basically we are using two xml configuration file for configuring such as Validator.xml and validation-rule.xml . validation-rule.xml defines the rule of validation like number format validation,email validation. Apart from this ActionForm having validate() method which we can implement for validating the form data.

12. What is the difference between the Session and SessionFactory in hibernate ?

Ans : Session and SessionFactory are playing a prominent role in hibernate. SessionFactory is used to create Session  and its created once during starting of the application. You can have only one SessionFactory per application. SessionFactory is also called 2ndlevel cache. But, Session could be many per application. Session is being created by using SessionFactory object.Session is called 1st level cache.

13. What is HQL ? 

Ans : HQL stands for Hibernate Query Language. Its fully object oriented and quite similar with SQL.It supports association and joins for effective entity relationship.

Wednesday, 17 September 2014

STUCK Thread in weblogic server - Hogging Thread

 As we all know facing issue with you job is casual. But, its too difficult, if you stuck somewhere and eat your own head and goooooogle all expected sites for solutions. Such kind of issues which you don't know what might be the root cause for this issue. For any issue we need application logs and if logs are not sufficient then you might go for bit deep level like thread dump.

Hogging thread in weblogic server is most often issue, but there is no such direct solution you found from internet.

What is a Hogging Thead ?

Hogging thread is thread which never back or never release the resource. Its a STUCK thread. When a thread starts an execution, it should return or complete the task and release the thread.But, hogging thread occurs when a thread wait for more than StuckThreadMaxTime.The normal time setting for StuckThreadMaxTime is 600sec.When a thread wait more than StuckThreadMaxTime , its known as hogging thread. 

 I faced such hogging thread issue in my project ,when support team reported that there are many hogging threads on server. From that time , it started eating the head.Below error log shows my issue.

Error log :-
<11/08/2014 4:50:00 PM EST> <Error> <WebLogicServer> <BEA-000337> <[STUCK] ExecuteThread: '452' for queue: 'weblogic.kernel.Default (self-tuning)' has been busy for "697" seconds working on the request "Workmanager: default, Version: 1, Scheduled=true, Started=true, Started time: 697198 ms
POST /centre_service/CentreService HTTP/1.1
Content-Type: text/xml; charset=utf-8
SOAPAction: ""
Transfer-Encoding: chunked
User-Agent: Java1.6.0_71
Accept: text/html, image/gif, image/jpeg, */*; q=.2
Connection: Keep-Alive

]", which is more than the configured time (StuckThreadMaxTime) of "600" seconds. Stack trace:
Thread-87420 "[STUCK] ExecuteThread: '452' for queue: 'weblogic.kernel.Default (self-tuning)'" <alive, suspended, blocked, priority=1, DAEMON> {
    -- Blocked trying to get lock: weblogic.jdbc.common.internal.GenericConnectionPool@23cd753b[thin lock]
    com.sun.proxy.$Proxy235.getCustomerByAccountNameList(Unknown Source)
    sun.reflect.GeneratedMethodAccessor1108.invoke(Unknown Source)
    ^-- Holding lock:[thin lock]$2.process($HttpToolkit.handle(
After lot of analysis I got few points , where I can stand and try for any possible solution.As per my investigation there are many reason behind hogging thread.

Possible cause for hogging thread :-

  1. Network issue
  2. Application issue (If connections are not closed properly)
  3. Dependent service has problem (upstream and downstream )
  4. Heavy Server Load

Possible Solution :-

If your application is OK, then you can check other possible key point for this issue. In my issue , we are working on OSB (Oracle Service Bus) . Normally its related to working with proxy service and business service.

1. First and initial solution is check the Connection timeout and Read timeout  for business service. Whether those settings are fine or not.If possible configured that to as expected standard time is 30 sec .You can configure this more than 30sec.
2. As second solutions , you need to create a separate work manager for per each thread.By default my proxy was using default work manager. I applied with separate work manager and it worked.

Hope it will help you.

Follow for more details on Google+ and @Facebook!!!

Find More :-

Sunday, 14 September 2014

Many-to-One relationship in hibernate - @ManyToOne Example

As we all know Hibernate is an easy and fast framework for work with any Database using Java language. This example show how to work with Many-To-One relationship in Hibernate.

I have used MySQL as database (Download Here) and Hibernate 3 jar files (Download Here). Also for applying Many-To-One relation we have used annotation. Even you can do this by using .xml config also, but that's too old and no one is using that now-a-days.

Create table department and employee :-

 Table department :
CREATE TABLE `department` (
  `deptid` int(11) NOT NULL,
  `deptname` varchar(100) default NULL,
  PRIMARY KEY  (`deptid`)

Table employee :
CREATE TABLE `employee` (
  `empid` int(11) NOT NULL,
  `empname` varchar(50) default NULL,
  `deptno` int(11) default NULL,
  PRIMARY KEY  (`empid`),
  KEY `deptno` (`deptno`),
  CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`deptno`) REFERENCES `department` (`deptid`)
In this above both table , many employee could be from one department and implements Many-to-One relationship.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        <!-- Adding mysql dialect -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
           <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <!-- Here jdeveloperguidedb is the database name -->
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/jdeveloperguidedb</property>
           <property name="hibernate.connection.username">root</property>
           <property name="hibernate.connection.password">root</property>
           <!-- show_sql property will help you to show the generated hibernate query on console -->          
           <property name="show_sql" >true</property> 
           <!-- Mapping entity class to hibernate -->
           <mapping class="com.jdeveloperguide.lab.Employee"></mapping> 
           <mapping class="com.jdeveloperguide.lab.Department"></mapping> 

In this hibernate.cnf.xml file we have configure the initial setup for db , and it will help us to interact with db using hibernate.

package com.jdeveloperguide.lab;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

public class Department {

    private int deptid;
    private String deptname;
    public int getDeptid() {
        return deptid;
    public void setDeptid(int deptid) {
        this.deptid = deptid;
    public String getDeptname() {
        return deptname;
    public void setDeptname(String deptname) {
        this.deptname = deptname;
Here is the entity class which we are going to persist or save into db.We have used few annotation here like @Entity ,@Table , @Id , @Column , etc. This entity class looks like a helper class.

@Entity - This annotation will help you to define entity in RDBMS.

@Table - This annotation will help you to define a database table (entity) by specifying name.

 i.e. @Table(name="department").

@Id - This annotation will help you to define the id column , it means the primary key.

@Column - This annotation will help you to define the column name by specifying the column name. If your column name and java variable name are different the you can specify by name. In this example our database column name and variable name are same so, not required specify the column name. Hibernate will automatically match and take care this. But I have used because for better understanding.

package com.jdeveloperguide.lab;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

public class Employee {

private int empid;

private String empname;

private Department dept;

public int getEmpid() {
    return empid;

public void setEmpid(int empid) {
    this.empid = empid;

public String getEmpname() {
    return empname;

public void setEmpname(String empname) {
    this.empname = empname;

public Department getDept() {
    return dept;

public void setDept(Department dept) {
    this.dept = dept;

 Here is the entity class which we are going to persist or save into db along with Department.We have used few more new annotations here like @ManyToOne , @JoinColumn, etc.
Here we have mapped many-to-one , means many employee from one department.

@ManyToOne - Mapping many-to-one relation with hibernate.

@JoinColumn - This annotation is used for map the reference foreign key for any entity. This indicates the association with entity/table.

package com.jdeveloperguide.lab;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class CreateSessionFactory {
    //Single point of access for SessionFactory
    private static final SessionFactory sessionFactory;
    static {
        try {
            //create sesson factory using the config file
            sessionFactory = new AnnotationConfiguration().configure("hibernate.cnf.xml").buildSessionFactory();
        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
    //Static method for exposing the session
    public static SessionFactory getSessionFactory() {
        return sessionFactory;

This class will help to create the SessionFactory object using .xml config file. This class having static method with we can call from any part of this application for getting SessionFactory object.Because , we will create Session object from SessionFactory

package com.jdeveloperguide.lab;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class MainClass {

     * @param args
    public static void main(String[] args)throws Exception {
        MainClass mainObject=new MainClass();;
        Employee employee=new Employee();
        Department department=new Department();
        department.setDeptname("Computer Science");
        employee.setEmpname("Mr. JDeveloper");
    public void saveEmployee(Employee employee)throws Exception{
        Transaction transaction=null;
        Session session=null;
        //Create and open session
        session = CreateSessionFactory.getSessionFactory().openSession();
        //Create Transaction for maintain a user session
        //Save the employee
        //Also it will save department ,because we have used cascade=CascadeType.ALL in employee;
        }catch(HibernateException hibernateException){
            System.out.println("Error during save into DB :::"+hibernateException);

 This is the starting point of this example. Here we will execute our hibernate Many-to-One example and it will persist the data into DB.

Hibernate Generated SQL :-

Hibernate: select department_.deptid, department_.deptname as deptname1_ from department department_ where department_.deptid=?
Hibernate: insert into department (deptname, deptid) values (?, ?)
Hibernate: insert into employee (empname, deptno, empid) values (?, ?, ?)
When we will execute the class , it will generate these above queries by hibernate and these are auto generated. Its generating and showing because, we have mentioned in .xml config file show_sql is true.

Result in DB :-

Department Table

Employee Table

Hope it will help you.