Apr 30

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder"

SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread “main” java.lang.NoClassDefFoundError: javax/persistence/EntityListeners
at org.hibernate.cfg.annotations.reflection.JPAMetadataProvider.getDefaults(JPAMetadataProvider.java:96)
at org.hibernate.annotations.common.reflection.java.JavaReflectionManager.getDefaults(JavaReflectionManager.java:226)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1355)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1826)
at employee.EmpTest.main(EmpTest.java:10)
Caused by: java.lang.ClassNotFoundException: javax.persistence.EntityListeners
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
… 5 more 

You will get this exception mostly when you forget to add slf4j-log4j.jar. When i added this slf4j-log4j12-1.5.6.jar my issue got resolved.

Apr 27

united health group java interview questions 2013|uhg interview process

UHG Interview process

United Health Group conducted  three rounds for java/j2ee developers on April 27th 2013.

For 2-5 years they conducted three rounds

1) Written test

2) Technical Round-1

3) Technical Round-2

Coming to written test,written test comprises of 20 questions .out of these 20 questions 10 questions are from exception handling and inheritance.

Technical interview is bit tough when compared to written test. In technical interview they asked in depth questions and unrelevant questions for java /j2ee developers with 0-3 years of experience.

Questions given here are posted by one of our visitors rajesh,attended interview on April 27th 2013. Lets say thanks to Rajesh.

United Health Group written test interview questions  and answers

1)      Which of the following type expressions  can be used in switch?

a)      int,byte

b)      int,byte,char,float,

c)       byte,short,int,char,enum

d)      int,char,String,float,double

e)      None of the above

Ans : C

Solution: In Switch statement only int ,char,byte,short expressions are allowed . float, double expressions are not allowed. From Java 6 even enums and Strings are allowed in Switch statements.

2) What is the output of the below program

[code lang=”java”]package UHG;

public class StringConcatDemo {

public static void main(String[] args) {
String s = "Hello";
s.concat("world");
System.out.println(s);
}

}
[/code]

a)      “Hello World”

b)      “World”;

c)       “Hello”

d)      Compilation error

e)      None of the above

3)  Which of the following are correct with regard to sleep() method?

a)      sleep() method suspends the current object for specified amount of time.

b)      sleep() will cause current thread to suspend for minimum amount of time specified in sleep method argument and releases the lock.

c)       sleep() will cause current thread to suspend for exact amount of time.

d)      Sleep() will cause current  thread to sleep for minimum amount of time specified in sleep() method argument. sleep() will not release any lock.

Ans : D

1) sleep() method supspends the current thread not current object for specified amount of time.

2) sleep() method does not releases any lock only wait(),notify() and notifyAll() method releases the lock.

3) Time specified in sleep method argument is the minimum amount of time current thread will sleep not the exact amount of time.

4) Choose the correct order of access modifiers

a) private,public,protected,default

b)public,private,default,protected

c)private,protected,public,default

d) private, default,protected,public

Ans : d

The correct order is d which is more restrictive private to least restrictive public

5) In JSP scope is associated with which object?

a) Session object

b) Servlet Context object

c) Servlet Config object

d) Page context object

Ans : D

Page context object is used to define different scopes such as SESSION_SCOPE, PAGE_SCOPE,REQUEST_SCOPE.

6)

[code lang=”java”]
package UHG;

public class ThreadExample implements Runnable {

public void start() {
System.out.println("Iam in start method");
}

public static void main(String[] args) {

Thread t = new Thread();
t.start();

}

}

[/code]

1)      Iam in start method

2)      Compile time error

3)      Runtime exception

4)      No output

5)      None of these

Explanation : whenever our class implements Runnable it should implement run() method but in the above code we don’t have run() we get compile time error.

2)      Compile time error

3)      Runtime exception

4)      No output

5)      None of these

7)

[code lang=”java”]

package UHG;

public class A {
A() {
System.out.println("Test started");
System.out.println("Iam in class A");
}

}

package UHG;

public class B extends A {

B() {
System.out.println("Iam in Class B");
}

}

package UHG;

public class C extends B {

public C() {
System.out.println("Iam in class c");
System.out.println("Test completed");
}

}

package UHG;

public class Test {

public static void main(String[] args) {

A a = new A();
B b = new B();
C c = new C();
}

}

[/code]

what is the output of the above program?

1) Test started

Iam in class A

Iam in Class B

Iam in class c

Test completed

2) Test started

Iam in class A

Test started

Iam in class A

Iam in Class B

Test started

Iam in class A

Iam in Class B

Iam in class c

Test completed

3) Test started

Iam in class A

Iam in Class B

Iam in class c

 

4)None of the above

Ans : 2

 

Apr 12

Difference between load() and get() in hibernate?

Difference between load() and get() in hibernate is one of the popular interview questions for both who attends telephonic as well as face to face interview. Here are list of differences if you find any more differences more than this you can post in comments section. I will include those valid comments in this tutorial.

 

Transaction tx = session.beginTransaction();Employee employee = (Employee) session.load(Employee.class, new Long(294912));

Employee employee1 = (Employee) session.get(Employee.class, new Long(294912));

tx.commit();

 

                         get()                    load()
If object is present in the database it return object otherwise it returns  null. If object is present in the database it return object otherwise it returns  objectNotFoundException if object is not found.load() never returns null.
get() never returns proxy object. Proxy object means object which has id or primarykey property and remaining properties are uninitialized. load() always returns proxy object.
get() eagerly loads the data i.e always hit the database load() lazily loads data.load hits the database on demand i.e whenever we are calling getter methods on pojo other than id property,proxied object gets initialized.
Performance wise it is not recommended to use get because it always hits the database. Performance wise it is recommended to use load because load will hit the database on demand.
We have four overloaded methods for get() method. We have five overloaded methods .public void load(Object object, Serializable id) throws HibernateException

This is an extra method which takes object as argument.

 

When to use get() and load() in database?

Both get() and load() are used to retrieve records from database.

1)    Use get() only when we are sure about record is existed in database other wise it is better to use load() method.

2)    load() performs lazy loading performance it is recommended to use load() method rather than get() which hits database everytime record is requested.

Apr 01

Static nested class example in java

package testclasses;

//Inner classes declared with static keyword are static nested classes
public class StaticNestedClassesExample {

static int x = 10;

public static class InnerClass {
void printText() {
System.out.println(“Iam inside static nested class”);
}
}

public static void main(String[] args) {
// Instance of outer class is not required to instantiatiate static
// nested class.
StaticNestedClassesExample.InnerClass staticNestedClass = new StaticNestedClassesExample.InnerClass();
staticNestedClass.printText();

}

}

Apr 01

Method local inner class example in java

[code lang=”java”]
package testclasses;

public class MethodLocalInnerExample {

private String str="Iam in outerClass";

public void getInnerClass()
{
final int i=10;

// class defined inside a method
//only abstract and final are permitted for method local inner class.
class InnerClass
{

public void methodLocalInner()
{
System.out.println("Iam in method local inner class");
System.out.println("Accesing inner class instance variable"+i);//Cannot refer to a non-final variable i inside an inner class defined in a different method if i is not declared as final;
}

}
//Method local inner class can be instantiated only with in the method where inner class is defined.
InnerClass innerClass=new InnerClass();
innerClass.methodLocalInner();
}

public static void main(String[] args) {
MethodLocalInnerExample ex=new MethodLocalInnerExample();
System.out.println(ex.str);
ex.getInnerClass();

}

}
[/code]

Apr 01

Member inner class example in java

[code lang=”java”]
package testclasses;

public class MemberInnerClassExample {

private String string;

public String getString() {
return string;
}

public void setString(String string) {
this.string = string;
}

public void instantiateInnerClass()
{
// we can instatiate inner class from outer class
InnerClass inner=new InnerClass();
inner.printInner();
}

// public,private,protected,final,abstract,static are eligible access modifiers for member inner class
public class InnerClass {
private String str;

// outer classes can access private members of inner class
private String getStr() {
return str;
}

private void setStr(String str) {
this.str = str;
}

public void printInner() {
System.out.println("Iam in inner");
}

}

public static void main(String[] args) {

MemberInnerClassExample innerClassExample = new MemberInnerClassExample();

innerClassExample.instantiateInnerClass();

innerClassExample.setString("Iam in outerClass");

// Inner class can only be accesed using outer class instance
//Instantiating inner class outside the class

MemberInnerClassExample.InnerClass in = innerClassExample.new InnerClass();

// outer classes can access private members of inner class
in.setStr("Iam in inner class");

System.out.println(innerClassExample.getString());
System.out.println(in.getStr());

}

}

[/code]

Apr 01

pass by value and pass by reference example in java

[code lang=”java”]

public class PassByReferenceExample {

public static void main(String[] args) {

PassByReferenceExample example = new PassByReferenceExample();
int i = 1;
example.primitiveMethod(i);
System.out.println("In main " + i);
StringBuffer sb = new StringBuffer("pass by reference");
example.referenceMethod(sb);
System.out.println("In main " + sb);

}

public void primitiveMethod(int i) {
i = i + 2;
System.out.println(" In method " + i);
}

public void referenceMethod(StringBuffer sb) {
sb.append("in java");
System.out.println("In method " + sb);

}

}

[/code]

Output

In method 3
In main 1
In method pass by referencein java
In main pass by referencein java